diff --git a/docs/api/puppeteer.eventemitter.addlistener.md b/docs/api/puppeteer.eventemitter.addlistener.md index 82941fd7..c139bd20 100644 --- a/docs/api/puppeteer.eventemitter.addlistener.md +++ b/docs/api/puppeteer.eventemitter.addlistener.md @@ -14,16 +14,16 @@ Add an event listener. ```typescript class EventEmitter { - addListener(event: EventType, handler: Handler): EventEmitter; + addListener(event: EventType, handler: Handler): EventEmitter; } ``` ## Parameters -| Parameter | Type | Description | -| --------- | ------------------------------------- | ----------- | -| event | [EventType](./puppeteer.eventtype.md) | | -| handler | [Handler](./puppeteer.handler.md) | | +| Parameter | Type | Description | +| --------- | -------------------------------------------- | ----------- | +| event | [EventType](./puppeteer.eventtype.md) | | +| handler | [Handler](./puppeteer.handler.md)<any> | | **Returns:** diff --git a/docs/api/puppeteer.eventemitter.off.md b/docs/api/puppeteer.eventemitter.off.md index 2969950b..98f6eac8 100644 --- a/docs/api/puppeteer.eventemitter.off.md +++ b/docs/api/puppeteer.eventemitter.off.md @@ -10,16 +10,16 @@ Remove an event listener from firing. ```typescript class EventEmitter { - off(event: EventType, handler: Handler): EventEmitter; + off(event: EventType, handler: Handler): EventEmitter; } ``` ## Parameters -| Parameter | Type | Description | -| --------- | ------------------------------------- | ----------------------------------------------- | -| event | [EventType](./puppeteer.eventtype.md) | the event type you'd like to stop listening to. | -| handler | [Handler](./puppeteer.handler.md) | the function that should be removed. | +| Parameter | Type | Description | +| --------- | -------------------------------------------- | ----------------------------------------------- | +| event | [EventType](./puppeteer.eventtype.md) | the event type you'd like to stop listening to. | +| handler | [Handler](./puppeteer.handler.md)<any> | the function that should be removed. | **Returns:** diff --git a/docs/api/puppeteer.eventemitter.on.md b/docs/api/puppeteer.eventemitter.on.md index b6972be2..e1fee2a2 100644 --- a/docs/api/puppeteer.eventemitter.on.md +++ b/docs/api/puppeteer.eventemitter.on.md @@ -10,16 +10,16 @@ Bind an event listener to fire when an event occurs. ```typescript class EventEmitter { - on(event: EventType, handler: Handler): EventEmitter; + on(event: EventType, handler: Handler): EventEmitter; } ``` ## Parameters -| Parameter | Type | Description | -| --------- | ------------------------------------- | ------------------------------------------------------------------ | -| event | [EventType](./puppeteer.eventtype.md) | the event type you'd like to listen to. Can be a string or symbol. | -| handler | [Handler](./puppeteer.handler.md) | the function to be called when the event occurs. | +| Parameter | Type | Description | +| --------- | -------------------------------------------- | ------------------------------------------------------------------ | +| event | [EventType](./puppeteer.eventtype.md) | the event type you'd like to listen to. Can be a string or symbol. | +| handler | [Handler](./puppeteer.handler.md)<any> | the function to be called when the event occurs. | **Returns:** diff --git a/docs/api/puppeteer.eventemitter.once.md b/docs/api/puppeteer.eventemitter.once.md index 9a04f288..94054eb2 100644 --- a/docs/api/puppeteer.eventemitter.once.md +++ b/docs/api/puppeteer.eventemitter.once.md @@ -10,16 +10,16 @@ Like `on` but the listener will only be fired once and then it will be removed. ```typescript class EventEmitter { - once(event: EventType, handler: Handler): EventEmitter; + once(event: EventType, handler: Handler): EventEmitter; } ``` ## Parameters -| Parameter | Type | Description | -| --------- | ------------------------------------- | ------------------------------------------------- | -| event | [EventType](./puppeteer.eventtype.md) | the event you'd like to listen to | -| handler | [Handler](./puppeteer.handler.md) | the handler function to run when the event occurs | +| Parameter | Type | Description | +| --------- | -------------------------------------------- | ------------------------------------------------- | +| event | [EventType](./puppeteer.eventtype.md) | the event you'd like to listen to | +| handler | [Handler](./puppeteer.handler.md)<any> | the handler function to run when the event occurs | **Returns:** diff --git a/docs/api/puppeteer.eventemitter.removelistener.md b/docs/api/puppeteer.eventemitter.removelistener.md index a2274e3f..76e25571 100644 --- a/docs/api/puppeteer.eventemitter.removelistener.md +++ b/docs/api/puppeteer.eventemitter.removelistener.md @@ -14,16 +14,16 @@ Remove an event listener. ```typescript class EventEmitter { - removeListener(event: EventType, handler: Handler): EventEmitter; + removeListener(event: EventType, handler: Handler): EventEmitter; } ``` ## Parameters -| Parameter | Type | Description | -| --------- | ------------------------------------- | ----------- | -| event | [EventType](./puppeteer.eventtype.md) | | -| handler | [Handler](./puppeteer.handler.md) | | +| Parameter | Type | Description | +| --------- | -------------------------------------------- | ----------- | +| event | [EventType](./puppeteer.eventtype.md) | | +| handler | [Handler](./puppeteer.handler.md)<any> | | **Returns:** diff --git a/docs/api/puppeteer.handler.md b/docs/api/puppeteer.handler.md index 56187524..405c4aaa 100644 --- a/docs/api/puppeteer.handler.md +++ b/docs/api/puppeteer.handler.md @@ -7,5 +7,5 @@ sidebar_label: Handler #### Signature: ```typescript -declare type Handler = (event?: T) => void; +declare type Handler = (event: T) => void; ``` diff --git a/package-lock.json b/package-lock.json index d9704db2..cada0861 100644 --- a/package-lock.json +++ b/package-lock.json @@ -58,7 +58,7 @@ "jpeg-js": "0.4.4", "mime": "3.0.0", "minimist": "1.2.6", - "mitt": "2.1.0", + "mitt": "3.0.0", "mocha": "10.0.0", "ncp": "2.0.0", "npm-run-all": "4.1.5", @@ -6217,9 +6217,9 @@ } }, "node_modules/mitt": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/mitt/-/mitt-2.1.0.tgz", - "integrity": "sha512-ILj2TpLiysu2wkBbWjAmww7TkZb65aiQO+DkVdUTBpBXq+MHYiETENkKFMtsJZX1Lf4pe4QOrTSjIfUwN5lRdg==", + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/mitt/-/mitt-3.0.0.tgz", + "integrity": "sha512-7dX2/10ITVyqh4aOSVI9gdape+t9l2/8QxHrFmUXu4EEUpdlxl6RudZUPZoc+zuY2hk1j7XxVroIVIan/pD/SQ==", "dev": true }, "node_modules/mkdirp-classic": { @@ -13200,9 +13200,9 @@ } }, "mitt": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/mitt/-/mitt-2.1.0.tgz", - "integrity": "sha512-ILj2TpLiysu2wkBbWjAmww7TkZb65aiQO+DkVdUTBpBXq+MHYiETENkKFMtsJZX1Lf4pe4QOrTSjIfUwN5lRdg==", + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/mitt/-/mitt-3.0.0.tgz", + "integrity": "sha512-7dX2/10ITVyqh4aOSVI9gdape+t9l2/8QxHrFmUXu4EEUpdlxl6RudZUPZoc+zuY2hk1j7XxVroIVIan/pD/SQ==", "dev": true }, "mkdirp-classic": { diff --git a/package.json b/package.json index 6911d263..21a9077c 100644 --- a/package.json +++ b/package.json @@ -81,7 +81,7 @@ "jpeg-js": "0.4.4", "mime": "3.0.0", "minimist": "1.2.6", - "mitt": "2.1.0", + "mitt": "3.0.0", "mocha": "10.0.0", "ncp": "2.0.0", "npm-run-all": "4.1.5", diff --git a/packages/puppeteer-core/src/api/Page.ts b/packages/puppeteer-core/src/api/Page.ts index 7f33695c..0e42e50e 100644 --- a/packages/puppeteer-core/src/api/Page.ts +++ b/packages/puppeteer-core/src/api/Page.ts @@ -425,7 +425,7 @@ export interface PageEventObject { * @public */ export class Page extends EventEmitter { - #handlerMap = new WeakMap(); + #handlerMap = new WeakMap, Handler>(); /** * @internal diff --git a/packages/puppeteer-core/src/common/EventEmitter.ts b/packages/puppeteer-core/src/common/EventEmitter.ts index 7fa77ab4..63dc0b0c 100644 --- a/packages/puppeteer-core/src/common/EventEmitter.ts +++ b/packages/puppeteer-core/src/common/EventEmitter.ts @@ -17,6 +17,7 @@ import mitt, { Emitter, EventType, + EventHandlerMap, Handler, } from '../../third_party/mitt/index.js'; @@ -57,8 +58,8 @@ export interface CommonEventEmitter { * @public */ export class EventEmitter implements CommonEventEmitter { - private emitter: Emitter; - private eventsMap = new Map(); + private emitter: Emitter>; + private eventsMap: EventHandlerMap> = new Map(); /** * @internal @@ -73,7 +74,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): EventEmitter { + on(event: EventType, handler: Handler): EventEmitter { this.emitter.on(event, handler); return this; } @@ -84,7 +85,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): EventEmitter { + off(event: EventType, handler: Handler): EventEmitter { this.emitter.off(event, handler); return this; } @@ -93,7 +94,7 @@ export class EventEmitter implements CommonEventEmitter { * Remove an event listener. * @deprecated please use {@link EventEmitter.off} instead. */ - removeListener(event: EventType, handler: Handler): EventEmitter { + removeListener(event: EventType, handler: Handler): EventEmitter { this.off(event, handler); return this; } @@ -102,7 +103,7 @@ export class EventEmitter implements CommonEventEmitter { * Add an event listener. * @deprecated please use {@link EventEmitter.on} instead. */ - addListener(event: EventType, handler: Handler): EventEmitter { + addListener(event: EventType, handler: Handler): EventEmitter { this.on(event, handler); return this; } @@ -125,8 +126,8 @@ 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): EventEmitter { - const onceHandler: Handler = eventData => { + once(event: EventType, handler: Handler): EventEmitter { + const onceHandler: Handler = eventData => { handler(eventData); this.off(event, onceHandler); }; diff --git a/test/src/CDPSession.spec.ts b/test/src/CDPSession.spec.ts index 51d85b3c..aaafb936 100644 --- a/test/src/CDPSession.spec.ts +++ b/test/src/CDPSession.spec.ts @@ -64,7 +64,10 @@ describe('Target.createCDPSession', function () { client.on('Network.requestWillBeSent', event => { return events.push(event); }); - await page.goto(server.EMPTY_PAGE); + await Promise.all([ + waitEvent(client, 'Network.requestWillBeSent'), + page.goto(server.EMPTY_PAGE), + ]); expect(events.length).toBe(1); }); it('should enable and disable domains independently', async () => {