Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(EventEmitter) only apply if listener #35577 #36087

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Libraries/vendor/emitter/EventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ export default class EventEmitter<TEventToArgsMap: {...}>
listener: (...args: $ElementType<TEventToArgsMap, TEvent>) => mixed,
context: mixed,
): EventSubscription {
if (typeof listener !== 'function') {
throw new TypeError(
'EventEmitter.addListener(…): 2nd argument must be a function.',
);
}
const registrations = allocate<_, _, TEventToArgsMap[TEvent]>(
this._registry,
eventType,
Expand Down
8 changes: 8 additions & 0 deletions Libraries/vendor/emitter/__tests__/EventEmitter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ describe('listeners', () => {
expect(results).toEqual(['A', 'B', 'C']);
});

it('registers an event with an illegal argument', () => {
const emitter = new EventEmitter<{A: []}>();

expect(() => emitter.addListener('A', null)).toThrow(
'EventEmitter.addListener(…): 2nd argument must be a function.',
);
});

it('invokes the same listener registered multiple times', () => {
const emitter = new EventEmitter<{A: []}>();

Expand Down