You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.9 KiB

2 years ago
/**
* 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
2 years ago
export type Handler<T = unknown> = (event: T) => void
export type WildcardHandler<T = Record<string, unknown>> = (type: keyof T, event: T[keyof T]) => void
2 years ago
// An array of all currently registered event handlers for a type
2 years ago
export type EventHandlerList<T = unknown> = Array<Handler<T>>
export type WildCardEventHandlerList<T = Record<string, unknown>> = Array<WildcardHandler<T>>
2 years ago
// A map of event types and their corresponding event handlers.
2 years ago
export type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<
keyof Events | '*',
EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>
>
2 years ago
2 years ago
export interface Emitter<Events extends Record<EventType, unknown>> {
all: EventHandlerMap<Events>
2 years ago
2 years ago
on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void
on(type: '*', handler: WildcardHandler<Events>): void
2 years ago
2 years ago
off<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>): void
off(type: '*', handler: WildcardHandler<Events>): void
2 years ago
2 years ago
emit<Key extends keyof Events>(type: Key, event: Events[Key]): void
emit<Key extends keyof Events>(type: undefined extends Events[Key] ? Key : never): void
2 years ago
clear(): void
}
/**
* Mitt: Tiny (~200b) functional event emitter / pubsub.
* @name mitt
* @returns {Mitt}
*/
2 years ago
export function mitt<Events extends Record<EventType, unknown>>(all?: EventHandlerMap<Events>): Emitter<Events> {
type GenericEventHandler = Handler<Events[keyof Events]> | WildcardHandler<Events>
2 years ago
all = all || new Map()
return {
/**
* A Map of event names to registered handler functions.
*/
all,
/**
* Register an event handler for the given type.
2 years ago
* @param {string|symbol} type Type of event to listen for, or `'*'` for all events
2 years ago
* @param {Function} handler Function to call in response to given event
* @memberOf mitt
*/
2 years ago
on<Key extends keyof Events>(type: Key, handler: GenericEventHandler) {
const handlers: Array<GenericEventHandler> | undefined = all!.get(type)
if (handlers) {
handlers.push(handler)
} else {
all!.set(type, [handler] as EventHandlerList<Events[keyof Events]>)
2 years ago
}
},
/**
* Remove an event handler for the given type.
2 years ago
* 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
2 years ago
* @memberOf mitt
*/
2 years ago
off<Key extends keyof Events>(type: Key, handler?: GenericEventHandler) {
const handlers: Array<GenericEventHandler> | undefined = all!.get(type)
2 years ago
if (handlers) {
2 years ago
if (handler) {
handlers.splice(handlers.indexOf(handler) >>> 0, 1)
} else {
all!.set(type, [])
}
2 years ago
}
},
/**
* Invoke all handlers for the given type.
2 years ago
* If present, `'*'` handlers are invoked after type-matched handlers.
2 years ago
*
2 years ago
* Note: Manually firing '*' handlers is not supported.
2 years ago
*
* @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
*/
2 years ago
emit<Key extends keyof Events>(type: Key, evt?: Events[Key]) {
let handlers = all!.get(type)
if (handlers) {
;(handlers as EventHandlerList<Events[keyof Events]>).slice().forEach((handler) => {
handler(evt as Events[Key])
})
}
handlers = all!.get('*')
if (handlers) {
;(handlers as WildCardEventHandlerList<Events>).slice().forEach((handler) => {
handler(type, evt as Events[Key])
})
}
2 years ago
},
/**
* Clear all
*/
clear() {
this.all.clear()
}
}
}