index.ts
This file
Path
src/index.ts
78 lines · 2.5 KiB · TypeScript
Last changed by
Add missing space (#103)Jay Linskibefore adoption1c26402
History of this fileTake it
Export Mitt types for TS consumers (#101)Jack Franklinbefore adoptioneb2be7c
line 1why, one step back1export type EventType = string | symbol;Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 2–4why, one step back23// An event handler can take an optional event argument4// and should not return a valueExport Mitt types for TS consumers (#101)Jack Franklinbefore adoptioneb2be7c
line 5why, one step back5export type Handler = (event?: any) => void;Add missing space (#103)Jay Linskibefore adoption1c26402
line 6why, one step back6export type WildcardHandler = (type: EventType, event?: any) => voidMigrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 7–8why, one step back78// An array of all currently registered event handlers for a typeExport Mitt types for TS consumers (#101)Jack Franklinbefore adoptioneb2be7c
lines 9–10why, one step back9export type EventHandlerList = Array<Handler>;10export type WildCardEventHandlerList = Array<WildcardHandler>;Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 11–12why, one step back1112// A map of event types and their corresponding event handlers.Export Mitt types for TS consumers (#101)Jack Franklinbefore adoptioneb2be7c
line 13why, one step back13export type EventHandlerMap = Map<EventType, EventHandlerList | WildCardEventHandlerList>;Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 14–16why, one step back1415export interface Emitter {16 on(type: EventType, handler: Handler): void;Use eslint-config-developitJason Millerbefore adoption59cc3d1
line 17why, one step back17 on(type: '*', handler: WildcardHandler): void;Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 18–19why, one step back1819 off(type: EventType, handler: Handler): void;Use eslint-config-developitJason Millerbefore adoption59cc3d1
line 20why, one step back20 off(type: '*', handler: WildcardHandler): void;Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 21–22why, one step back2122 emit<T = any>(type: EventType, event?: T): void;Use eslint-config-developitJason Millerbefore adoption59cc3d1
line 23why, one step back23 emit(type: '*', event?: any): void;Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 24–29why, one step back24}2526/** Mitt: Tiny (~200b) functional event emitter / pubsub.27 * @name mitt28 * @returns {Mitt}29 */cache map.get() for size wins (#100)Jason Millerbefore adoption3bce9a1
line 30why, one step back30export default function mitt(all?: EventHandlerMap): Emitter {Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 31–33why, one step back31 all = all || new Map();3233 return {Use eslint-config-developitJason Millerbefore adoption59cc3d1
line 34why, one step back34Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 35–41why, one step back35 /**36 * Register an event handler for the given type.37 * @param {string|symbol} type Type of event to listen for, or `"*"` for all events38 * @param {Function} handler Function to call in response to given event39 * @memberOf mitt40 */41 on(type: EventType, handler: Handler) {cache map.get() for size wins (#100)Jason Millerbefore adoption3bce9a1
lines 42–46why, one step back42 const handlers = all.get(type);43 const added = handlers && handlers.push(handler);44 if (!added) {45 all.set(type, [handler]);46 }Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 47–56why, one step back47 },4849 /**50 * Remove an event handler for the given type.51 *52 * @param {string|symbol} type Type of event to unregister `handler` from, or `"*"`53 * @param {Function} handler Handler function to remove54 * @memberOf mitt55 */56 off(type: EventType, handler: Handler) {cache map.get() for size wins (#100)Jason Millerbefore adoption3bce9a1
lines 57–59why, one step back57 const handlers = all.get(type);58 if (handlers) {59 handlers.splice(handlers.indexOf(handler) >>> 0, 1);Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 60–78why, one step back60 }61 },6263 /**64 * Invoke all handlers for the given type.65 * If present, `"*"` handlers are invoked after type-matched handlers.66 *67 * Note: Manually firing "*" handlers is not supported.68 *69 * @param {string|symbol} type The event type to invoke70 * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler71 * @memberOf mitt72 */73 emit(type: EventType, evt: any) {74 ((all.get(type) || []) as EventHandlerList).slice().map((handler) => { handler(evt); });75 ((all.get('*') || []) as WildCardEventHandlerList).slice().map((handler) => { handler(type, evt); });76 }77 };78}