mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore: EventEmitter should mimic NodeJS' one (#10241)
This commit is contained in:
parent
557ec24cfc
commit
0cac94c0d6
@ -8,7 +8,7 @@ sidebar_label: CommonEventEmitter.addListener
|
||||
|
||||
```typescript
|
||||
interface CommonEventEmitter {
|
||||
addListener(event: EventType, handler: Handler): CommonEventEmitter;
|
||||
addListener(event: EventType, handler: Handler): this;
|
||||
}
|
||||
```
|
||||
|
||||
@ -21,4 +21,4 @@ interface CommonEventEmitter {
|
||||
|
||||
**Returns:**
|
||||
|
||||
[CommonEventEmitter](./puppeteer.commoneventemitter.md)
|
||||
this
|
||||
|
@ -8,7 +8,7 @@ sidebar_label: CommonEventEmitter.off
|
||||
|
||||
```typescript
|
||||
interface CommonEventEmitter {
|
||||
off(event: EventType, handler: Handler): CommonEventEmitter;
|
||||
off(event: EventType, handler: Handler): this;
|
||||
}
|
||||
```
|
||||
|
||||
@ -21,4 +21,4 @@ interface CommonEventEmitter {
|
||||
|
||||
**Returns:**
|
||||
|
||||
[CommonEventEmitter](./puppeteer.commoneventemitter.md)
|
||||
this
|
||||
|
@ -8,7 +8,7 @@ sidebar_label: CommonEventEmitter.on
|
||||
|
||||
```typescript
|
||||
interface CommonEventEmitter {
|
||||
on(event: EventType, handler: Handler): CommonEventEmitter;
|
||||
on(event: EventType, handler: Handler): this;
|
||||
}
|
||||
```
|
||||
|
||||
@ -21,4 +21,4 @@ interface CommonEventEmitter {
|
||||
|
||||
**Returns:**
|
||||
|
||||
[CommonEventEmitter](./puppeteer.commoneventemitter.md)
|
||||
this
|
||||
|
@ -8,7 +8,7 @@ sidebar_label: CommonEventEmitter.once
|
||||
|
||||
```typescript
|
||||
interface CommonEventEmitter {
|
||||
once(event: EventType, handler: Handler): CommonEventEmitter;
|
||||
once(event: EventType, handler: Handler): this;
|
||||
}
|
||||
```
|
||||
|
||||
@ -21,4 +21,4 @@ interface CommonEventEmitter {
|
||||
|
||||
**Returns:**
|
||||
|
||||
[CommonEventEmitter](./puppeteer.commoneventemitter.md)
|
||||
this
|
||||
|
@ -8,7 +8,7 @@ sidebar_label: CommonEventEmitter.removeAllListeners
|
||||
|
||||
```typescript
|
||||
interface CommonEventEmitter {
|
||||
removeAllListeners(event?: EventType): CommonEventEmitter;
|
||||
removeAllListeners(event?: EventType): this;
|
||||
}
|
||||
```
|
||||
|
||||
@ -20,4 +20,4 @@ interface CommonEventEmitter {
|
||||
|
||||
**Returns:**
|
||||
|
||||
[CommonEventEmitter](./puppeteer.commoneventemitter.md)
|
||||
this
|
||||
|
@ -8,7 +8,7 @@ sidebar_label: CommonEventEmitter.removeListener
|
||||
|
||||
```typescript
|
||||
interface CommonEventEmitter {
|
||||
removeListener(event: EventType, handler: Handler): CommonEventEmitter;
|
||||
removeListener(event: EventType, handler: Handler): this;
|
||||
}
|
||||
```
|
||||
|
||||
@ -21,4 +21,4 @@ interface CommonEventEmitter {
|
||||
|
||||
**Returns:**
|
||||
|
||||
[CommonEventEmitter](./puppeteer.commoneventemitter.md)
|
||||
this
|
||||
|
@ -14,7 +14,7 @@ Add an event listener.
|
||||
|
||||
```typescript
|
||||
class EventEmitter {
|
||||
addListener(event: EventType, handler: Handler<any>): EventEmitter;
|
||||
addListener(event: EventType, handler: Handler<any>): this;
|
||||
}
|
||||
```
|
||||
|
||||
@ -27,4 +27,4 @@ class EventEmitter {
|
||||
|
||||
**Returns:**
|
||||
|
||||
[EventEmitter](./puppeteer.eventemitter.md)
|
||||
this
|
||||
|
@ -10,7 +10,7 @@ Remove an event listener from firing.
|
||||
|
||||
```typescript
|
||||
class EventEmitter {
|
||||
off(event: EventType, handler: Handler<any>): EventEmitter;
|
||||
off(event: EventType, handler: Handler<any>): this;
|
||||
}
|
||||
```
|
||||
|
||||
@ -23,6 +23,6 @@ class EventEmitter {
|
||||
|
||||
**Returns:**
|
||||
|
||||
[EventEmitter](./puppeteer.eventemitter.md)
|
||||
this
|
||||
|
||||
`this` to enable you to chain method calls.
|
||||
|
@ -10,7 +10,7 @@ Bind an event listener to fire when an event occurs.
|
||||
|
||||
```typescript
|
||||
class EventEmitter {
|
||||
on(event: EventType, handler: Handler<any>): EventEmitter;
|
||||
on(event: EventType, handler: Handler<any>): this;
|
||||
}
|
||||
```
|
||||
|
||||
@ -23,6 +23,6 @@ class EventEmitter {
|
||||
|
||||
**Returns:**
|
||||
|
||||
[EventEmitter](./puppeteer.eventemitter.md)
|
||||
this
|
||||
|
||||
`this` to enable you to chain method calls.
|
||||
|
@ -10,7 +10,7 @@ Like `on` but the listener will only be fired once and then it will be removed.
|
||||
|
||||
```typescript
|
||||
class EventEmitter {
|
||||
once(event: EventType, handler: Handler<any>): EventEmitter;
|
||||
once(event: EventType, handler: Handler<any>): this;
|
||||
}
|
||||
```
|
||||
|
||||
@ -23,6 +23,6 @@ class EventEmitter {
|
||||
|
||||
**Returns:**
|
||||
|
||||
[EventEmitter](./puppeteer.eventemitter.md)
|
||||
this
|
||||
|
||||
`this` to enable you to chain method calls.
|
||||
|
@ -10,7 +10,7 @@ Removes all listeners. If given an event argument, it will remove only listeners
|
||||
|
||||
```typescript
|
||||
class EventEmitter {
|
||||
removeAllListeners(event?: EventType): EventEmitter;
|
||||
removeAllListeners(event?: EventType): this;
|
||||
}
|
||||
```
|
||||
|
||||
@ -22,6 +22,6 @@ class EventEmitter {
|
||||
|
||||
**Returns:**
|
||||
|
||||
[EventEmitter](./puppeteer.eventemitter.md)
|
||||
this
|
||||
|
||||
`this` to enable you to chain method calls.
|
||||
|
@ -14,7 +14,7 @@ Remove an event listener.
|
||||
|
||||
```typescript
|
||||
class EventEmitter {
|
||||
removeListener(event: EventType, handler: Handler<any>): EventEmitter;
|
||||
removeListener(event: EventType, handler: Handler<any>): this;
|
||||
}
|
||||
```
|
||||
|
||||
@ -27,4 +27,4 @@ class EventEmitter {
|
||||
|
||||
**Returns:**
|
||||
|
||||
[EventEmitter](./puppeteer.eventemitter.md)
|
||||
this
|
||||
|
@ -11,7 +11,7 @@ class Page {
|
||||
off<K extends keyof PageEventObject>(
|
||||
eventName: K,
|
||||
handler: (event: PageEventObject[K]) => void
|
||||
): EventEmitter;
|
||||
): this;
|
||||
}
|
||||
```
|
||||
|
||||
@ -24,4 +24,4 @@ class Page {
|
||||
|
||||
**Returns:**
|
||||
|
||||
[EventEmitter](./puppeteer.eventemitter.md)
|
||||
this
|
||||
|
@ -19,7 +19,7 @@ class Page {
|
||||
on<K extends keyof PageEventObject>(
|
||||
eventName: K,
|
||||
handler: (event: PageEventObject[K]) => void
|
||||
): EventEmitter;
|
||||
): this;
|
||||
}
|
||||
```
|
||||
|
||||
@ -32,4 +32,4 @@ class Page {
|
||||
|
||||
**Returns:**
|
||||
|
||||
[EventEmitter](./puppeteer.eventemitter.md)
|
||||
this
|
||||
|
@ -11,7 +11,7 @@ class Page {
|
||||
once<K extends keyof PageEventObject>(
|
||||
eventName: K,
|
||||
handler: (event: PageEventObject[K]) => void
|
||||
): EventEmitter;
|
||||
): this;
|
||||
}
|
||||
```
|
||||
|
||||
@ -24,4 +24,4 @@ class Page {
|
||||
|
||||
**Returns:**
|
||||
|
||||
[EventEmitter](./puppeteer.eventemitter.md)
|
||||
this
|
||||
|
@ -109,22 +109,22 @@ export class Locator extends EventEmitter {
|
||||
override on<K extends keyof LocatorEventObject>(
|
||||
eventName: K,
|
||||
handler: (event: LocatorEventObject[K]) => void
|
||||
): Locator {
|
||||
return super.on(eventName, handler) as Locator;
|
||||
): this {
|
||||
return super.on(eventName, handler);
|
||||
}
|
||||
|
||||
override once<K extends keyof LocatorEventObject>(
|
||||
eventName: K,
|
||||
handler: (event: LocatorEventObject[K]) => void
|
||||
): Locator {
|
||||
return super.once(eventName, handler) as Locator;
|
||||
): this {
|
||||
return super.once(eventName, handler);
|
||||
}
|
||||
|
||||
override off<K extends keyof LocatorEventObject>(
|
||||
eventName: K,
|
||||
handler: (event: LocatorEventObject[K]) => void
|
||||
): Locator {
|
||||
return super.off(eventName, handler) as Locator;
|
||||
): this {
|
||||
return super.off(eventName, handler);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -480,7 +480,7 @@ export class Page extends EventEmitter {
|
||||
override on<K extends keyof PageEventObject>(
|
||||
eventName: K,
|
||||
handler: (event: PageEventObject[K]) => void
|
||||
): EventEmitter {
|
||||
): this {
|
||||
if (eventName === 'request') {
|
||||
const wrap =
|
||||
this.#handlerMap.get(handler) ||
|
||||
@ -500,7 +500,7 @@ export class Page extends EventEmitter {
|
||||
override once<K extends keyof PageEventObject>(
|
||||
eventName: K,
|
||||
handler: (event: PageEventObject[K]) => void
|
||||
): EventEmitter {
|
||||
): this {
|
||||
// Note: this method only exists to define the types; we delegate the impl
|
||||
// to EventEmitter.
|
||||
return super.once(eventName, handler);
|
||||
@ -509,7 +509,7 @@ export class Page extends EventEmitter {
|
||||
override off<K extends keyof PageEventObject>(
|
||||
eventName: K,
|
||||
handler: (event: PageEventObject[K]) => void
|
||||
): EventEmitter {
|
||||
): this {
|
||||
if (eventName === 'request') {
|
||||
handler = this.#handlerMap.get(handler) || handler;
|
||||
}
|
||||
|
@ -29,19 +29,19 @@ export type Handler<T = unknown> = (event: T) => void;
|
||||
* @public
|
||||
*/
|
||||
export interface CommonEventEmitter {
|
||||
on(event: EventType, handler: Handler): CommonEventEmitter;
|
||||
off(event: EventType, handler: Handler): CommonEventEmitter;
|
||||
on(event: EventType, handler: Handler): this;
|
||||
off(event: EventType, handler: Handler): this;
|
||||
/* 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(event: EventType, handler: Handler): CommonEventEmitter;
|
||||
removeListener(event: EventType, handler: Handler): CommonEventEmitter;
|
||||
addListener(event: EventType, handler: Handler): this;
|
||||
removeListener(event: EventType, handler: Handler): this;
|
||||
emit(event: EventType, eventData?: unknown): boolean;
|
||||
once(event: EventType, handler: Handler): CommonEventEmitter;
|
||||
once(event: EventType, handler: Handler): this;
|
||||
listenerCount(event: string): number;
|
||||
|
||||
removeAllListeners(event?: EventType): CommonEventEmitter;
|
||||
removeAllListeners(event?: EventType): this;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -73,7 +73,7 @@ export class EventEmitter implements CommonEventEmitter {
|
||||
* @param handler - the function to be called when the event occurs.
|
||||
* @returns `this` to enable you to chain method calls.
|
||||
*/
|
||||
on(event: EventType, handler: Handler<any>): EventEmitter {
|
||||
on(event: EventType, handler: Handler<any>): this {
|
||||
this.emitter.on(event, handler);
|
||||
return this;
|
||||
}
|
||||
@ -84,7 +84,7 @@ export class EventEmitter implements CommonEventEmitter {
|
||||
* @param handler - the function that should be removed.
|
||||
* @returns `this` to enable you to chain method calls.
|
||||
*/
|
||||
off(event: EventType, handler: Handler<any>): EventEmitter {
|
||||
off(event: EventType, handler: Handler<any>): this {
|
||||
this.emitter.off(event, handler);
|
||||
return this;
|
||||
}
|
||||
@ -93,7 +93,7 @@ export class EventEmitter implements CommonEventEmitter {
|
||||
* Remove an event listener.
|
||||
* @deprecated please use {@link EventEmitter.off} instead.
|
||||
*/
|
||||
removeListener(event: EventType, handler: Handler<any>): EventEmitter {
|
||||
removeListener(event: EventType, handler: Handler<any>): this {
|
||||
this.off(event, handler);
|
||||
return this;
|
||||
}
|
||||
@ -102,7 +102,7 @@ export class EventEmitter implements CommonEventEmitter {
|
||||
* Add an event listener.
|
||||
* @deprecated please use {@link EventEmitter.on} instead.
|
||||
*/
|
||||
addListener(event: EventType, handler: Handler<any>): EventEmitter {
|
||||
addListener(event: EventType, handler: Handler<any>): this {
|
||||
this.on(event, handler);
|
||||
return this;
|
||||
}
|
||||
@ -125,7 +125,7 @@ export class EventEmitter implements CommonEventEmitter {
|
||||
* @param handler - the handler function to run when the event occurs
|
||||
* @returns `this` to enable you to chain method calls.
|
||||
*/
|
||||
once(event: EventType, handler: Handler<any>): EventEmitter {
|
||||
once(event: EventType, handler: Handler<any>): this {
|
||||
const onceHandler: Handler<any> = eventData => {
|
||||
handler(eventData);
|
||||
this.off(event, onceHandler);
|
||||
@ -150,7 +150,7 @@ export class EventEmitter implements CommonEventEmitter {
|
||||
* @param event - the event to remove listeners for.
|
||||
* @returns `this` to enable you to chain method calls.
|
||||
*/
|
||||
removeAllListeners(event?: EventType): EventEmitter {
|
||||
removeAllListeners(event?: EventType): this {
|
||||
if (event) {
|
||||
this.eventsMap.delete(event);
|
||||
} else {
|
||||
|
@ -20,7 +20,7 @@ import type {ProtocolMapping} from 'devtools-protocol/types/protocol-mapping.js'
|
||||
|
||||
import {CDPSession, Connection as CDPPPtrConnection} from '../Connection.js';
|
||||
import {TargetCloseError} from '../Errors.js';
|
||||
import {Handler} from '../EventEmitter.js';
|
||||
import {EventEmitter, Handler} from '../EventEmitter.js';
|
||||
|
||||
import {Connection as BidiPPtrConnection} from './Connection.js';
|
||||
|
||||
@ -107,7 +107,7 @@ class CDPConnectionAdapter {
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class CDPClientAdapter<T extends Pick<CDPPPtrConnection, 'send' | 'on' | 'off'>>
|
||||
class CDPClientAdapter<T extends EventEmitter & Pick<CDPPPtrConnection, 'send'>>
|
||||
extends BidiMapper.EventEmitter<CdpEvents>
|
||||
implements BidiMapper.CdpClient
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user