index.ts
This file
Path
src/index.ts
111 lines · 3.5 KiB · TypeScript
Last changed by
Merge branch 'cheaper' of https://github.com/sealice/mitt into sealice-cheaperJason Millerbefore adoption7872a47
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 valueStronger typing (#114)iyegoroffbefore adoption8f439b8
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 typeStronger typing (#114)iyegoroffbefore adoption8f439b8
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.Stronger typing (#114)iyegoroffbefore adoption8f439b8
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 back20Stronger typing (#114)iyegoroffbefore adoption8f439b8
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 back23Stronger typing (#114)iyegoroffbefore adoption8f439b8
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 back26Stronger typing (#114)iyegoroffbefore adoption8f439b8
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 back29Stronger typing (#114)iyegoroffbefore adoption8f439b8
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 */Stronger typing (#114)iyegoroffbefore adoption8f439b8
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.Stronger typing (#114)iyegoroffbefore adoption8f439b8
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 */Stronger typing (#114)iyegoroffbefore adoption8f439b8
lines 60–61why, one step back60 on<Key extends keyof Events>(type: Key, handler: GenericEventHandler) {61 const handlers: Array<GenericEventHandler> | undefined = all!.get(type);cache map.get() for size wins (#100)Jason Millerbefore adoption3bce9a1
lines 62–63why, one step back62 const added = handlers && handlers.push(handler);63 if (!added) {Stronger typing (#114)iyegoroffbefore adoption8f439b8
line 64why, one step back64 all!.set(type, [handler] as EventHandlerList<Events[keyof Events]>);cache map.get() for size wins (#100)Jason Millerbefore adoption3bce9a1
line 65why, one step back65 }Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 66–69why, one step back66 },6768 /**69 * Remove an event handler for the given type.Stronger typing (#114)iyegoroffbefore adoption8f439b8
line 70why, one step back70 * @param {string|symbol} type Type of event to unregister `handler` from, or `'*'`Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 71–73why, one step back71 * @param {Function} handler Handler function to remove72 * @memberOf mitt73 */Stronger typing (#114)iyegoroffbefore adoption8f439b8
lines 74–75why, one step back74 off<Key extends keyof Events>(type: Key, handler: GenericEventHandler) {75 const handlers: Array<GenericEventHandler> | undefined = all!.get(type);cache map.get() for size wins (#100)Jason Millerbefore adoption3bce9a1
lines 76–77why, one step back76 if (handlers) {77 handlers.splice(handlers.indexOf(handler) >>> 0, 1);Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 78–82why, one step back78 }79 },8081 /**82 * Invoke all handlers for the given type.Stronger typing (#114)iyegoroffbefore adoption8f439b8
line 83why, one step back83 * If present, `'*'` handlers are invoked after type-matched handlers.Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
line 84why, one step back84 *Stronger typing (#114)iyegoroffbefore adoption8f439b8
line 85why, one step back85 * Note: Manually firing '*' handlers is not supported.Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 86–90why, one step back86 *87 * @param {string|symbol} type The event type to invoke88 * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler89 * @memberOf mitt90 */Stronger typing (#114)iyegoroffbefore adoption8f439b8
line 91why, one step back91 emit<Key extends keyof Events>(type: Key, evt?: Events[Key]) {Merge branch 'cheaper' of https://github.com/sealice/mitt into sealice-cheaperJason Millerbefore adoption7872a47
lines 92–108why, one step back92 let handlers = all!.get(type);93 if (handlers) {94 (handlers as EventHandlerList<Events[keyof Events]>)95 .slice()96 .map((handler) => {97 handler(evt!);98 });99 }100101 handlers = all!.get('*');102 if (handlers) {103 (handlers as WildCardEventHandlerList<Events>)104 .slice()105 .map((handler) => {106 handler(type, evt!);107 });108 }Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 109–111why, one step back109 }110 };111}