2022-09-19 10:49:13 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2022 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-09-28 13:23:37 +00:00
|
|
|
import mitt, {
|
|
|
|
Emitter,
|
|
|
|
EventType,
|
2022-11-29 19:50:58 +00:00
|
|
|
EventHandlerMap,
|
2022-09-28 13:23:37 +00:00
|
|
|
Handler,
|
|
|
|
} from '../../third_party/mitt/index.js';
|
2020-06-15 10:52:19 +00:00
|
|
|
|
2021-04-12 13:57:05 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2022-06-22 13:25:44 +00:00
|
|
|
export {EventType, Handler};
|
2021-04-12 13:57:05 +00:00
|
|
|
|
2020-06-15 10:52:19 +00:00
|
|
|
/**
|
2021-04-06 08:58:01 +00:00
|
|
|
* @public
|
2020-06-15 10:52:19 +00:00
|
|
|
*/
|
|
|
|
export interface CommonEventEmitter {
|
|
|
|
on(event: EventType, handler: Handler): CommonEventEmitter;
|
|
|
|
off(event: EventType, handler: Handler): CommonEventEmitter;
|
|
|
|
/* 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;
|
2021-05-12 16:43:05 +00:00
|
|
|
emit(event: EventType, eventData?: unknown): boolean;
|
2020-06-15 10:52:19 +00:00
|
|
|
once(event: EventType, handler: Handler): CommonEventEmitter;
|
|
|
|
listenerCount(event: string): number;
|
|
|
|
|
|
|
|
removeAllListeners(event?: EventType): CommonEventEmitter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The EventEmitter class that many Puppeteer classes extend.
|
|
|
|
*
|
|
|
|
* @remarks
|
|
|
|
*
|
|
|
|
* This allows you to listen to events that Puppeteer classes fire and act
|
|
|
|
* accordingly. Therefore you'll mostly use {@link EventEmitter.on | on} and
|
|
|
|
* {@link EventEmitter.off | off} to bind
|
|
|
|
* and unbind to event listeners.
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export class EventEmitter implements CommonEventEmitter {
|
2022-11-29 19:50:58 +00:00
|
|
|
private emitter: Emitter<Record<string | symbol, any>>;
|
|
|
|
private eventsMap: EventHandlerMap<Record<string | symbol, any>> = new Map();
|
2020-06-15 10:52:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
this.emitter = mitt(this.eventsMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bind an event listener to fire when an event occurs.
|
|
|
|
* @param event - the event type you'd like to listen to. Can be a string or symbol.
|
2022-08-12 12:15:26 +00:00
|
|
|
* @param handler - the function to be called when the event occurs.
|
2021-05-25 06:47:25 +00:00
|
|
|
* @returns `this` to enable you to chain method calls.
|
2020-06-15 10:52:19 +00:00
|
|
|
*/
|
2022-11-29 19:50:58 +00:00
|
|
|
on(event: EventType, handler: Handler<any>): EventEmitter {
|
2020-06-15 10:52:19 +00:00
|
|
|
this.emitter.on(event, handler);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an event listener from firing.
|
|
|
|
* @param event - the event type you'd like to stop listening to.
|
2022-08-12 12:15:26 +00:00
|
|
|
* @param handler - the function that should be removed.
|
2021-05-25 06:47:25 +00:00
|
|
|
* @returns `this` to enable you to chain method calls.
|
2020-06-15 10:52:19 +00:00
|
|
|
*/
|
2022-11-29 19:50:58 +00:00
|
|
|
off(event: EventType, handler: Handler<any>): EventEmitter {
|
2020-06-15 10:52:19 +00:00
|
|
|
this.emitter.off(event, handler);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an event listener.
|
2021-05-26 14:37:38 +00:00
|
|
|
* @deprecated please use {@link EventEmitter.off} instead.
|
2020-06-15 10:52:19 +00:00
|
|
|
*/
|
2022-11-29 19:50:58 +00:00
|
|
|
removeListener(event: EventType, handler: Handler<any>): EventEmitter {
|
2020-06-15 10:52:19 +00:00
|
|
|
this.off(event, handler);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an event listener.
|
2021-05-26 14:37:38 +00:00
|
|
|
* @deprecated please use {@link EventEmitter.on} instead.
|
2020-06-15 10:52:19 +00:00
|
|
|
*/
|
2022-11-29 19:50:58 +00:00
|
|
|
addListener(event: EventType, handler: Handler<any>): EventEmitter {
|
2020-06-15 10:52:19 +00:00
|
|
|
this.on(event, handler);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Emit an event and call any associated listeners.
|
|
|
|
*
|
|
|
|
* @param event - the event you'd like to emit
|
|
|
|
* @param eventData - any data you'd like to emit with the event
|
|
|
|
* @returns `true` if there are any listeners, `false` if there are not.
|
|
|
|
*/
|
2021-05-12 16:43:05 +00:00
|
|
|
emit(event: EventType, eventData?: unknown): boolean {
|
2020-06-15 10:52:19 +00:00
|
|
|
this.emitter.emit(event, eventData);
|
|
|
|
return this.eventListenersCount(event) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Like `on` but the listener will only be fired once and then it will be removed.
|
|
|
|
* @param event - the event you'd like to listen to
|
|
|
|
* @param handler - the handler function to run when the event occurs
|
2021-05-25 06:47:25 +00:00
|
|
|
* @returns `this` to enable you to chain method calls.
|
2020-06-15 10:52:19 +00:00
|
|
|
*/
|
2022-11-29 19:50:58 +00:00
|
|
|
once(event: EventType, handler: Handler<any>): EventEmitter {
|
|
|
|
const onceHandler: Handler<any> = eventData => {
|
2020-06-15 10:52:19 +00:00
|
|
|
handler(eventData);
|
|
|
|
this.off(event, onceHandler);
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.on(event, onceHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the number of listeners for a given event.
|
|
|
|
*
|
|
|
|
* @param event - the event to get the listener count for
|
|
|
|
* @returns the number of listeners bound to the given event
|
|
|
|
*/
|
|
|
|
listenerCount(event: EventType): number {
|
|
|
|
return this.eventListenersCount(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all listeners. If given an event argument, it will remove only
|
|
|
|
* listeners for that event.
|
|
|
|
* @param event - the event to remove listeners for.
|
2021-05-25 06:47:25 +00:00
|
|
|
* @returns `this` to enable you to chain method calls.
|
2020-06-15 10:52:19 +00:00
|
|
|
*/
|
|
|
|
removeAllListeners(event?: EventType): EventEmitter {
|
|
|
|
if (event) {
|
|
|
|
this.eventsMap.delete(event);
|
|
|
|
} else {
|
|
|
|
this.eventsMap.clear();
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private eventListenersCount(event: EventType): number {
|
2022-01-31 15:16:32 +00:00
|
|
|
return this.eventsMap.get(event)?.length || 0;
|
2020-06-15 10:52:19 +00:00
|
|
|
}
|
|
|
|
}
|