/** * copy to https://github.com/developit/mitt * Expand clear method */ export type EventType = string | symbol // An event handler can take an optional event argument // and should not return a value export type Handler = (event: T) => void export type WildcardHandler> = ( type: keyof T, event: T[keyof T], ) => void // An array of all currently registered event handlers for a type export type EventHandlerList = Array> export type WildCardEventHandlerList> = Array> // A map of event types and their corresponding event handlers. export type EventHandlerMap> = Map< keyof Events | '*', EventHandlerList | 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 clear(): void } /** * Mitt: Tiny (~200b) functional event emitter / pubsub. * @name mitt * @returns {Mitt} mitt */ export function mitt>( all?: EventHandlerMap, ): Emitter { type GenericEventHandler = Handler | WildcardHandler all = all || new Map() return { /** * A Map of event names to registered handler functions. */ all, /** * 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 * @memberOf mitt */ on(type: Key, handler: GenericEventHandler) { const handlers: Array | undefined = all!.get(type) if (handlers) handlers.push(handler) else all!.set(type, [handler] as EventHandlerList) }, /** * 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 * @memberOf mitt */ off(type: Key, handler?: GenericEventHandler) { const handlers: Array | undefined = all!.get(type) if (handlers) { if (handler) handlers.splice(handlers.indexOf(handler) >>> 0, 1) else all!.set(type, []) } }, /** * 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 * @memberOf mitt */ emit(type: Key, evt?: Events[Key]) { let handlers = all!.get(type) if (handlers) { (handlers as EventHandlerList).slice().forEach((handler) => { handler(evt as Events[Key]) }) } handlers = all!.get('*') if (handlers) { (handlers as WildCardEventHandlerList).slice().forEach((handler) => { handler(type, evt as Events[Key]) }) } }, /** * Clear all */ clear() { this.all.clear() }, } }