index.ts
This file
Path
src/index.ts
123 lines · 3.6 KiB · TypeScript
Last changed by
apply prettier formatting to all filesJason Millerbefore adoption05f4ce6
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
line 12why, one step back12export type EventHandlerList<T = unknown> = Array<Handler<T>>;apply prettier formatting to all filesJason Millerbefore adoption05f4ce6
lines 13–15why, one step back13export type WildCardEventHandlerList<T = Record<string, unknown>> = Array<14 WildcardHandler<T>15>;Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 16–17why, one step back1617// A map of event types and their corresponding event handlers.Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
lines 18–21why, one step back18export type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<19 keyof Events | '*',20 EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>21>;Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
line 22why, one step back22Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
lines 23–24why, one step back23export interface Emitter<Events extends Record<EventType, unknown>> {24 all: EventHandlerMap<Events>;Merge branch 'expose-all' of https://github.com/jaylinski/mittJason Millerbefore adoption0f41ead
line 25why, one step back25Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
lines 26–27why, one step back26 on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void;27 on(type: '*', handler: WildcardHandler<Events>): void;Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
line 28why, one step back28apply prettier formatting to all filesJason Millerbefore adoption05f4ce6
lines 29–32why, one step back29 off<Key extends keyof Events>(30 type: Key,31 handler?: Handler<Events[Key]>32 ): void;Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
line 33why, one step back33 off(type: '*', handler: WildcardHandler<Events>): void;Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
line 34why, one step back34Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
line 35why, one step back35 emit<Key extends keyof Events>(type: Key, event: Events[Key]): void;apply prettier formatting to all filesJason Millerbefore adoption05f4ce6
lines 36–38why, one step back36 emit<Key extends keyof Events>(37 type: undefined extends Events[Key] ? Key : never38 ): void;Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 39–40why, one step back39}40Merge branch 'expose-all' of https://github.com/jaylinski/mittJason Millerbefore adoption0f41ead
lines 41–44why, one step back41/**42 * Mitt: Tiny (~200b) functional event emitter / pubsub.43 * @name mitt44 * @returns {Mitt}Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
line 45why, one step back45 */Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
lines 46–51why, one step back46export default function mitt<Events extends Record<EventType, unknown>>(47 all?: EventHandlerMap<Events>48): Emitter<Events> {49 type GenericEventHandler =50 | Handler<Events[keyof Events]>51 | WildcardHandler<Events>;Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 52–55why, one step back52 all = all || new Map();5354 return {55 /**Merge branch 'expose-all' of https://github.com/jaylinski/mittJason Millerbefore adoption0f41ead
lines 56–60why, one step back56 * A Map of event names to registered handler functions.57 */58 all,5960 /**Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
line 61why, one step back61 * Register an event handler for the given type.Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
line 62why, one step back62 * @param {string|symbol} type Type of event to listen for, or `'*'` for all eventsMigrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 63–65why, one step back63 * @param {Function} handler Function to call in response to given event64 * @memberOf mitt65 */Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
lines 66–67why, one step back66 on<Key extends keyof Events>(type: Key, handler: GenericEventHandler) {67 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 68–69why, one step back68 if (handlers) {69 handlers.push(handler);apply prettier formatting to all filesJason Millerbefore adoption05f4ce6
line 70why, one step back70 } else {Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
line 71why, one step back71 all!.set(type, [handler] as EventHandlerList<Events[keyof Events]>);cache map.get() for size wins (#100)Jason Millerbefore adoption3bce9a1
line 72why, one step back72 }Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 73–76why, one step back73 },7475 /**76 * Remove an event handler for the given type.Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
line 77why, one step back77 * If `handler` is omitted, all handlers of the given type are removed.Clarify what `off('*')` doesJason Millerbefore adoptionf9a0b53
line 78why, one step back78 * @param {string|symbol} type Type of event to unregister `handler` from (`'*'` to remove a wildcard handler)Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
line 79why, one step back79 * @param {Function} [handler] Handler function to removeMigrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 80–81why, one step back80 * @memberOf mitt81 */Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
lines 82–83why, one step back82 off<Key extends keyof Events>(type: Key, handler?: GenericEventHandler) {83 const handlers: Array<GenericEventHandler> | undefined = all!.get(type);cache map.get() for size wins (#100)Jason Millerbefore adoption3bce9a1
line 84why, one step back84 if (handlers) {Fix the format and update the off APIsealicebefore adoptionec9122b
lines 85–86why, one step back85 if (handler) {86 handlers.splice(handlers.indexOf(handler) >>> 0, 1);apply prettier formatting to all filesJason Millerbefore adoption05f4ce6
line 87why, one step back87 } else {Add support for mitt.off("type"), which removes all handlers of a given typeJason Millerbefore adoption4cce9cb
line 88why, one step back88 all!.set(type, []);Fix the format and update the off APIsealicebefore adoptionec9122b
line 89why, one step back89 }Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 90–94why, one step back90 }91 },9293 /**94 * Invoke all handlers for the given type.Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
line 95why, one step back95 * If present, `'*'` handlers are invoked after type-matched handlers.Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
line 96why, one step back96 *Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
line 97why, one step back97 * Note: Manually firing '*' handlers is not supported.Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 98–102why, one step back98 *99 * @param {string|symbol} type The event type to invoke100 * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler101 * @memberOf mitt102 */Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
lines 103–120why, one step back103 emit<Key extends keyof Events>(type: Key, evt?: Events[Key]) {104 let handlers = all!.get(type);105 if (handlers) {106 (handlers as EventHandlerList<Events[keyof Events]>)107 .slice()108 .map((handler) => {109 handler(evt!);110 });111 }112113 handlers = all!.get('*');114 if (handlers) {115 (handlers as WildCardEventHandlerList<Events>)116 .slice()117 .map((handler) => {118 handler(type, evt!);119 });120 }Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 121–123why, one step back121 }122 };123}