Skip to content
Myr

index.ts

This file

Path

src/index.ts

91 lines · 2.8 KiB · TypeScript

Last changed by

Fix the format and update the off APIsealicebefore adoptionec9122b

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

Add generic types and update tests (#107)Jack Franklinbefore adoption5116df4

lines 5–6why, one step back
5export type Handler<T = any> = (event?: T) => void;
6export type WildcardHandler = (type: EventType, event?: any) => void;

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

lines 7–8why, one step back
7
8// An array of all currently registered event handlers for a type

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

lines 9–10why, one step back
9export type EventHandlerList = Array<Handler>;
10export type WildCardEventHandlerList = Array<WildcardHandler>;

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

lines 11–12why, one step back
11
12// A map of event types and their corresponding event handlers.

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

line 13why, one step back
13export type EventHandlerMap = Map<EventType, EventHandlerList | WildCardEventHandlerList>;

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

lines 14–15why, one step back
14
15export interface Emitter {

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

lines 16–17why, one step back
16 all: EventHandlerMap;
17

Add generic types and update tests (#107)Jack Franklinbefore adoption5116df4

line 18why, one step back
18 on<T = any>(type: EventType, handler: Handler<T>): void;

Use eslint-config-developitJason Millerbefore adoption59cc3d1

line 19why, one step back
19 on(type: '*', handler: WildcardHandler): void;

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

line 20why, one step back
20

Add new features of emitter.off. Fixes #123sealicebefore adoption5f2ccef

line 21why, one step back
21 off<T = any>(type: EventType, handler?: Handler<T>): void;

Use eslint-config-developitJason Millerbefore adoption59cc3d1

line 22why, one step back
22 off(type: '*', handler: WildcardHandler): void;

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

lines 23–24why, one step back
23
24 emit<T = any>(type: EventType, event?: T): void;

Use eslint-config-developitJason Millerbefore adoption59cc3d1

line 25why, one step back
25 emit(type: '*', event?: any): void;

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

lines 26–27why, one step back
26}
27

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

lines 28–31why, one step back
28/**
29 * Mitt: Tiny (~200b) functional event emitter / pubsub.30 * @name mitt31 * @returns {Mitt}

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

line 32why, one step back
32 */

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

line 33why, one step back
33export default function mitt(all?: EventHandlerMap): Emitter {

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

lines 34–36why, one step back
34 all = all || new Map();
3536 return {

Use eslint-config-developitJason Millerbefore adoption59cc3d1

line 37why, one step back
37

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

line 38why, one step back
38 /**

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

lines 39–43why, one step back
39 * A Map of event names to registered handler functions.
40 */41 all,4243 /**

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

lines 44–48why, one step back
44 * Register an event handler for the given type.
45 * @param {string|symbol} type Type of event to listen for, or `"*"` for all events46 * @param {Function} handler Function to call in response to given event47 * @memberOf mitt48 */

Add generic types and update tests (#107)Jack Franklinbefore adoption5116df4

line 49why, one step back
49 on<T = any>(type: EventType, handler: Handler<T>) {

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

lines 50–54why, one step back
50 const handlers = all.get(type);
51 const added = handlers && handlers.push(handler);52 if (!added) {53 all.set(type, [handler]);54 }

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

lines 55–58why, one step back
55 },
5657 /**58 * Remove an event handler for the given type.

Fix the format and update the off APIsealicebefore adoptionec9122b

line 59why, one step back
59 * If omit the `handler`, all event handlers of the given type are deleted.

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

lines 60–63why, one step back
60 * @param {string|symbol} type Type of event to unregister `handler` from, or `"*"`
61 * @param {Function} handler Handler function to remove62 * @memberOf mitt63 */

Add new features of emitter.off. Fixes #123sealicebefore adoption5f2ccef

line 64why, one step back
64 off<T = any>(type: EventType, handler?: Handler<T>) {

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

lines 65–66why, one step back
65 const handlers = all.get(type);
66 if (handlers) {

Fix the format and update the off APIsealicebefore adoptionec9122b

lines 67–72why, one step back
67 if (handler) {
68 handlers.splice(handlers.indexOf(handler) >>> 0, 1);69 }70 else {71 all.delete(type);72 }

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

lines 73–85why, one step back
73 }
74 },7576 /**77 * Invoke all handlers for the given type.78 * If present, `"*"` handlers are invoked after type-matched handlers.79 *80 * Note: Manually firing "*" handlers is not supported.81 *82 * @param {string|symbol} type The event type to invoke83 * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler84 * @memberOf mitt85 */

Add generic types and update tests (#107)Jack Franklinbefore adoption5116df4

line 86why, one step back
86 emit<T = any>(type: EventType, evt: T) {

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

lines 87–91why, one step back
87 ((all.get(type) || []) as EventHandlerList).slice().map((handler) => { handler(evt); });
88 ((all.get('*') || []) as WildCardEventHandlerList).slice().map((handler) => { handler(type, evt); });89 }90 };91}