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
|
```typescript
|
||||||
interface CommonEventEmitter {
|
interface CommonEventEmitter {
|
||||||
addListener(event: EventType, handler: Handler): CommonEventEmitter;
|
addListener(event: EventType, handler: Handler): this;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -21,4 +21,4 @@ interface CommonEventEmitter {
|
|||||||
|
|
||||||
**Returns:**
|
**Returns:**
|
||||||
|
|
||||||
[CommonEventEmitter](./puppeteer.commoneventemitter.md)
|
this
|
||||||
|
@ -8,7 +8,7 @@ sidebar_label: CommonEventEmitter.off
|
|||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
interface CommonEventEmitter {
|
interface CommonEventEmitter {
|
||||||
off(event: EventType, handler: Handler): CommonEventEmitter;
|
off(event: EventType, handler: Handler): this;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -21,4 +21,4 @@ interface CommonEventEmitter {
|
|||||||
|
|
||||||
**Returns:**
|
**Returns:**
|
||||||
|
|
||||||
[CommonEventEmitter](./puppeteer.commoneventemitter.md)
|
this
|
||||||
|
@ -8,7 +8,7 @@ sidebar_label: CommonEventEmitter.on
|
|||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
interface CommonEventEmitter {
|
interface CommonEventEmitter {
|
||||||
on(event: EventType, handler: Handler): CommonEventEmitter;
|
on(event: EventType, handler: Handler): this;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -21,4 +21,4 @@ interface CommonEventEmitter {
|
|||||||
|
|
||||||
**Returns:**
|
**Returns:**
|
||||||
|
|
||||||
[CommonEventEmitter](./puppeteer.commoneventemitter.md)
|
this
|
||||||
|
@ -8,7 +8,7 @@ sidebar_label: CommonEventEmitter.once
|
|||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
interface CommonEventEmitter {
|
interface CommonEventEmitter {
|
||||||
once(event: EventType, handler: Handler): CommonEventEmitter;
|
once(event: EventType, handler: Handler): this;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -21,4 +21,4 @@ interface CommonEventEmitter {
|
|||||||
|
|
||||||
**Returns:**
|
**Returns:**
|
||||||
|
|
||||||
[CommonEventEmitter](./puppeteer.commoneventemitter.md)
|
this
|
||||||
|
@ -8,7 +8,7 @@ sidebar_label: CommonEventEmitter.removeAllListeners
|
|||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
interface CommonEventEmitter {
|
interface CommonEventEmitter {
|
||||||
removeAllListeners(event?: EventType): CommonEventEmitter;
|
removeAllListeners(event?: EventType): this;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -20,4 +20,4 @@ interface CommonEventEmitter {
|
|||||||
|
|
||||||
**Returns:**
|
**Returns:**
|
||||||
|
|
||||||
[CommonEventEmitter](./puppeteer.commoneventemitter.md)
|
this
|
||||||
|
@ -8,7 +8,7 @@ sidebar_label: CommonEventEmitter.removeListener
|
|||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
interface CommonEventEmitter {
|
interface CommonEventEmitter {
|
||||||
removeListener(event: EventType, handler: Handler): CommonEventEmitter;
|
removeListener(event: EventType, handler: Handler): this;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -21,4 +21,4 @@ interface CommonEventEmitter {
|
|||||||
|
|
||||||
**Returns:**
|
**Returns:**
|
||||||
|
|
||||||
[CommonEventEmitter](./puppeteer.commoneventemitter.md)
|
this
|
||||||
|
@ -14,7 +14,7 @@ Add an event listener.
|
|||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
class EventEmitter {
|
class EventEmitter {
|
||||||
addListener(event: EventType, handler: Handler<any>): EventEmitter;
|
addListener(event: EventType, handler: Handler<any>): this;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -27,4 +27,4 @@ class EventEmitter {
|
|||||||
|
|
||||||
**Returns:**
|
**Returns:**
|
||||||
|
|
||||||
[EventEmitter](./puppeteer.eventemitter.md)
|
this
|
||||||
|
@ -10,7 +10,7 @@ Remove an event listener from firing.
|
|||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
class EventEmitter {
|
class EventEmitter {
|
||||||
off(event: EventType, handler: Handler<any>): EventEmitter;
|
off(event: EventType, handler: Handler<any>): this;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -23,6 +23,6 @@ class EventEmitter {
|
|||||||
|
|
||||||
**Returns:**
|
**Returns:**
|
||||||
|
|
||||||
[EventEmitter](./puppeteer.eventemitter.md)
|
this
|
||||||
|
|
||||||
`this` to enable you to chain method calls.
|
`this` to enable you to chain method calls.
|
||||||
|
@ -10,7 +10,7 @@ Bind an event listener to fire when an event occurs.
|
|||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
class EventEmitter {
|
class EventEmitter {
|
||||||
on(event: EventType, handler: Handler<any>): EventEmitter;
|
on(event: EventType, handler: Handler<any>): this;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -23,6 +23,6 @@ class EventEmitter {
|
|||||||
|
|
||||||
**Returns:**
|
**Returns:**
|
||||||
|
|
||||||
[EventEmitter](./puppeteer.eventemitter.md)
|
this
|
||||||
|
|
||||||
`this` to enable you to chain method calls.
|
`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
|
```typescript
|
||||||
class EventEmitter {
|
class EventEmitter {
|
||||||
once(event: EventType, handler: Handler<any>): EventEmitter;
|
once(event: EventType, handler: Handler<any>): this;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -23,6 +23,6 @@ class EventEmitter {
|
|||||||
|
|
||||||
**Returns:**
|
**Returns:**
|
||||||
|
|
||||||
[EventEmitter](./puppeteer.eventemitter.md)
|
this
|
||||||
|
|
||||||
`this` to enable you to chain method calls.
|
`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
|
```typescript
|
||||||
class EventEmitter {
|
class EventEmitter {
|
||||||
removeAllListeners(event?: EventType): EventEmitter;
|
removeAllListeners(event?: EventType): this;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -22,6 +22,6 @@ class EventEmitter {
|
|||||||
|
|
||||||
**Returns:**
|
**Returns:**
|
||||||
|
|
||||||
[EventEmitter](./puppeteer.eventemitter.md)
|
this
|
||||||
|
|
||||||
`this` to enable you to chain method calls.
|
`this` to enable you to chain method calls.
|
||||||
|
@ -14,7 +14,7 @@ Remove an event listener.
|
|||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
class EventEmitter {
|
class EventEmitter {
|
||||||
removeListener(event: EventType, handler: Handler<any>): EventEmitter;
|
removeListener(event: EventType, handler: Handler<any>): this;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -27,4 +27,4 @@ class EventEmitter {
|
|||||||
|
|
||||||
**Returns:**
|
**Returns:**
|
||||||
|
|
||||||
[EventEmitter](./puppeteer.eventemitter.md)
|
this
|
||||||
|
@ -11,7 +11,7 @@ class Page {
|
|||||||
off<K extends keyof PageEventObject>(
|
off<K extends keyof PageEventObject>(
|
||||||
eventName: K,
|
eventName: K,
|
||||||
handler: (event: PageEventObject[K]) => void
|
handler: (event: PageEventObject[K]) => void
|
||||||
): EventEmitter;
|
): this;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -24,4 +24,4 @@ class Page {
|
|||||||
|
|
||||||
**Returns:**
|
**Returns:**
|
||||||
|
|
||||||
[EventEmitter](./puppeteer.eventemitter.md)
|
this
|
||||||
|
@ -19,7 +19,7 @@ class Page {
|
|||||||
on<K extends keyof PageEventObject>(
|
on<K extends keyof PageEventObject>(
|
||||||
eventName: K,
|
eventName: K,
|
||||||
handler: (event: PageEventObject[K]) => void
|
handler: (event: PageEventObject[K]) => void
|
||||||
): EventEmitter;
|
): this;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -32,4 +32,4 @@ class Page {
|
|||||||
|
|
||||||
**Returns:**
|
**Returns:**
|
||||||
|
|
||||||
[EventEmitter](./puppeteer.eventemitter.md)
|
this
|
||||||
|
@ -11,7 +11,7 @@ class Page {
|
|||||||
once<K extends keyof PageEventObject>(
|
once<K extends keyof PageEventObject>(
|
||||||
eventName: K,
|
eventName: K,
|
||||||
handler: (event: PageEventObject[K]) => void
|
handler: (event: PageEventObject[K]) => void
|
||||||
): EventEmitter;
|
): this;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -24,4 +24,4 @@ class Page {
|
|||||||
|
|
||||||
**Returns:**
|
**Returns:**
|
||||||
|
|
||||||
[EventEmitter](./puppeteer.eventemitter.md)
|
this
|
||||||
|
@ -109,22 +109,22 @@ export class Locator extends EventEmitter {
|
|||||||
override on<K extends keyof LocatorEventObject>(
|
override on<K extends keyof LocatorEventObject>(
|
||||||
eventName: K,
|
eventName: K,
|
||||||
handler: (event: LocatorEventObject[K]) => void
|
handler: (event: LocatorEventObject[K]) => void
|
||||||
): Locator {
|
): this {
|
||||||
return super.on(eventName, handler) as Locator;
|
return super.on(eventName, handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
override once<K extends keyof LocatorEventObject>(
|
override once<K extends keyof LocatorEventObject>(
|
||||||
eventName: K,
|
eventName: K,
|
||||||
handler: (event: LocatorEventObject[K]) => void
|
handler: (event: LocatorEventObject[K]) => void
|
||||||
): Locator {
|
): this {
|
||||||
return super.once(eventName, handler) as Locator;
|
return super.once(eventName, handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
override off<K extends keyof LocatorEventObject>(
|
override off<K extends keyof LocatorEventObject>(
|
||||||
eventName: K,
|
eventName: K,
|
||||||
handler: (event: LocatorEventObject[K]) => void
|
handler: (event: LocatorEventObject[K]) => void
|
||||||
): Locator {
|
): this {
|
||||||
return super.off(eventName, handler) as Locator;
|
return super.off(eventName, handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -480,7 +480,7 @@ export class Page extends EventEmitter {
|
|||||||
override on<K extends keyof PageEventObject>(
|
override on<K extends keyof PageEventObject>(
|
||||||
eventName: K,
|
eventName: K,
|
||||||
handler: (event: PageEventObject[K]) => void
|
handler: (event: PageEventObject[K]) => void
|
||||||
): EventEmitter {
|
): this {
|
||||||
if (eventName === 'request') {
|
if (eventName === 'request') {
|
||||||
const wrap =
|
const wrap =
|
||||||
this.#handlerMap.get(handler) ||
|
this.#handlerMap.get(handler) ||
|
||||||
@ -500,7 +500,7 @@ export class Page extends EventEmitter {
|
|||||||
override once<K extends keyof PageEventObject>(
|
override once<K extends keyof PageEventObject>(
|
||||||
eventName: K,
|
eventName: K,
|
||||||
handler: (event: PageEventObject[K]) => void
|
handler: (event: PageEventObject[K]) => void
|
||||||
): EventEmitter {
|
): this {
|
||||||
// Note: this method only exists to define the types; we delegate the impl
|
// Note: this method only exists to define the types; we delegate the impl
|
||||||
// to EventEmitter.
|
// to EventEmitter.
|
||||||
return super.once(eventName, handler);
|
return super.once(eventName, handler);
|
||||||
@ -509,7 +509,7 @@ export class Page extends EventEmitter {
|
|||||||
override off<K extends keyof PageEventObject>(
|
override off<K extends keyof PageEventObject>(
|
||||||
eventName: K,
|
eventName: K,
|
||||||
handler: (event: PageEventObject[K]) => void
|
handler: (event: PageEventObject[K]) => void
|
||||||
): EventEmitter {
|
): this {
|
||||||
if (eventName === 'request') {
|
if (eventName === 'request') {
|
||||||
handler = this.#handlerMap.get(handler) || handler;
|
handler = this.#handlerMap.get(handler) || handler;
|
||||||
}
|
}
|
||||||
|
@ -29,19 +29,19 @@ export type Handler<T = unknown> = (event: T) => void;
|
|||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
export interface CommonEventEmitter {
|
export interface CommonEventEmitter {
|
||||||
on(event: EventType, handler: Handler): CommonEventEmitter;
|
on(event: EventType, handler: Handler): this;
|
||||||
off(event: EventType, handler: Handler): CommonEventEmitter;
|
off(event: EventType, handler: Handler): this;
|
||||||
/* To maintain parity with the built in NodeJS event emitter which uses removeListener
|
/* To maintain parity with the built in NodeJS event emitter which uses removeListener
|
||||||
* rather than `off`.
|
* rather than `off`.
|
||||||
* If you're implementing new code you should use `off`.
|
* If you're implementing new code you should use `off`.
|
||||||
*/
|
*/
|
||||||
addListener(event: EventType, handler: Handler): CommonEventEmitter;
|
addListener(event: EventType, handler: Handler): this;
|
||||||
removeListener(event: EventType, handler: Handler): CommonEventEmitter;
|
removeListener(event: EventType, handler: Handler): this;
|
||||||
emit(event: EventType, eventData?: unknown): boolean;
|
emit(event: EventType, eventData?: unknown): boolean;
|
||||||
once(event: EventType, handler: Handler): CommonEventEmitter;
|
once(event: EventType, handler: Handler): this;
|
||||||
listenerCount(event: string): number;
|
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.
|
* @param handler - the function to be called when the event occurs.
|
||||||
* @returns `this` to enable you to chain method calls.
|
* @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);
|
this.emitter.on(event, handler);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -84,7 +84,7 @@ export class EventEmitter implements CommonEventEmitter {
|
|||||||
* @param handler - the function that should be removed.
|
* @param handler - the function that should be removed.
|
||||||
* @returns `this` to enable you to chain method calls.
|
* @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);
|
this.emitter.off(event, handler);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -93,7 +93,7 @@ export class EventEmitter implements CommonEventEmitter {
|
|||||||
* Remove an event listener.
|
* Remove an event listener.
|
||||||
* @deprecated please use {@link EventEmitter.off} instead.
|
* @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);
|
this.off(event, handler);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -102,7 +102,7 @@ export class EventEmitter implements CommonEventEmitter {
|
|||||||
* Add an event listener.
|
* Add an event listener.
|
||||||
* @deprecated please use {@link EventEmitter.on} instead.
|
* @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);
|
this.on(event, handler);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -125,7 +125,7 @@ export class EventEmitter implements CommonEventEmitter {
|
|||||||
* @param handler - the handler function to run when the event occurs
|
* @param handler - the handler function to run when the event occurs
|
||||||
* @returns `this` to enable you to chain method calls.
|
* @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 => {
|
const onceHandler: Handler<any> = eventData => {
|
||||||
handler(eventData);
|
handler(eventData);
|
||||||
this.off(event, onceHandler);
|
this.off(event, onceHandler);
|
||||||
@ -150,7 +150,7 @@ export class EventEmitter implements CommonEventEmitter {
|
|||||||
* @param event - the event to remove listeners for.
|
* @param event - the event to remove listeners for.
|
||||||
* @returns `this` to enable you to chain method calls.
|
* @returns `this` to enable you to chain method calls.
|
||||||
*/
|
*/
|
||||||
removeAllListeners(event?: EventType): EventEmitter {
|
removeAllListeners(event?: EventType): this {
|
||||||
if (event) {
|
if (event) {
|
||||||
this.eventsMap.delete(event);
|
this.eventsMap.delete(event);
|
||||||
} else {
|
} else {
|
||||||
|
@ -20,7 +20,7 @@ import type {ProtocolMapping} from 'devtools-protocol/types/protocol-mapping.js'
|
|||||||
|
|
||||||
import {CDPSession, Connection as CDPPPtrConnection} from '../Connection.js';
|
import {CDPSession, Connection as CDPPPtrConnection} from '../Connection.js';
|
||||||
import {TargetCloseError} from '../Errors.js';
|
import {TargetCloseError} from '../Errors.js';
|
||||||
import {Handler} from '../EventEmitter.js';
|
import {EventEmitter, Handler} from '../EventEmitter.js';
|
||||||
|
|
||||||
import {Connection as BidiPPtrConnection} from './Connection.js';
|
import {Connection as BidiPPtrConnection} from './Connection.js';
|
||||||
|
|
||||||
@ -107,7 +107,7 @@ class CDPConnectionAdapter {
|
|||||||
*
|
*
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
class CDPClientAdapter<T extends Pick<CDPPPtrConnection, 'send' | 'on' | 'off'>>
|
class CDPClientAdapter<T extends EventEmitter & Pick<CDPPPtrConnection, 'send'>>
|
||||||
extends BidiMapper.EventEmitter<CdpEvents>
|
extends BidiMapper.EventEmitter<CdpEvents>
|
||||||
implements BidiMapper.CdpClient
|
implements BidiMapper.CdpClient
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user