Browse Source

fix: mitt

main
xingyu 2 years ago
parent
commit
ac4180d2ce
  1. 86
      src/utils/mitt.ts

86
src/utils/mitt.ts

@ -2,34 +2,32 @@
* copy to https://github.com/developit/mitt * copy to https://github.com/developit/mitt
* Expand clear method * Expand clear method
*/ */
export type EventType = string | symbol export type EventType = string | symbol
// An event handler can take an optional event argument // An event handler can take an optional event argument
// and should not return a value // and should not return a value
export type Handler<T = unknown> = (event: T) => void export type Handler<T = any> = (event?: T) => void
export type WildcardHandler<T = Record<string, unknown>> = (type: keyof T, event: T[keyof T]) => void export type WildcardHandler = (type: EventType, event?: any) => void
// An array of all currently registered event handlers for a type // An array of all currently registered event handlers for a type
export type EventHandlerList<T = unknown> = Array<Handler<T>> export type EventHandlerList = Array<Handler>
export type WildCardEventHandlerList<T = Record<string, unknown>> = Array<WildcardHandler<T>> export type WildCardEventHandlerList = Array<WildcardHandler>
// A map of event types and their corresponding event handlers. // A map of event types and their corresponding event handlers.
export type EventHandlerMap<Events extends Record<EventType, unknown>> = Map< export type EventHandlerMap = Map<EventType, EventHandlerList | WildCardEventHandlerList>
keyof Events | '*',
EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>
>
export interface Emitter<Events extends Record<EventType, unknown>> { export interface Emitter {
all: EventHandlerMap<Events> all: EventHandlerMap
on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void on<T = any>(type: EventType, handler: Handler<T>): void
on(type: '*', handler: WildcardHandler<Events>): void on(type: '*', handler: WildcardHandler): void
off<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>): void off<T = any>(type: EventType, handler: Handler<T>): void
off(type: '*', handler: WildcardHandler<Events>): void off(type: '*', handler: WildcardHandler): void
emit<Key extends keyof Events>(type: Key, event: Events[Key]): void emit<T = any>(type: EventType, event?: T): void
emit<Key extends keyof Events>(type: undefined extends Events[Key] ? Key : never): void emit(type: '*', event?: any): void
clear(): void clear(): void
} }
@ -38,8 +36,7 @@ export interface Emitter<Events extends Record<EventType, unknown>> {
* @name mitt * @name mitt
* @returns {Mitt} * @returns {Mitt}
*/ */
export function mitt<Events extends Record<EventType, unknown>>(all?: EventHandlerMap<Events>): Emitter<Events> { export default function mitt(all?: EventHandlerMap): Emitter {
type GenericEventHandler = Handler<Events[keyof Events]> | WildcardHandler<Events>
all = all || new Map() all = all || new Map()
return { return {
@ -50,61 +47,48 @@ export function mitt<Events extends Record<EventType, unknown>>(all?: EventHandl
/** /**
* Register an event handler for the given type. * Register an event handler for the given type.
* @param {string|symbol} type Type of event to listen for, or `'*'` for all events * @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 * @param {Function} handler Function to call in response to given event
* @memberOf mitt * @memberOf mitt
*/ */
on<Key extends keyof Events>(type: Key, handler: GenericEventHandler) { on<T = any>(type: EventType, handler: Handler<T>) {
const handlers: Array<GenericEventHandler> | undefined = all!.get(type) const handlers = all?.get(type)
if (handlers) { const added = handlers && handlers.push(handler)
handlers.push(handler) if (!added) {
} else { all?.set(type, [handler])
all!.set(type, [handler] as EventHandlerList<Events[keyof Events]>)
} }
}, },
/** /**
* Remove an event handler for the given type. * 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, or `"*"`
* @param {string|symbol} type Type of event to unregister `handler` from (`'*'` to remove a wildcard handler) * @param {Function} handler Handler function to remove
* @param {Function} [handler] Handler function to remove
* @memberOf mitt * @memberOf mitt
*/ */
off<Key extends keyof Events>(type: Key, handler?: GenericEventHandler) { off<T = any>(type: EventType, handler: Handler<T>) {
const handlers: Array<GenericEventHandler> | undefined = all!.get(type) const handlers = all?.get(type)
if (handlers) { if (handlers) {
if (handler) { handlers.splice(handlers.indexOf(handler) >>> 0, 1)
handlers.splice(handlers.indexOf(handler) >>> 0, 1)
} else {
all!.set(type, [])
}
} }
}, },
/** /**
* Invoke all handlers for the given type. * Invoke all handlers for the given type.
* If present, `'*'` handlers are invoked after type-matched handlers. * If present, `"*"` handlers are invoked after type-matched handlers.
* *
* Note: Manually firing '*' handlers is not supported. * Note: Manually firing "*" handlers is not supported.
* *
* @param {string|symbol} type The event type to invoke * @param {string|symbol} type The event type to invoke
* @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler
* @memberOf mitt * @memberOf mitt
*/ */
emit<Key extends keyof Events>(type: Key, evt?: Events[Key]) { emit<T = any>(type: EventType, evt: T) {
let handlers = all!.get(type) ;((all?.get(type) || []) as EventHandlerList).slice().map((handler) => {
if (handlers) { handler(evt)
;(handlers as EventHandlerList<Events[keyof Events]>).slice().forEach((handler) => { })
handler(evt as Events[Key]) ;((all?.get('*') || []) as WildCardEventHandlerList).slice().map((handler) => {
}) handler(type, evt)
} })
handlers = all!.get('*')
if (handlers) {
;(handlers as WildCardEventHandlerList<Events>).slice().forEach((handler) => {
handler(type, evt as Events[Key])
})
}
}, },
/** /**