export type EventType = string | symbol; export type Handler = (event: T) => void; export type WildcardHandler> = (type: keyof T, event: T[keyof T]) => void; export type EventHandlerList = Array>; export type WildCardEventHandlerList> = Array>; export type EventHandlerMap> = Map | WildCardEventHandlerList>; export interface Emitter> { all: EventHandlerMap; on(type: Key, handler: Handler): void; on(type: "*", handler: WildcardHandler): void; off(type: Key, handler?: Handler): void; off(type: "*", handler: WildcardHandler): void; emit(type: Key, event: Events[Key]): void; emit(type: undefined extends Events[Key] ? Key : never): void; } /** * EventEmitter: A class-based event emitter for inheritance */ export default class EventEmitter> implements Emitter { all: EventHandlerMap; /** * Register an event handler for the given type. * @param {string|symbol} type Type of event to listen for, or `'*'` for all events * @param {Function} handler Function to call in response to given event * @returns {Function} A function to unregister the handler */ on(type: Key, handler: Handler): () => void; on(type: "*", handler: WildcardHandler): () => void; /** * Register a one-time event handler for the given type. * @param {string|symbol} type Type of event to listen for * @param {Function} handler Function to call in response to given event * @returns {Function} A function to unregister the handler */ once(type: Key, handler: Handler): () => void; /** * Remove an event handler for the given type. * If `handler` is omitted, all handlers of the given type are removed. * @param {string|symbol} type Type of event to unregister `handler` from (`'*'` to remove a wildcard handler) * @param {Function} [handler] Handler function to remove */ off(type: Key, handler?: Handler): void; off(type: "*", handler: WildcardHandler): void; /** * Invoke all handlers for the given type. * If present, `'*'` handlers are invoked after type-matched handlers. * * Note: Manually firing '*' handlers is not supported. * * @param {string|symbol} type The event type to invoke * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler */ emit(type: Key, event: Events[Key]): void; emit(type: undefined extends Events[Key] ? Key : never): void; }