index.ts
This file
Path
src/index.ts
119 lines · 3.6 KiB · TypeScript
Last changed by
Add support for mitt.off("type"), which removes all handlers of a given typeJason Millerbefore adoption4cce9cb
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 valueMerge branch 'master' into patch_123Jason Millerbefore adoption53eb689
lines 5–9why, one step back5export type Handler<T = unknown> = (event: T) => void;6export type WildcardHandler<T = Record<string, unknown>> = (7 type: keyof T,8 event: T[keyof T]9) => void;Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 10–11why, one step back1011// An array of all currently registered event handlers for a typeMerge branch 'master' into patch_123Jason Millerbefore adoption53eb689
lines 12–13why, one step back12export type EventHandlerList<T = unknown> = Array<Handler<T>>;13export type WildCardEventHandlerList<T = Record<string, unknown>> = Array<WildcardHandler<T>>;Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 14–15why, one step back1415// A map of event types and their corresponding event handlers.Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
lines 16–19why, one step back16export type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<17 keyof Events | '*',18 EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>19>;Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
line 20why, one step back20Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
lines 21–22why, one step back21export interface Emitter<Events extends Record<EventType, unknown>> {22 all: EventHandlerMap<Events>;Merge branch 'expose-all' of https://github.com/jaylinski/mittJason Millerbefore adoption0f41ead
line 23why, one step back23Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
lines 24–25why, one step back24 on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void;25 on(type: '*', handler: WildcardHandler<Events>): void;Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
line 26why, one step back26Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
lines 27–28why, one step back27 off<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>): void;28 off(type: '*', handler: WildcardHandler<Events>): void;Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
line 29why, one step back29Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
lines 30–31why, one step back30 emit<Key extends keyof Events>(type: Key, event: Events[Key]): void;31 emit<Key extends keyof Events>(type: undefined extends Events[Key] ? Key : never): void;Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 32–33why, one step back32}33Merge branch 'expose-all' of https://github.com/jaylinski/mittJason Millerbefore adoption0f41ead
lines 34–37why, one step back34/**35 * Mitt: Tiny (~200b) functional event emitter / pubsub.36 * @name mitt37 * @returns {Mitt}Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
line 38why, one step back38 */Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
lines 39–44why, one step back39export default function mitt<Events extends Record<EventType, unknown>>(40 all?: EventHandlerMap<Events>41): Emitter<Events> {42 type GenericEventHandler =43 | Handler<Events[keyof Events]>44 | WildcardHandler<Events>;Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 45–47why, one step back45 all = all || new Map();4647 return {Use eslint-config-developitJason Millerbefore adoption59cc3d1
line 48why, one step back48Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
line 49why, one step back49 /**Merge branch 'expose-all' of https://github.com/jaylinski/mittJason Millerbefore adoption0f41ead
lines 50–54why, one step back50 * A Map of event names to registered handler functions.51 */52 all,5354 /**Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
line 55why, one step back55 * Register an event handler for the given type.Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
line 56why, one step back56 * @param {string|symbol} type Type of event to listen for, or `'*'` for all eventsMigrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 57–59why, one step back57 * @param {Function} handler Function to call in response to given event58 * @memberOf mitt59 */Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
lines 60–61why, one step back60 on<Key extends keyof Events>(type: Key, handler: GenericEventHandler) {61 const handlers: Array<GenericEventHandler> | undefined = all!.get(type);Add support for mitt.off("type"), which removes all handlers of a given typeJason Millerbefore adoption4cce9cb
lines 62–65why, one step back62 if (handlers) {63 handlers.push(handler);64 }65 else {Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
line 66why, one step back66 all!.set(type, [handler] as EventHandlerList<Events[keyof Events]>);cache map.get() for size wins (#100)Jason Millerbefore adoption3bce9a1
line 67why, one step back67 }Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 68–71why, one step back68 },6970 /**71 * Remove an event handler for the given type.Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
lines 72–74why, one step back72 * If `handler` is omitted, all handlers of the given type are removed.73 * @param {string|symbol} type Type of event to unregister `handler` from, or `'*'`74 * @param {Function} [handler] Handler function to removeMigrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 75–76why, one step back75 * @memberOf mitt76 */Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
lines 77–78why, one step back77 off<Key extends keyof Events>(type: Key, handler?: GenericEventHandler) {78 const handlers: Array<GenericEventHandler> | undefined = all!.get(type);cache map.get() for size wins (#100)Jason Millerbefore adoption3bce9a1
line 79why, one step back79 if (handlers) {Fix the format and update the off APIsealicebefore adoptionec9122b
lines 80–83why, one step back80 if (handler) {81 handlers.splice(handlers.indexOf(handler) >>> 0, 1);82 }83 else {Add support for mitt.off("type"), which removes all handlers of a given typeJason Millerbefore adoption4cce9cb
line 84why, one step back84 all!.set(type, []);Fix the format and update the off APIsealicebefore adoptionec9122b
line 85why, one step back85 }Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 86–90why, one step back86 }87 },8889 /**90 * Invoke all handlers for the given type.Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
line 91why, one step back91 * If present, `'*'` handlers are invoked after type-matched handlers.Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
line 92why, one step back92 *Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
line 93why, one step back93 * Note: Manually firing '*' handlers is not supported.Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 94–98why, one step back94 *95 * @param {string|symbol} type The event type to invoke96 * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler97 * @memberOf mitt98 */Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
lines 99–116why, one step back99 emit<Key extends keyof Events>(type: Key, evt?: Events[Key]) {100 let handlers = all!.get(type);101 if (handlers) {102 (handlers as EventHandlerList<Events[keyof Events]>)103 .slice()104 .map((handler) => {105 handler(evt!);106 });107 }108109 handlers = all!.get('*');110 if (handlers) {111 (handlers as WildCardEventHandlerList<Events>)112 .slice()113 .map((handler) => {114 handler(type, evt!);115 });116 }Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 117–119why, one step back117 }118 };119}