/** * @packageDocumentation * * Adds types to the EventTarget class. * * Hopefully this won't be necessary * forever: * * - https://github.com/microsoft/TypeScript/issues/28357 * - https://github.com/microsoft/TypeScript/issues/43477 * - https://github.com/microsoft/TypeScript/issues/299 * - https://www.npmjs.com/package/typed-events * - https://www.npmjs.com/package/typed-event-emitter * - https://www.npmjs.com/package/typed-event-target * - etc * * In addition to types, a `safeDispatchEvent` method is available which * prevents dispatching events that aren't in the event map, and a * `listenerCount` method which reports the number of listeners that are * currently registered for a given event. * * @example * * ```ts * import { TypedEventEmitter } from 'main-event' * import type { TypedEventTarget } from 'main-event' * * interface EventTypes { * 'test': CustomEvent * } * * const target = new TypedEventEmitter() * * // it's a regular EventTarget * console.info(target instanceof EventTarget) // true * * // register listeners normally * target.addEventListener('test', (evt) => { * // evt is CustomEvent * }) * * // @ts-expect-error 'derp' is not in the event map * target.addEventListener('derp', () => {}) * * // use normal dispatchEvent method * target.dispatchEvent(new CustomEvent('test', { * detail: 'hello' * })) * * // use type safe dispatch method * target.safeDispatchEvent('test', { * detail: 'world' * }) * * // report listener count * console.info(target.listenerCount('test')) // 0 * * // event emitters can be used purely as interfaces too * function acceptTarget (target: TypedEventTarget) { * // ... * } * ``` */ import { setMaxListeners } from './events.ts' export interface EventCallback { (evt: EventType): void } export interface EventObject { handleEvent: EventCallback } export type EventHandler = EventCallback | EventObject interface Listener { /** * The original listener */ callback: any /** * The function actually registered with the native EventTarget. `callback` is * never registered directly, so this is what has to be passed to * `super.removeEventListener` for the listener to be detached. */ wrapper: EventCallback /** * Event listener options normalized according to https://dom.spec.whatwg.org/#event-flatten-more */ options: AddEventListenerOptions /** * Callback function added to any passed signal */ onAbort(): void } /** * An event target with a typed map of supported events */ export interface TypedEventTarget > extends EventTarget { /** * Add a listener for an event */ addEventListener(type: K, listener: EventHandler | null, options?: boolean | AddEventListenerOptions): void /** * Returns the number of listeners for the event type */ listenerCount (type: string): number /** * Remove a listener previously registered for an event */ removeEventListener(type: K, listener?: EventHandler | null, options?: boolean | EventListenerOptions): void removeEventListener (type: string, listener?: EventHandler, options?: boolean | EventListenerOptions): void /** * Type-safe version of `EventTarget.dispatchEvent` - only events from the event map are supported. * * Attempting to dispatch an unsupported event results in a TypeScript compilation error. */ safeDispatchEvent(type: keyof EventMap, detail?: CustomEventInit): boolean } function isEventObject (obj?: any): obj is EventObject { return typeof obj?.handleEvent === 'function' } /** * Returns true if the passed argument is a boolean value */ function isBoolean (obj?: any): obj is boolean { return obj === true || obj === false } /** * @see https://dom.spec.whatwg.org/#concept-flatten-options */ function flattenOptions (options?: any): boolean { if (isBoolean(options)) { return options } return options?.capture ?? false } /** * @see https://dom.spec.whatwg.org/#event-flatten-more */ function flattenMoreOptions (options?: any): AddEventListenerOptions { const opts: AddEventListenerOptions = { capture: flattenOptions(options), once: Boolean(options?.once) } if (options?.passive != null) { opts.passive = options?.passive } if (options?.signal != null) { opts.signal = options?.signal } return opts } /** * Returns true if the capture/useCapture arg of each opts are equal */ function captureIsEqual (optsA?: boolean | AddEventListenerOptions, optsB?: boolean | EventListenerOptions): boolean { return flattenMoreOptions(optsA).capture === flattenMoreOptions(optsB).capture } /** * An implementation of a typed event target */ export class TypedEventEmitter> extends EventTarget implements TypedEventTarget { readonly #listeners = new Map() constructor () { super() // silence MaxListenersExceededWarning warning on Node.js, this is a red // herring almost all of the time setMaxListeners(Infinity, this) } listenerCount (type: string): number { const listeners = this.#listeners.get(type) if (listeners == null) { return 0 } return listeners.length } addEventListener(type: K, listener: EventHandler | null, options?: boolean | AddEventListenerOptions): void addEventListener (type: string, listener: EventHandler, opts?: boolean | AddEventListenerOptions): void { const options = flattenMoreOptions(opts) if (options?.signal?.aborted === true) { return } // the wrapper - and not `listener` - is what is registered with the native // EventTarget, so a reference to it must be kept in order to be able to // remove it again later const wrapper = (evt: Event): void => { if (options.once) { this.removeEventListener(type, listener, options) } if (isEventObject(listener)) { listener.handleEvent(evt) } else { listener(evt) } } let list = this.#listeners.get(type) if (list == null) { list = [] this.#listeners.set(type, list) } // if there is already an entry in the list for the same type, callback and // capture value, listening again is a no-op // @see https://dom.spec.whatwg.org/#add-an-event-listener const alreadyListening = list.some(entry => { if (entry.callback !== listener) { return false } // listener is already present, check capture options return captureIsEqual(entry.options, options) }) if (alreadyListening) { return } super.addEventListener(type, wrapper, options) const onAbort = (): void => { this.removeEventListener(type, listener, options) } options.signal?.addEventListener('abort', onAbort) list.push({ callback: listener, wrapper, options, onAbort }) } removeEventListener(type: K, listener?: EventHandler | null, options?: boolean | EventListenerOptions): void removeEventListener (type: string, listener?: EventHandler, options?: boolean | EventListenerOptions): void { const list = this.#listeners.get(type) if (list == null) { super.removeEventListener(type, listener ?? null, options) return } this.#listeners.set(type, list.filter(entry => { // listeners are the same so we need to check the capture argument if (entry.callback === listener && captureIsEqual(entry.options, options)) { super.removeEventListener(type, entry.wrapper, options) // remove abort signal abort event listener if set entry.options?.signal?.removeEventListener('abort', entry.onAbort) return false } return true })) } safeDispatchEvent(type: keyof EventMap, detail: CustomEventInit = {}): boolean { return this.dispatchEvent(new CustomEvent(type as string, detail)) } } export { setMaxListeners }