From f1608743c83e8ce7b56aec98ccdddacc91b86179 Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Fri, 2 Feb 2024 13:22:28 +0100 Subject: [PATCH] refactor!: remove add/removeEventListener in favor of on/off (#11792) --- ...uppeteer.commoneventemitter.addlistener.md | 27 -------------- docs/api/puppeteer.commoneventemitter.md | 2 -- ...eteer.commoneventemitter.removelistener.md | 27 -------------- .../api/puppeteer.eventemitter.addlistener.md | 33 ----------------- docs/api/puppeteer.eventemitter.md | 18 +++++----- .../puppeteer.eventemitter.removelistener.md | 33 ----------------- .../src/common/EventEmitter.test.ts | 8 ++--- .../puppeteer-core/src/common/EventEmitter.ts | 36 ------------------- 8 files changed, 10 insertions(+), 174 deletions(-) delete mode 100644 docs/api/puppeteer.commoneventemitter.addlistener.md delete mode 100644 docs/api/puppeteer.commoneventemitter.removelistener.md delete mode 100644 docs/api/puppeteer.eventemitter.addlistener.md delete mode 100644 docs/api/puppeteer.eventemitter.removelistener.md diff --git a/docs/api/puppeteer.commoneventemitter.addlistener.md b/docs/api/puppeteer.commoneventemitter.addlistener.md deleted file mode 100644 index 99a8a74433c..00000000000 --- a/docs/api/puppeteer.commoneventemitter.addlistener.md +++ /dev/null @@ -1,27 +0,0 @@ ---- -sidebar_label: CommonEventEmitter.addListener ---- - -# CommonEventEmitter.addListener() method - -#### Signature: - -```typescript -interface CommonEventEmitter { - addListener( - type: Key, - handler: Handler - ): this; -} -``` - -## Parameters - -| Parameter | Type | Description | -| --------- | ------------------------------------------------------ | ----------- | -| type | Key | | -| handler | [Handler](./puppeteer.handler.md)<Events\[Key\]> | | - -**Returns:** - -this diff --git a/docs/api/puppeteer.commoneventemitter.md b/docs/api/puppeteer.commoneventemitter.md index d117efeb4be..9ef669c2c31 100644 --- a/docs/api/puppeteer.commoneventemitter.md +++ b/docs/api/puppeteer.commoneventemitter.md @@ -14,11 +14,9 @@ export interface CommonEventEmitter> | Method | Description | | --------------------------------------------------------------------------------- | ----------- | -| [addListener(type, handler)](./puppeteer.commoneventemitter.addlistener.md) | | | [emit(type, event)](./puppeteer.commoneventemitter.emit.md) | | | [listenerCount(event)](./puppeteer.commoneventemitter.listenercount.md) | | | [off(type, handler)](./puppeteer.commoneventemitter.off.md) | | | [on(type, handler)](./puppeteer.commoneventemitter.on.md) | | | [once(type, handler)](./puppeteer.commoneventemitter.once.md) | | | [removeAllListeners(event)](./puppeteer.commoneventemitter.removealllisteners.md) | | -| [removeListener(type, handler)](./puppeteer.commoneventemitter.removelistener.md) | | diff --git a/docs/api/puppeteer.commoneventemitter.removelistener.md b/docs/api/puppeteer.commoneventemitter.removelistener.md deleted file mode 100644 index fbe04a68aa0..00000000000 --- a/docs/api/puppeteer.commoneventemitter.removelistener.md +++ /dev/null @@ -1,27 +0,0 @@ ---- -sidebar_label: CommonEventEmitter.removeListener ---- - -# CommonEventEmitter.removeListener() method - -#### Signature: - -```typescript -interface CommonEventEmitter { - removeListener( - type: Key, - handler: Handler - ): this; -} -``` - -## Parameters - -| Parameter | Type | Description | -| --------- | ------------------------------------------------------ | ----------- | -| type | Key | | -| handler | [Handler](./puppeteer.handler.md)<Events\[Key\]> | | - -**Returns:** - -this diff --git a/docs/api/puppeteer.eventemitter.addlistener.md b/docs/api/puppeteer.eventemitter.addlistener.md deleted file mode 100644 index d653ac50dc7..00000000000 --- a/docs/api/puppeteer.eventemitter.addlistener.md +++ /dev/null @@ -1,33 +0,0 @@ ---- -sidebar_label: EventEmitter.addListener ---- - -# EventEmitter.addListener() method - -> Warning: This API is now obsolete. -> -> please use [EventEmitter.on()](./puppeteer.eventemitter.on.md) instead. - -Add an event listener. - -#### Signature: - -```typescript -class EventEmitter { - addListener>( - type: Key, - handler: Handler[Key]> - ): this; -} -``` - -## Parameters - -| Parameter | Type | Description | -| --------- | --------------------------------------------------------------------------------------------------------------------- | ----------- | -| type | Key | | -| handler | [Handler](./puppeteer.handler.md)<[EventsWithWildcard](./puppeteer.eventswithwildcard.md)<Events>\[Key\]> | | - -**Returns:** - -this diff --git a/docs/api/puppeteer.eventemitter.md b/docs/api/puppeteer.eventemitter.md index 1a3865ba982..4ccdab9028d 100644 --- a/docs/api/puppeteer.eventemitter.md +++ b/docs/api/puppeteer.eventemitter.md @@ -22,13 +22,11 @@ The constructor for this class is marked as internal. Third-party code should no ## Methods -| Method | Modifiers | Description | -| --------------------------------------------------------------------------- | --------- | ------------------------------------------------------------------------------------------------ | -| [addListener(type, handler)](./puppeteer.eventemitter.addlistener.md) | | Add an event listener. | -| [emit(type, event)](./puppeteer.eventemitter.emit.md) | | Emit an event and call any associated listeners. | -| [listenerCount(type)](./puppeteer.eventemitter.listenercount.md) | | Gets the number of listeners for a given event. | -| [off(type, handler)](./puppeteer.eventemitter.off.md) | | Remove an event listener from firing. | -| [on(type, handler)](./puppeteer.eventemitter.on.md) | | Bind an event listener to fire when an event occurs. | -| [once(type, handler)](./puppeteer.eventemitter.once.md) | | Like on but the listener will only be fired once and then it will be removed. | -| [removeAllListeners(type)](./puppeteer.eventemitter.removealllisteners.md) | | Removes all listeners. If given an event argument, it will remove only listeners for that event. | -| [removeListener(type, handler)](./puppeteer.eventemitter.removelistener.md) | | Remove an event listener. | +| Method | Modifiers | Description | +| -------------------------------------------------------------------------- | --------- | ------------------------------------------------------------------------------------------------ | +| [emit(type, event)](./puppeteer.eventemitter.emit.md) | | Emit an event and call any associated listeners. | +| [listenerCount(type)](./puppeteer.eventemitter.listenercount.md) | | Gets the number of listeners for a given event. | +| [off(type, handler)](./puppeteer.eventemitter.off.md) | | Remove an event listener from firing. | +| [on(type, handler)](./puppeteer.eventemitter.on.md) | | Bind an event listener to fire when an event occurs. | +| [once(type, handler)](./puppeteer.eventemitter.once.md) | | Like on but the listener will only be fired once and then it will be removed. | +| [removeAllListeners(type)](./puppeteer.eventemitter.removealllisteners.md) | | Removes all listeners. If given an event argument, it will remove only listeners for that event. | diff --git a/docs/api/puppeteer.eventemitter.removelistener.md b/docs/api/puppeteer.eventemitter.removelistener.md deleted file mode 100644 index c2127cebd6d..00000000000 --- a/docs/api/puppeteer.eventemitter.removelistener.md +++ /dev/null @@ -1,33 +0,0 @@ ---- -sidebar_label: EventEmitter.removeListener ---- - -# EventEmitter.removeListener() method - -> Warning: This API is now obsolete. -> -> please use [EventEmitter.off()](./puppeteer.eventemitter.off.md) instead. - -Remove an event listener. - -#### Signature: - -```typescript -class EventEmitter { - removeListener>( - type: Key, - handler: Handler[Key]> - ): this; -} -``` - -## Parameters - -| Parameter | Type | Description | -| --------- | --------------------------------------------------------------------------------------------------------------------- | ----------- | -| type | Key | | -| handler | [Handler](./puppeteer.handler.md)<[EventsWithWildcard](./puppeteer.eventswithwildcard.md)<Events>\[Key\]> | | - -**Returns:** - -this diff --git a/packages/puppeteer-core/src/common/EventEmitter.test.ts b/packages/puppeteer-core/src/common/EventEmitter.test.ts index cf05ef67001..f3875e99e86 100644 --- a/packages/puppeteer-core/src/common/EventEmitter.test.ts +++ b/packages/puppeteer-core/src/common/EventEmitter.test.ts @@ -19,7 +19,7 @@ describe('EventEmitter', () => { }); describe('on', () => { - const onTests = (methodName: 'on' | 'addListener'): void => { + const onTests = (methodName: 'on'): void => { it(`${methodName}: adds an event listener that is fired when the event is emitted`, () => { const listener = sinon.spy(); emitter[methodName]('foo', listener); @@ -43,12 +43,10 @@ describe('EventEmitter', () => { }); }; onTests('on'); - // we support addListener for legacy reasons - onTests('addListener'); }); describe('off', () => { - const offTests = (methodName: 'off' | 'removeListener'): void => { + const offTests = (methodName: 'off'): void => { it(`${methodName}: removes the listener so it is no longer called`, () => { const listener = sinon.spy(); emitter.on('foo', listener); @@ -67,8 +65,6 @@ describe('EventEmitter', () => { }); }; offTests('off'); - // we support removeListener for legacy reasons - offTests('removeListener'); }); describe('once', () => { diff --git a/packages/puppeteer-core/src/common/EventEmitter.ts b/packages/puppeteer-core/src/common/EventEmitter.ts index 4a8bcb801f7..0aace8b2eb8 100644 --- a/packages/puppeteer-core/src/common/EventEmitter.ts +++ b/packages/puppeteer-core/src/common/EventEmitter.ts @@ -27,18 +27,6 @@ export interface CommonEventEmitter> { handler?: Handler ): this; emit(type: Key, event: Events[Key]): boolean; - /* To maintain parity with the built in NodeJS event emitter which uses removeListener - * rather than `off`. - * If you're implementing new code you should use `off`. - */ - addListener( - type: Key, - handler: Handler - ): this; - removeListener( - type: Key, - handler: Handler - ): this; once( type: Key, handler: Handler @@ -148,30 +136,6 @@ export class EventEmitter> return this.listenerCount(type) > 0; } - /** - * Remove an event listener. - * - * @deprecated please use {@link EventEmitter.off} instead. - */ - removeListener>( - type: Key, - handler: Handler[Key]> - ): this { - return this.off(type, handler); - } - - /** - * Add an event listener. - * - * @deprecated please use {@link EventEmitter.on} instead. - */ - addListener>( - type: Key, - handler: Handler[Key]> - ): this { - return this.on(type, handler); - } - /** * Like `on` but the listener will only be fired once and then it will be removed. * @param type - the event you'd like to listen to