vben
authored
5 years ago
1
/**
vben
authored
4 years ago
2
3
* copy to https://github.com/developit/mitt
* Expand clear method
vben
authored
5 years ago
4
*/
Vben
authored
4 years ago
5
6
7
8
export type EventType = string | symbol;
// An event handler can take an optional event argument
// and should not return a value
9
10
11
12
13
export type Handler<T = unknown> = (event: T) => void;
export type WildcardHandler<T = Record<string, unknown>> = (
type: keyof T,
event: T[keyof T],
) => void;
Vben
authored
4 years ago
14
15
// An array of all currently registered event handlers for a type
16
17
export type EventHandlerList<T = unknown> = Array<Handler<T>>;
export type WildCardEventHandlerList<T = Record<string, unknown>> = Array<WildcardHandler<T>>;
Vben
authored
4 years ago
18
19
// A map of event types and their corresponding event handlers.
20
21
22
23
export type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<
keyof Events | '*',
EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>
>;
Vben
authored
4 years ago
24
25
26
export interface Emitter<Events extends Record<EventType, unknown>> {
all: EventHandlerMap<Events>;
Vben
authored
4 years ago
27
28
29
on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void;
on(type: '*', handler: WildcardHandler<Events>): void;
Vben
authored
4 years ago
30
31
32
off<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>): void;
off(type: '*', handler: WildcardHandler<Events>): void;
Vben
authored
4 years ago
33
34
35
emit<Key extends keyof Events>(type: Key, event: Events[Key]): void;
emit<Key extends keyof Events>(type: undefined extends Events[Key] ? Key : never): void;
Vben
authored
4 years ago
36
clear(): void;
Vben
authored
4 years ago
37
38
39
40
41
42
43
}
/**
* Mitt: Tiny (~200b) functional event emitter / pubsub.
* @name mitt
* @returns {Mitt}
*/
44
45
46
47
export function mitt<Events extends Record<EventType, unknown>>(
all?: EventHandlerMap<Events>,
): Emitter<Events> {
type GenericEventHandler = Handler<Events[keyof Events]> | WildcardHandler<Events>;
Vben
authored
4 years ago
48
all = all || new Map();
vben
authored
5 years ago
49
Vben
authored
4 years ago
50
51
52
53
54
return {
/**
* A Map of event names to registered handler functions.
*/
all,
vben
authored
5 years ago
55
Vben
authored
4 years ago
56
57
/**
* Register an event handler for the given type.
58
* @param {string|symbol} type Type of event to listen for, or `'*'` for all events
Vben
authored
4 years ago
59
60
61
* @param {Function} handler Function to call in response to given event
* @memberOf mitt
*/
62
63
64
65
66
67
on<Key extends keyof Events>(type: Key, handler: GenericEventHandler) {
const handlers: Array<GenericEventHandler> | undefined = all!.get(type);
if (handlers) {
handlers.push(handler);
} else {
all!.set(type, [handler] as EventHandlerList<Events[keyof Events]>);
Vben
authored
4 years ago
68
69
}
},
vben
authored
5 years ago
70
Vben
authored
4 years ago
71
72
/**
* Remove an event handler for the given type.
73
74
75
* If `handler` is omitted, all handlers of the given type are removed.
* @param {string|symbol} type Type of event to unregister `handler` from (`'*'` to remove a wildcard handler)
* @param {Function} [handler] Handler function to remove
Vben
authored
4 years ago
76
77
* @memberOf mitt
*/
78
79
off<Key extends keyof Events>(type: Key, handler?: GenericEventHandler) {
const handlers: Array<GenericEventHandler> | undefined = all!.get(type);
Vben
authored
4 years ago
80
if (handlers) {
81
82
83
84
85
if (handler) {
handlers.splice(handlers.indexOf(handler) >>> 0, 1);
} else {
all!.set(type, []);
}
Vben
authored
4 years ago
86
87
}
},
vben
authored
5 years ago
88
Vben
authored
4 years ago
89
90
/**
* Invoke all handlers for the given type.
91
* If present, `'*'` handlers are invoked after type-matched handlers.
Vben
authored
4 years ago
92
*
93
* Note: Manually firing '*' handlers is not supported.
Vben
authored
4 years ago
94
95
96
97
98
*
* @param {string|symbol} type The event type to invoke
* @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler
* @memberOf mitt
*/
99
100
101
102
103
104
105
106
107
108
109
110
111
112
emit<Key extends keyof Events>(type: Key, evt?: Events[Key]) {
let handlers = all!.get(type);
if (handlers) {
(handlers as EventHandlerList<Events[keyof Events]>).slice().forEach((handler) => {
handler(evt as Events[Key]);
});
}
handlers = all!.get('*');
if (handlers) {
(handlers as WildCardEventHandlerList<Events>).slice().forEach((handler) => {
handler(type, evt as Events[Key]);
});
}
Vben
authored
4 years ago
113
},
Vben
authored
4 years ago
114
115
116
117
118
119
120
/**
* Clear all
*/
clear() {
this.all.clear();
},
Vben
authored
4 years ago
121
};
vben
authored
5 years ago
122
}