1
- import { UIManager } from 'react-native'
1
+ import * as UIManager from 'react-native/Libraries/ReactNative/UIManager '
2
2
import getNativeComponentAttributes from 'react-native/Libraries/ReactNative/getNativeComponentAttributes'
3
- import ReactNativePrivateInterface from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface'
3
+ import * as ReactNativePrivateInterface from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface'
4
4
5
5
let ROOT_TAG
6
6
@@ -14,6 +14,11 @@ const KEYBOARD_EVENTS = ['topFocus', 'topEndEditing']
14
14
const FOCUS_EVENTS = [ 'topFocus' ]
15
15
const BLUR_EVENTS = [ 'topBlur' ]
16
16
17
+ const EVENTPHASE_NONE = 0
18
+ const EVENTPHASE_CAPTURE = 1
19
+ const EVENTPHASE_AT_TARGET = 2
20
+ const EVENTPHASE_BUBBLE = 3
21
+
17
22
const EVENT_TYPES = {
18
23
CLICK : 'Click' ,
19
24
CHANGE : 'Change' ,
@@ -409,17 +414,15 @@ class Element extends Node {
409
414
}
410
415
list . push ( {
411
416
_listener : fn ,
412
- _flags : getListenerFlags ( options ) ,
413
417
} )
414
418
}
415
419
416
420
removeEventListener ( type , listener , options ) {
417
421
const list = this [ LISTENERS ] . get ( type )
418
422
if ( ! list ) return false
419
- const flags = getListenerFlags ( options )
420
423
for ( let i = 0 ; i < list . length ; i ++ ) {
421
424
const item = list [ i ]
422
- if ( item . _listener === listener && item . _flags === flags ) {
425
+ if ( item . _listener === listener ) {
423
426
list . splice ( i , 1 )
424
427
return true
425
428
}
@@ -429,47 +432,33 @@ class Element extends Node {
429
432
430
433
dispatchEvent ( event ) {
431
434
let target = ( event . target = this )
432
- const path = ( event . path = [ this ] )
433
- while ( ( target = target . parentNode ) ) path . push ( target )
435
+ const bubblePath = [ ]
434
436
let defaultPrevented = false
435
- for ( let i = path . length ; i -- ; ) {
436
- if (
437
- fireEvent (
438
- event ,
439
- path [ i ] ,
440
- i === 0 ? EVENTPHASE_AT_TARGET : EVENTPHASE_CAPTURE
441
- )
442
- ) {
443
- defaultPrevented = true
444
- }
437
+
438
+ while ( target != null ) {
439
+ bubblePath . push ( target )
440
+ target = target . parentNode
445
441
}
446
- for ( let i = 1 ; i < path . length ; i ++ ) {
447
- if ( fireEvent ( event , path [ i ] , EVENTPHASE_BUBBLE ) ) {
442
+
443
+ for ( let i = bubblePath . length ; -- i ; ) {
444
+ if ( fireEvent ( event , bubblePath [ i ] , EVENTPHASE_CAPTURE ) ) {
448
445
defaultPrevented = true
449
446
}
450
447
}
451
- return ! defaultPrevented
452
- }
453
448
454
- render ( ) {
455
- const component = TYPES [ this . localName ] . hostComponent
456
- const _self = this
457
- const reactElement = {
458
- type : component ,
459
- props : { } ,
460
- ref : x => {
461
- if ( ! VIEWS_RENDERED ) {
462
- VIEWS_RENDERED = true
449
+ if ( fireEvent ( event , this , EVENTPHASE_AT_TARGET ) ) {
450
+ defaultPrevented = true
451
+ }
452
+
453
+ if ( ! event . cancelBubble ) {
454
+ for ( let i = 1 ; i < bubblePath . length ; i ++ ) {
455
+ if ( fireEvent ( event , bubblePath [ i ] , EVENTPHASE_BUBBLE ) ) {
456
+ defaultPrevented = true
463
457
}
464
- _self . ref = x
465
- INSTANCES . set ( this [ BINDING ] , x )
466
- } ,
458
+ }
467
459
}
468
460
469
- reactElement . props . children = ( this . children || [ ] ) . map ( x => x . render ( ) )
470
- Object . assign ( reactElement . props , Object . fromEntries ( this [ BINDING ] . props ) )
471
- reactElement . $$typeof = REACT_ELEMENT_TYPE
472
- return reactElement
461
+ return ! defaultPrevented
473
462
}
474
463
}
475
464
@@ -710,7 +699,7 @@ export function createDOM(rootTag) {
710
699
}
711
700
712
701
class Event {
713
- constructor ( type , bubbles , cancelable , timeStamp ) {
702
+ constructor ( type , bubbles , cancelable ) {
714
703
Object . defineProperty ( this , IS_TRUSTED , { value : false } )
715
704
this . type = type
716
705
this . bubbles = bubbles
@@ -720,16 +709,31 @@ class Event {
720
709
this . currentTarget = null
721
710
this . inPassiveListener = false
722
711
this . defaultPrevented = false
723
- this . cancelBubble = false
712
+ this . _stopPropagation = false
724
713
this . immediatePropagationStopped = false
725
714
this . data = undefined
715
+ this . timestamp = new Date ( ) . valueOf ( )
726
716
}
717
+
727
718
get isTrusted ( ) {
728
719
return this [ IS_TRUSTED ]
729
720
}
721
+
722
+ get cancelBubble ( ) {
723
+ return this . _stopPropagation
724
+ }
725
+
726
+ set cancelBubble ( val ) {
727
+ if ( val ) {
728
+ this . _stopPropagation = true
729
+ }
730
+ }
731
+
730
732
stopPropagation ( ) {
731
733
this . cancelBubble = true
734
+ this . _stopPropagation = true
732
735
}
736
+
733
737
stopImmediatePropagation ( ) {
734
738
this . immediatePropagationStopped = true
735
739
}
@@ -744,61 +748,30 @@ class Event {
744
748
}
745
749
}
746
750
747
- const EVENTPHASE_NONE = 0
748
- const EVENTPHASE_BUBBLE = 1
749
- const EVENTPHASE_CAPTURE = 2
750
- const EVENTPHASE_PASSIVE = 4
751
- const EVENTPHASE_AT_TARGET = 5
752
- const EVENTOPT_ONCE = 8
753
-
754
- // Flags are easier to compare for listener lookups
755
- function getListenerFlags ( options ) {
756
- if ( typeof options === 'object' && options ) {
757
- let flags = options . capture ? EVENTPHASE_CAPTURE : EVENTPHASE_BUBBLE
758
- if ( options . passive ) flags &= EVENTPHASE_PASSIVE
759
- if ( options . once ) flags &= EVENTOPT_ONCE
760
- return flags
761
- }
762
- return options ? EVENTPHASE_CAPTURE : EVENTPHASE_BUBBLE
763
- }
764
-
765
751
function fireEvent ( event , target , phase ) {
766
752
const list = target [ LISTENERS ] . get ( event . type )
767
753
if ( ! list ) return
768
- // let error;
754
+
769
755
let defaultPrevented = false
770
- // use forEach for freezing
771
- const frozen = list . slice ( )
772
- for ( let i = 0 ; i < frozen . length ; i ++ ) {
773
- const item = frozen [ i ]
774
- const fn = item . _listener
756
+
757
+ for ( const item of Array . from ( list ) ) {
775
758
event . eventPhase = phase
776
- // the bridge is async, so events are always passive.
777
- //event.inPassiveListener = passive;
778
759
event . currentTarget = target
779
760
try {
780
- let ret = fn . call ( target , event )
761
+ let ret = item . _listener . call ( target , event )
781
762
if ( ret === false ) {
782
763
event . defaultPrevented = true
783
764
}
784
765
} catch ( e ) {
785
- //error = e;
786
766
setTimeout ( thrower , 0 , e )
787
767
}
788
- // @ts -ignore
789
- // FIXME: the binary shift is always going to be true, need to
790
- // handle based on options
791
- if ( item . _flags & ( EVENTOPT_ONCE !== 0 ) ) {
792
- // list.splice(list.indexOf(item), 1)
793
- }
794
768
if ( event . defaultPrevented === true ) {
795
769
defaultPrevented = true
796
770
}
797
771
if ( event . immediatePropagationStopped ) {
798
772
break
799
773
}
800
774
}
801
- // if (error !== undefined) throw error;
802
775
return defaultPrevented
803
776
}
804
777
0 commit comments