index.ts
This file
Path
src/index.ts
91 lines · 2.8 KiB · TypeScript
Last changed by
Fix the format and update the off APIsealicebefore adoptionec9122b
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 valueAdd generic types and update tests (#107)Jack Franklinbefore adoption5116df4
lines 5–6why, one step back5export type Handler<T = any> = (event?: T) => void;6export type WildcardHandler = (type: EventType, event?: any) => void;Migrate 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–15why, one step back1415export interface Emitter {Merge branch 'expose-all' of https://github.com/jaylinski/mittJason Millerbefore adoption0f41ead
lines 16–17why, one step back16 all: EventHandlerMap;17Add generic types and update tests (#107)Jack Franklinbefore adoption5116df4
line 18why, one step back18 on<T = any>(type: EventType, handler: Handler<T>): void;Use eslint-config-developitJason Millerbefore adoption59cc3d1
line 19why, one step back19 on(type: '*', handler: WildcardHandler): void;Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
line 20why, one step back20Add new features of emitter.off. Fixes #123sealicebefore adoption5f2ccef
line 21why, one step back21 off<T = any>(type: EventType, handler?: Handler<T>): void;Use eslint-config-developitJason Millerbefore adoption59cc3d1
line 22why, one step back22 off(type: '*', handler: WildcardHandler): void;Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 23–24why, one step back2324 emit<T = any>(type: EventType, event?: T): void;Use eslint-config-developitJason Millerbefore adoption59cc3d1
line 25why, one step back25 emit(type: '*', event?: any): void;Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 26–27why, one step back26}27Merge branch 'expose-all' of https://github.com/jaylinski/mittJason Millerbefore adoption0f41ead
lines 28–31why, one step back28/**29 * Mitt: Tiny (~200b) functional event emitter / pubsub.30 * @name mitt31 * @returns {Mitt}Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
line 32why, one step back32 */cache map.get() for size wins (#100)Jason Millerbefore adoption3bce9a1
line 33why, one step back33export default function mitt(all?: EventHandlerMap): Emitter {Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 34–36why, one step back34 all = all || new Map();3536 return {Use eslint-config-developitJason Millerbefore adoption59cc3d1
line 37why, one step back37Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
line 38why, one step back38 /**Merge branch 'expose-all' of https://github.com/jaylinski/mittJason Millerbefore adoption0f41ead
lines 39–43why, one step back39 * A Map of event names to registered handler functions.40 */41 all,4243 /**Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 44–48why, one step back44 * Register an event handler for the given type.45 * @param {string|symbol} type Type of event to listen for, or `"*"` for all events46 * @param {Function} handler Function to call in response to given event47 * @memberOf mitt48 */Add generic types and update tests (#107)Jack Franklinbefore adoption5116df4
line 49why, one step back49 on<T = any>(type: EventType, handler: Handler<T>) {cache map.get() for size wins (#100)Jason Millerbefore adoption3bce9a1
lines 50–54why, one step back50 const handlers = all.get(type);51 const added = handlers && handlers.push(handler);52 if (!added) {53 all.set(type, [handler]);54 }Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 55–58why, one step back55 },5657 /**58 * Remove an event handler for the given type.Fix the format and update the off APIsealicebefore adoptionec9122b
line 59why, one step back59 * If omit the `handler`, all event handlers of the given type are deleted.Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 60–63why, one step back60 * @param {string|symbol} type Type of event to unregister `handler` from, or `"*"`61 * @param {Function} handler Handler function to remove62 * @memberOf mitt63 */Add new features of emitter.off. Fixes #123sealicebefore adoption5f2ccef
line 64why, one step back64 off<T = any>(type: EventType, handler?: Handler<T>) {cache map.get() for size wins (#100)Jason Millerbefore adoption3bce9a1
lines 65–66why, one step back65 const handlers = all.get(type);66 if (handlers) {Fix the format and update the off APIsealicebefore adoptionec9122b
lines 67–72why, one step back67 if (handler) {68 handlers.splice(handlers.indexOf(handler) >>> 0, 1);69 }70 else {71 all.delete(type);72 }Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 73–85why, one step back73 }74 },7576 /**77 * Invoke all handlers for the given type.78 * If present, `"*"` handlers are invoked after type-matched handlers.79 *80 * Note: Manually firing "*" handlers is not supported.81 *82 * @param {string|symbol} type The event type to invoke83 * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler84 * @memberOf mitt85 */Add generic types and update tests (#107)Jack Franklinbefore adoption5116df4
line 86why, one step back86 emit<T = any>(type: EventType, evt: T) {Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 87–91why, one step back87 ((all.get(type) || []) as EventHandlerList).slice().map((handler) => { handler(evt); });88 ((all.get('*') || []) as WildCardEventHandlerList).slice().map((handler) => { handler(type, evt); });89 }90 };91}