Skip to content
Myr

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 file

Take it

Export Mitt types for TS consumers (#101)Jack Franklinbefore adoptioneb2be7c

line 1why, one step back
1export type EventType = string | symbol;

Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f

lines 2–4why, one step back
2
3// An event handler can take an optional event argument4// and should not return a value

Stronger typing (#114)iyegoroffbefore adoption8f439b8

lines 5–9why, one step back
5export 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 back
10
11// An array of all currently registered event handlers for a type

Stronger typing (#114)iyegoroffbefore adoption8f439b8

lines 12–13why, one step back
12export 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 back
14
15// A map of event types and their corresponding event handlers.

Stronger typing (#114)iyegoroffbefore adoption8f439b8

lines 16–19why, one step back
16export 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 back
20

Stronger typing (#114)iyegoroffbefore adoption8f439b8

lines 21–22why, one step back
21export 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 back
23

Stronger typing (#114)iyegoroffbefore adoption8f439b8

lines 24–25why, one step back
24 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 back
26

Stronger typing (#114)iyegoroffbefore adoption8f439b8

lines 27–28why, one step back
27 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 back
29

Stronger typing (#114)iyegoroffbefore adoption8f439b8

lines 30–31why, one step back
30 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 back
32}
33

Merge branch 'expose-all' of https://github.com/jaylinski/mittJason Millerbefore adoption0f41ead

lines 34–37why, one step back
34/**
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 back
38 */

Stronger typing (#114)iyegoroffbefore adoption8f439b8

lines 39–44why, one step back
39export 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 back
45 all = all || new Map();
4647 return {

Use eslint-config-developitJason Millerbefore adoption59cc3d1

line 48why, one step back
48

Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f

line 49why, one step back
49 /**

Merge branch 'expose-all' of https://github.com/jaylinski/mittJason Millerbefore adoption0f41ead

lines 50–54why, one step back
50 * 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 back
55 * Register an event handler for the given type.

Stronger typing (#114)iyegoroffbefore adoption8f439b8

line 56why, one step back
56 * @param {string|symbol} type Type of event to listen for, or `'*'` for all events

Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f

lines 57–59why, one step back
57 * @param {Function} handler Function to call in response to given event
58 * @memberOf mitt59 */

Stronger typing (#114)iyegoroffbefore adoption8f439b8

lines 60–61why, one step back
60 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 back
62 const added = handlers && handlers.push(handler);
63 if (!added) {

Stronger typing (#114)iyegoroffbefore adoption8f439b8

line 64why, one step back
64 all!.set(type, [handler] as EventHandlerList<Events[keyof Events]>);

cache map.get() for size wins (#100)Jason Millerbefore adoption3bce9a1

line 65why, one step back
65 }

Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f

lines 66–69why, one step back
66 },
6768 /**69 * Remove an event handler for the given type.

Stronger typing (#114)iyegoroffbefore adoption8f439b8

line 70why, one step back
70 * @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 back
71 * @param {Function} handler Handler function to remove
72 * @memberOf mitt73 */

Stronger typing (#114)iyegoroffbefore adoption8f439b8

lines 74–75why, one step back
74 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 back
76 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 back
78 }
79 },8081 /**82 * Invoke all handlers for the given type.

Stronger typing (#114)iyegoroffbefore adoption8f439b8

line 83why, one step back
83 * If present, `'*'` handlers are invoked after type-matched handlers.

Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f

line 84why, one step back
84 *

Stronger typing (#114)iyegoroffbefore adoption8f439b8

line 85why, one step back
85 * Note: Manually firing '*' handlers is not supported.

Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f

lines 86–90why, one step back
86 *
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 back
91 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 back
92 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 back
109 }
110 };111}