Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export default class EventEmitter<

constructor() {
// $FlowFixMe[incompatible-type]
this.#registry = {};
this.#registry = Object.create(null);
}

/**
Expand Down Expand Up @@ -147,7 +147,7 @@ export default class EventEmitter<
): void {
if (eventType == null) {
// $FlowFixMe[incompatible-type]
this.#registry = {};
this.#registry = Object.create(null);
} else {
// $FlowFixMe[cannot-write]
delete this.#registry[eventType];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ describe('listeners', () => {
expect(listenerB).toHaveBeenCalledTimes(1);
});

it('supports event types that match Object prototype keys', () => {
const emitter = new EventEmitter<Readonly<Record<string, []>>>();

const constructorListener = jest.fn();
const protoListener = jest.fn();
emitter.addListener('constructor', constructorListener);
emitter.addListener('__proto__', protoListener);

emitter.emit('constructor');
emitter.emit('__proto__');

expect(constructorListener).toHaveBeenCalledTimes(1);
expect(protoListener).toHaveBeenCalledTimes(1);
expect(emitter.listenerCount('constructor')).toBe(1);
expect(emitter.listenerCount('__proto__')).toBe(1);

emitter.removeAllListeners();
expect(emitter.listenerCount('constructor')).toBe(0);
expect(emitter.listenerCount('__proto__')).toBe(0);
});

it('invokes listeners in registration order', () => {
const emitter = new EventEmitter<{A: []}>();

Expand Down
Loading