Skip to content

Commit 726ae87

Browse files
author
cedric Madoerin
committed
fix(EventEmitter) throw non func argument facebook#35577 facebook#36087
1 parent 2a2c1f2 commit 726ae87

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

Libraries/vendor/emitter/EventEmitter.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ export default class EventEmitter<TEventToArgsMap: {...}>
7474
listener: (...args: $ElementType<TEventToArgsMap, TEvent>) => mixed,
7575
context: mixed,
7676
): EventSubscription {
77+
if (typeof listener !== 'function') {
78+
throw new TypeError('EventEmitter.addListener(…): 2nd argument must be a function.');
79+
}
7780
const registrations = allocate<_, _, TEventToArgsMap[TEvent]>(
7881
this._registry,
7982
eventType,

Libraries/vendor/emitter/__tests__/EventEmitter-test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ describe('listeners', () => {
7878
expect(results).toEqual(['A', 'B', 'C']);
7979
});
8080

81+
it('registers an event with an illegal argument', () => {
82+
const emitter = new EventEmitter<{A: []}>();
83+
const fakeFunction = ({}: any);
84+
85+
expect(() => emitter.addListener('A', fakeFunction)).toThrow('EventEmitter.addListener(…): 2nd argument must be a function.');
86+
});
87+
8188
it('invokes the same listener registered multiple times', () => {
8289
const emitter = new EventEmitter<{A: []}>();
8390

0 commit comments

Comments
 (0)