File tree Expand file tree Collapse file tree
packages/react-native/Libraries/vendor/emitter Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -84,7 +84,7 @@ export default class EventEmitter<
8484
8585 constructor ( ) {
8686 // $FlowFixMe[incompatible-type]
87- this. #registry = { } ;
87+ this. #registry = Object . create ( null ) ;
8888 }
8989
9090 /**
@@ -147,7 +147,7 @@ export default class EventEmitter<
147147 ) : void {
148148 if ( eventType == null ) {
149149 // $FlowFixMe[incompatible-type]
150- this . #registry = { } ;
150+ this . #registry = Object . create ( null ) ;
151151 } else {
152152 // $FlowFixMe[cannot-write]
153153 delete this . #registry[ eventType ] ;
Original file line number Diff line number Diff line change @@ -57,6 +57,27 @@ describe('listeners', () => {
5757 expect ( listenerB ) . toHaveBeenCalledTimes ( 1 ) ;
5858 } ) ;
5959
60+ it ( 'supports event types that match Object prototype keys' , ( ) => {
61+ const emitter = new EventEmitter < Readonly < Record < string , [ ] > >> ( ) ;
62+
63+ const constructorListener = jest . fn ( ) ;
64+ const protoListener = jest . fn ( ) ;
65+ emitter . addListener ( 'constructor' , constructorListener ) ;
66+ emitter . addListener ( '__proto__' , protoListener ) ;
67+
68+ emitter . emit ( 'constructor' ) ;
69+ emitter . emit ( '__proto__' ) ;
70+
71+ expect ( constructorListener ) . toHaveBeenCalledTimes ( 1 ) ;
72+ expect ( protoListener ) . toHaveBeenCalledTimes ( 1 ) ;
73+ expect ( emitter . listenerCount ( 'constructor' ) ) . toBe ( 1 ) ;
74+ expect ( emitter . listenerCount ( '__proto__' ) ) . toBe ( 1 ) ;
75+
76+ emitter . removeAllListeners ( ) ;
77+ expect ( emitter . listenerCount ( 'constructor' ) ) . toBe ( 0 ) ;
78+ expect ( emitter . listenerCount ( '__proto__' ) ) . toBe ( 0 ) ;
79+ } ) ;
80+
6081 it ( 'invokes listeners in registration order' , ( ) => {
6182 const emitter = new EventEmitter < { A : [ ] } > ( ) ;
6283
You can’t perform that action at this time.
0 commit comments