Skip to content
Myr

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 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

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

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

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

line 12why, one step back
12export type EventHandlerList<T = unknown> = Array<Handler<T>>;

apply prettier formatting to all filesJason Millerbefore adoption05f4ce6

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

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

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

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

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

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

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

apply prettier formatting to all filesJason Millerbefore adoption05f4ce6

lines 29–32why, one step back
29 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 back
33 off(type: '*', handler: WildcardHandler<Events>): void;

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

line 34why, one step back
34

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

line 35why, one step back
35 emit<Key extends keyof Events>(type: Key, event: Events[Key]): void;

apply prettier formatting to all filesJason Millerbefore adoption05f4ce6

lines 36–38why, one step back
36 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 back
39}
40

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

lines 41–44why, one step back
41/**
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 back
45 */

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

lines 46–51why, one step back
46export 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 back
52 all = all || new Map();
5354 return {55 /**

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

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

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

line 62why, one step back
62 * @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 63–65why, one step back
63 * @param {Function} handler Function to call in response to given event
64 * @memberOf mitt65 */

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

lines 66–67why, one step back
66 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 back
68 if (handlers) {
69 handlers.push(handler);

apply prettier formatting to all filesJason Millerbefore adoption05f4ce6

line 70why, one step back
70 } else {

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

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

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

line 72why, one step back
72 }

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

lines 73–76why, one step back
73 },
7475 /**76 * Remove an event handler for the given type.

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

line 77why, one step back
77 * If `handler` is omitted, all handlers of the given type are removed.

Clarify what `off('*')` doesJason Millerbefore adoptionf9a0b53

line 78why, one step back
78 * @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 back
79 * @param {Function} [handler] Handler function to remove

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

lines 80–81why, one step back
80 * @memberOf mitt
81 */

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

lines 82–83why, one step back
82 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 back
84 if (handlers) {

Fix the format and update the off APIsealicebefore adoptionec9122b

lines 85–86why, one step back
85 if (handler) {
86 handlers.splice(handlers.indexOf(handler) >>> 0, 1);

apply prettier formatting to all filesJason Millerbefore adoption05f4ce6

line 87why, one step back
87 } else {

Add support for mitt.off("type"), which removes all handlers of a given typeJason Millerbefore adoption4cce9cb

line 88why, one step back
88 all!.set(type, []);

Fix the format and update the off APIsealicebefore adoptionec9122b

line 89why, one step back
89 }

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

lines 90–94why, one step back
90 }
91 },9293 /**94 * Invoke all handlers for the given type.

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

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

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

line 96why, one step back
96 *

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

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

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

lines 98–102why, one step back
98 *
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 back
103 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 back
121 }
122 };123}