refactor!: remove add/removeEventListener in favor of on/off (#11792)

This commit is contained in:
Alex Rudenko 2024-02-02 13:22:28 +01:00 committed by GitHub
parent d4e9d8d591
commit f1608743c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 10 additions and 174 deletions

View File

@ -1,27 +0,0 @@
---
sidebar_label: CommonEventEmitter.addListener
---
# CommonEventEmitter.addListener() method
#### Signature:
```typescript
interface CommonEventEmitter {
addListener<Key extends keyof Events>(
type: Key,
handler: Handler<Events[Key]>
): this;
}
```
## Parameters
| Parameter | Type | Description |
| --------- | ------------------------------------------------------ | ----------- |
| type | Key | |
| handler | [Handler](./puppeteer.handler.md)&lt;Events\[Key\]&gt; | |
**Returns:**
this

View File

@ -14,11 +14,9 @@ export interface CommonEventEmitter<Events extends Record<EventType, unknown>>
| 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) | |

View File

@ -1,27 +0,0 @@
---
sidebar_label: CommonEventEmitter.removeListener
---
# CommonEventEmitter.removeListener() method
#### Signature:
```typescript
interface CommonEventEmitter {
removeListener<Key extends keyof Events>(
type: Key,
handler: Handler<Events[Key]>
): this;
}
```
## Parameters
| Parameter | Type | Description |
| --------- | ------------------------------------------------------ | ----------- |
| type | Key | |
| handler | [Handler](./puppeteer.handler.md)&lt;Events\[Key\]&gt; | |
**Returns:**
this

View File

@ -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<Key extends keyof EventsWithWildcard<Events>>(
type: Key,
handler: Handler<EventsWithWildcard<Events>[Key]>
): this;
}
```
## Parameters
| Parameter | Type | Description |
| --------- | --------------------------------------------------------------------------------------------------------------------- | ----------- |
| type | Key | |
| handler | [Handler](./puppeteer.handler.md)&lt;[EventsWithWildcard](./puppeteer.eventswithwildcard.md)&lt;Events&gt;\[Key\]&gt; | |
**Returns:**
this

View File

@ -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 <code>on</code> 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 <code>on</code> 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. |

View File

@ -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<Key extends keyof EventsWithWildcard<Events>>(
type: Key,
handler: Handler<EventsWithWildcard<Events>[Key]>
): this;
}
```
## Parameters
| Parameter | Type | Description |
| --------- | --------------------------------------------------------------------------------------------------------------------- | ----------- |
| type | Key | |
| handler | [Handler](./puppeteer.handler.md)&lt;[EventsWithWildcard](./puppeteer.eventswithwildcard.md)&lt;Events&gt;\[Key\]&gt; | |
**Returns:**
this

View File

@ -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', () => {

View File

@ -27,18 +27,6 @@ export interface CommonEventEmitter<Events extends Record<EventType, unknown>> {
handler?: Handler<Events[Key]>
): this;
emit<Key extends keyof Events>(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<Key extends keyof Events>(
type: Key,
handler: Handler<Events[Key]>
): this;
removeListener<Key extends keyof Events>(
type: Key,
handler: Handler<Events[Key]>
): this;
once<Key extends keyof Events>(
type: Key,
handler: Handler<Events[Key]>
@ -148,30 +136,6 @@ export class EventEmitter<Events extends Record<EventType, unknown>>
return this.listenerCount(type) > 0;
}
/**
* Remove an event listener.
*
* @deprecated please use {@link EventEmitter.off} instead.
*/
removeListener<Key extends keyof EventsWithWildcard<Events>>(
type: Key,
handler: Handler<EventsWithWildcard<Events>[Key]>
): this {
return this.off(type, handler);
}
/**
* Add an event listener.
*
* @deprecated please use {@link EventEmitter.on} instead.
*/
addListener<Key extends keyof EventsWithWildcard<Events>>(
type: Key,
handler: Handler<EventsWithWildcard<Events>[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