Skip to content
Myr

index.ts

This file

Path

src/index.ts

78 lines · 2.5 KiB · TypeScript

Last changed by

Add missing space (#103)Jay Linskibefore adoption1c26402

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

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

line 5why, one step back
5export type Handler = (event?: any) => void;

Add missing space (#103)Jay Linskibefore adoption1c26402

line 6why, one step back
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–16why, one step back
14
15export interface Emitter {16 on(type: EventType, handler: Handler): void;

Use eslint-config-developitJason Millerbefore adoption59cc3d1

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

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

lines 18–19why, one step back
18
19 off(type: EventType, handler: Handler): void;

Use eslint-config-developitJason Millerbefore adoption59cc3d1

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

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

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

Use eslint-config-developitJason Millerbefore adoption59cc3d1

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

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

lines 24–29why, one step back
24}
2526/** Mitt: Tiny (~200b) functional event emitter / pubsub.27 * @name mitt28 * @returns {Mitt}29 */

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

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

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

lines 31–33why, one step back
31 all = all || new Map();
3233 return {

Use eslint-config-developitJason Millerbefore adoption59cc3d1

line 34why, one step back
34

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

lines 35–41why, one step back
35 /**
36 * Register an event handler for the given type.37 * @param {string|symbol} type Type of event to listen for, or `"*"` for all events38 * @param {Function} handler Function to call in response to given event39 * @memberOf mitt40 */41 on(type: EventType, handler: Handler) {

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

lines 42–46why, one step back
42 const handlers = all.get(type);
43 const added = handlers && handlers.push(handler);44 if (!added) {45 all.set(type, [handler]);46 }

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

lines 47–56why, one step back
47 },
4849 /**50 * Remove an event handler for the given type.51 *52 * @param {string|symbol} type Type of event to unregister `handler` from, or `"*"`53 * @param {Function} handler Handler function to remove54 * @memberOf mitt55 */56 off(type: EventType, handler: Handler) {

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

lines 57–59why, one step back
57 const handlers = all.get(type);
58 if (handlers) {59 handlers.splice(handlers.indexOf(handler) >>> 0, 1);

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

lines 60–78why, one step back
60 }
61 },6263 /**64 * Invoke all handlers for the given type.65 * If present, `"*"` handlers are invoked after type-matched handlers.66 *67 * Note: Manually firing "*" handlers is not supported.68 *69 * @param {string|symbol} type The event type to invoke70 * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler71 * @memberOf mitt72 */73 emit(type: EventType, evt: any) {74 ((all.get(type) || []) as EventHandlerList).slice().map((handler) => { handler(evt); });75 ((all.get('*') || []) as WildCardEventHandlerList).slice().map((handler) => { handler(type, evt); });76 }77 };78}