8 changed files with 107 additions and 86 deletions
@ -1,73 +1,92 @@ |
|||||
/** |
/** |
||||
* Mitt: Tiny functional event emitter / pubsub |
* https://github.com/developit/mitt
|
||||
* |
|
||||
* @name mitt |
|
||||
* @param {Array} [all] Optional array of event names to registered handler functions |
|
||||
* @returns {Function} The function's instance |
|
||||
*/ |
*/ |
||||
export default class Mitt { |
|
||||
private cache: Map<string | Symbol, Array<(...data: any) => void>>; |
|
||||
constructor(all = []) { |
|
||||
// A Map of event names to registered handler functions.
|
|
||||
this.cache = new Map(all); |
|
||||
} |
|
||||
|
|
||||
once(type: string | Symbol, handler: Fn) { |
export type EventType = string | symbol; |
||||
const decor = (...args: any[]) => { |
|
||||
handler && handler.apply(this, args); |
// An event handler can take an optional event argument
|
||||
this.off(type, decor); |
// and should not return a value
|
||||
}; |
export type Handler<T = any> = (event?: T) => void; |
||||
this.on(type, decor); |
export type WildcardHandler = (type: EventType, event?: any) => void; |
||||
return this; |
|
||||
} |
// An array of all currently registered event handlers for a type
|
||||
|
export type EventHandlerList = Array<Handler>; |
||||
|
export type WildCardEventHandlerList = Array<WildcardHandler>; |
||||
|
|
||||
|
// A map of event types and their corresponding event handlers.
|
||||
|
export type EventHandlerMap = Map<EventType, EventHandlerList | WildCardEventHandlerList>; |
||||
|
|
||||
|
export interface Emitter { |
||||
|
all: EventHandlerMap; |
||||
|
|
||||
|
on<T = any>(type: EventType, handler: Handler<T>): void; |
||||
|
on(type: '*', handler: WildcardHandler): void; |
||||
|
|
||||
|
off<T = any>(type: EventType, handler: Handler<T>): void; |
||||
|
off(type: '*', handler: WildcardHandler): void; |
||||
|
|
||||
|
emit<T = any>(type: EventType, event?: T): void; |
||||
|
emit(type: '*', event?: any): void; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Mitt: Tiny (~200b) functional event emitter / pubsub. |
||||
|
* @name mitt |
||||
|
* @returns {Mitt} |
||||
|
*/ |
||||
|
export default function mitt(all?: EventHandlerMap): Emitter { |
||||
|
all = all || new Map(); |
||||
|
|
||||
/** |
return { |
||||
* Register an event handler for the given type. |
/** |
||||
* |
* A Map of event names to registered handler functions. |
||||
* @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 |
all, |
||||
*/ |
|
||||
on(type: string | Symbol, handler: Fn) { |
|
||||
const handlers = this.cache?.get(type); |
|
||||
const added = handlers && handlers.push(handler); |
|
||||
if (!added) { |
|
||||
this.cache.set(type, [handler]); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/** |
/** |
||||
* Remove 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 unregister `handler` from, or `"*"` |
* @param {Function} handler Function to call in response to given event |
||||
* @param {Function} handler Handler function to remove |
* @memberOf mitt |
||||
*/ |
*/ |
||||
off(type: string | Symbol, handler: Fn) { |
on<T = any>(type: EventType, handler: Handler<T>) { |
||||
const handlers = this.cache.get(type); |
const handlers = all?.get(type); |
||||
if (handlers) { |
const added = handlers && handlers.push(handler); |
||||
handlers.splice(handlers.indexOf(handler) >>> 0, 1); |
if (!added) { |
||||
} |
all?.set(type, [handler]); |
||||
} |
} |
||||
|
}, |
||||
|
|
||||
/** |
/** |
||||
* Invoke all handlers for the given type. |
* Remove an event handler for the given type. |
||||
* If present, `"*"` handlers are invoked after type-matched handlers. |
* @param {string|symbol} type Type of event to unregister `handler` from, or `"*"` |
||||
* |
* @param {Function} handler Handler function to remove |
||||
* Note: Manually firing "*" handlers is not supported. |
* @memberOf mitt |
||||
* |
*/ |
||||
* @param {string|symbol} type The event type to invoke |
off<T = any>(type: EventType, handler: Handler<T>) { |
||||
* @param {*} [evt] Any value (object is recommended and powerful), passed to each handler |
const handlers = all?.get(type); |
||||
*/ |
if (handlers) { |
||||
emit(type: string | Symbol, evt?: any) { |
handlers.splice(handlers.indexOf(handler) >>> 0, 1); |
||||
for (const handler of (this.cache.get(type) || []).slice()) handler(evt); |
} |
||||
for (const handler of (this.cache.get('*') || []).slice()) handler(type, evt); |
}, |
||||
} |
|
||||
|
|
||||
/** |
/** |
||||
* Remove all event handlers. |
* Invoke all handlers for the given type. |
||||
* |
* If present, `"*"` handlers are invoked after type-matched handlers. |
||||
* Note: This will also remove event handlers passed via `mitt(all: EventHandlerMap)`. |
* |
||||
*/ |
* Note: Manually firing "*" handlers is not supported. |
||||
clear() { |
* |
||||
this.cache.clear(); |
* @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<T = any>(type: EventType, evt: T) { |
||||
|
((all?.get(type) || []) as EventHandlerList).slice().map((handler) => { |
||||
|
handler(evt); |
||||
|
}); |
||||
|
((all?.get('*') || []) as WildCardEventHandlerList).slice().map((handler) => { |
||||
|
handler(type, evt); |
||||
|
}); |
||||
|
}, |
||||
|
}; |
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue