Skip to content
Myr

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

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.

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

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

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

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

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

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

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

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

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

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

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

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.

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

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

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

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

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

lines 62–65why, one step back
62 if (handlers) {
63 handlers.push(handler);64 }65 else {

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

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

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

line 67why, one step back
67 }

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

lines 68–71why, one step back
68 },
6970 /**71 * Remove an event handler for the given type.

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

lines 72–74why, one step back
72 * 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 remove

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

lines 75–76why, one step back
75 * @memberOf mitt
76 */

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

lines 77–78why, one step back
77 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 back
79 if (handlers) {

Fix the format and update the off APIsealicebefore adoptionec9122b

lines 80–83why, one step back
80 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 back
84 all!.set(type, []);

Fix the format and update the off APIsealicebefore adoptionec9122b

line 85why, one step back
85 }

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

lines 86–90why, one step back
86 }
87 },8889 /**90 * Invoke all handlers for the given type.

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

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

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

line 92why, one step back
92 *

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

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

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

lines 94–98why, one step back
94 *
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 back
99 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 back
117 }
118 };119}