@stacksjs/events 0.59.11 → 0.61.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +14 -14
- package/package.json +1 -1
- package/src/index.ts +39 -51
- package/dist/index.d.ts +0 -25
package/dist/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// @bun
|
|
2
2
|
// src/index.ts
|
|
3
3
|
function mitt(all) {
|
|
4
|
-
|
|
4
|
+
if (!all)
|
|
5
|
+
all = new Map;
|
|
5
6
|
return {
|
|
6
7
|
all,
|
|
7
8
|
on(type, handler) {
|
|
@@ -14,29 +15,28 @@ function mitt(all) {
|
|
|
14
15
|
off(type, handler) {
|
|
15
16
|
const handlers = all.get(type);
|
|
16
17
|
if (handlers) {
|
|
17
|
-
if (handler)
|
|
18
|
-
handlers.
|
|
19
|
-
|
|
18
|
+
if (handler) {
|
|
19
|
+
const index = handlers.indexOf(handler);
|
|
20
|
+
if (index > -1)
|
|
21
|
+
handlers.splice(index, 1);
|
|
22
|
+
} else {
|
|
20
23
|
all.set(type, []);
|
|
24
|
+
}
|
|
21
25
|
}
|
|
22
26
|
},
|
|
23
27
|
emit(type, evt) {
|
|
24
28
|
let handlers = all.get(type);
|
|
25
29
|
if (handlers) {
|
|
26
|
-
handlers.slice().
|
|
27
|
-
if (evt)
|
|
28
|
-
|
|
29
|
-
console.error("No event provided");
|
|
30
|
-
return "No event provided";
|
|
30
|
+
handlers.slice().forEach((handler) => {
|
|
31
|
+
if (evt !== undefined)
|
|
32
|
+
handler(evt);
|
|
31
33
|
});
|
|
32
34
|
}
|
|
33
35
|
handlers = all.get("*");
|
|
34
36
|
if (handlers) {
|
|
35
|
-
handlers.slice().
|
|
36
|
-
if (evt)
|
|
37
|
-
|
|
38
|
-
console.error("No event provided");
|
|
39
|
-
return "No event provided";
|
|
37
|
+
handlers.slice().forEach((handler) => {
|
|
38
|
+
if (evt !== undefined)
|
|
39
|
+
handler(type, evt);
|
|
40
40
|
});
|
|
41
41
|
}
|
|
42
42
|
}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -5,10 +5,7 @@ export type EventType = string | symbol
|
|
|
5
5
|
// An event handler can take an optional event argument
|
|
6
6
|
// and should not return a value
|
|
7
7
|
export type Handler<T = unknown> = (event: T) => void
|
|
8
|
-
export type WildcardHandler<T = Record<string, unknown>> = (
|
|
9
|
-
type: keyof T,
|
|
10
|
-
event: T[keyof T]
|
|
11
|
-
) => void
|
|
8
|
+
export type WildcardHandler<T = Record<string, unknown>> = (type: keyof T, event: T[keyof T]) => void
|
|
12
9
|
|
|
13
10
|
// An array of all currently registered event handlers for a type
|
|
14
11
|
export type EventHandlerList<T = unknown> = Array<Handler<T>>
|
|
@@ -23,26 +20,25 @@ export type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<
|
|
|
23
20
|
export interface Emitter<Events extends Record<EventType, unknown>> {
|
|
24
21
|
all: EventHandlerMap<Events>
|
|
25
22
|
|
|
26
|
-
on: (<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>) => void) &
|
|
23
|
+
on: (<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>) => void) &
|
|
24
|
+
((type: '*', handler: WildcardHandler<Events>) => void)
|
|
27
25
|
|
|
28
|
-
off: (<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>) => void) &
|
|
26
|
+
off: (<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>) => void) &
|
|
27
|
+
((type: '*', handler?: WildcardHandler<Events>) => void)
|
|
29
28
|
|
|
30
|
-
emit: (<Key extends keyof Events>(type: Key, event: Events[Key]) => void) &
|
|
29
|
+
emit: (<Key extends keyof Events>(type: Key, event: Events[Key]) => void) &
|
|
30
|
+
(<Key extends keyof Events>(type: undefined extends Events[Key] ? Key : never) => void)
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
/**
|
|
34
|
-
*
|
|
34
|
+
* Tiny (~200b) functional event emitter / pubsub.
|
|
35
35
|
*/
|
|
36
36
|
export default function mitt<Events extends Record<EventType, unknown>>(
|
|
37
37
|
all?: EventHandlerMap<Events>,
|
|
38
38
|
): Emitter<Events> {
|
|
39
|
-
|
|
40
|
-
| Handler<Events[keyof Events]>
|
|
41
|
-
| WildcardHandler<Events>
|
|
42
|
-
all = all || new Map()
|
|
39
|
+
if (!all) all = new Map()
|
|
43
40
|
|
|
44
41
|
return {
|
|
45
|
-
|
|
46
42
|
/**
|
|
47
43
|
* A Map of event names to registered handler functions.
|
|
48
44
|
*/
|
|
@@ -54,13 +50,16 @@ export default function mitt<Events extends Record<EventType, unknown>>(
|
|
|
54
50
|
* @param {Function} handler Function to call in response to given event
|
|
55
51
|
* @memberOf mitt
|
|
56
52
|
*/
|
|
57
|
-
on<Key extends keyof Events>(type: Key, handler:
|
|
58
|
-
const handlers: Array<
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
53
|
+
on<Key extends keyof Events>(type: Key | '*', handler: Handler<Events[Key]> | WildcardHandler<Events>) {
|
|
54
|
+
const handlers: Array<Handler<Events[Key]> | WildcardHandler<Events>> | undefined = (
|
|
55
|
+
all as EventHandlerMap<Events>
|
|
56
|
+
).get(type)
|
|
57
|
+
if (handlers) handlers.push(handler as Handler<Events[Key]> | WildcardHandler<Events>)
|
|
58
|
+
// Explicitly assert the type of the handler array being set in the map. Unsure if there is a better way to do this
|
|
62
59
|
else
|
|
63
|
-
all
|
|
60
|
+
(all as EventHandlerMap<Events>).set(type, [handler] as
|
|
61
|
+
| EventHandlerList<Events[keyof Events]>
|
|
62
|
+
| WildCardEventHandlerList<Events>)
|
|
64
63
|
},
|
|
65
64
|
|
|
66
65
|
/**
|
|
@@ -70,14 +69,17 @@ export default function mitt<Events extends Record<EventType, unknown>>(
|
|
|
70
69
|
* @param {Function} [handler] Handler function to remove
|
|
71
70
|
* @memberOf mitt
|
|
72
71
|
*/
|
|
73
|
-
off<Key extends keyof Events>(type: Key, handler?:
|
|
74
|
-
const handlers: Array<
|
|
72
|
+
off<Key extends keyof Events>(type: Key | '*', handler?: Handler<Events[Key]> | WildcardHandler<Events>) {
|
|
73
|
+
const handlers: Array<Handler<Events[Key]> | WildcardHandler<Events>> | undefined = (
|
|
74
|
+
all as EventHandlerMap<Events>
|
|
75
|
+
).get(type)
|
|
75
76
|
if (handlers) {
|
|
76
|
-
if (handler)
|
|
77
|
-
handlers.
|
|
78
|
-
|
|
79
|
-
else
|
|
80
|
-
all
|
|
77
|
+
if (handler) {
|
|
78
|
+
const index = handlers.indexOf(handler as Handler<Events[Key]> | WildcardHandler<Events>)
|
|
79
|
+
if (index > -1) handlers.splice(index, 1)
|
|
80
|
+
} else {
|
|
81
|
+
;(all as EventHandlerMap<Events>).set(type, [])
|
|
82
|
+
}
|
|
81
83
|
}
|
|
82
84
|
},
|
|
83
85
|
|
|
@@ -92,31 +94,17 @@ export default function mitt<Events extends Record<EventType, unknown>>(
|
|
|
92
94
|
* @memberOf mitt
|
|
93
95
|
*/
|
|
94
96
|
emit<Key extends keyof Events>(type: Key, evt?: Events[Key]) {
|
|
95
|
-
let handlers = all
|
|
97
|
+
let handlers = (all as EventHandlerMap<Events>).get(type)
|
|
96
98
|
if (handlers) {
|
|
97
|
-
(handlers as EventHandlerList<Events[keyof Events]>)
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
if (evt)
|
|
101
|
-
return handler(evt)
|
|
102
|
-
|
|
103
|
-
console.error('No event provided')
|
|
104
|
-
return 'No event provided'
|
|
105
|
-
})
|
|
99
|
+
;(handlers as EventHandlerList<Events[keyof Events]>).slice().forEach((handler) => {
|
|
100
|
+
if (evt !== undefined) handler(evt)
|
|
101
|
+
})
|
|
106
102
|
}
|
|
107
|
-
|
|
108
|
-
handlers = all!.get('*')
|
|
103
|
+
handlers = (all as EventHandlerMap<Events>).get('*')
|
|
109
104
|
if (handlers) {
|
|
110
|
-
(handlers as WildCardEventHandlerList<Events>)
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
.map((handler: any) => {
|
|
114
|
-
if (evt)
|
|
115
|
-
return handler(type, evt)
|
|
116
|
-
|
|
117
|
-
console.error('No event provided')
|
|
118
|
-
return 'No event provided'
|
|
119
|
-
})
|
|
105
|
+
;(handlers as WildCardEventHandlerList<Events>).slice().forEach((handler) => {
|
|
106
|
+
if (evt !== undefined) handler(type, evt)
|
|
107
|
+
})
|
|
120
108
|
}
|
|
121
109
|
},
|
|
122
110
|
}
|
|
@@ -144,8 +132,8 @@ export default function mitt<Events extends Record<EventType, unknown>>(
|
|
|
144
132
|
* ```
|
|
145
133
|
*/
|
|
146
134
|
|
|
147
|
-
// TODO: need to create an action that auto generates this Events type from the ./app/
|
|
148
|
-
|
|
135
|
+
// TODO: need to create an action that auto generates this Events type from the ./app/Events
|
|
136
|
+
type StacksEvents = {
|
|
149
137
|
'user:registered': { name: string }
|
|
150
138
|
'user:logged-in': { name: string }
|
|
151
139
|
'user:logged-out': { name: string }
|
|
@@ -155,7 +143,7 @@ interface Events {
|
|
|
155
143
|
'user:password-changed': { name: string }
|
|
156
144
|
}
|
|
157
145
|
|
|
158
|
-
const events = mitt<
|
|
146
|
+
const events = mitt<StacksEvents>
|
|
159
147
|
const emitter = events()
|
|
160
148
|
const useEvent: typeof emitter.emit = emitter.emit.bind(emitter)
|
|
161
149
|
const useEvents = events()
|
package/dist/index.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
export type EventType = string | symbol;
|
|
2
|
-
export type Handler<T = unknown> = (event: T) => void;
|
|
3
|
-
export type WildcardHandler<T = Record<string, unknown>> = (type: keyof T, event: T[keyof T]) => void;
|
|
4
|
-
export type EventHandlerList<T = unknown> = Array<Handler<T>>;
|
|
5
|
-
export type WildCardEventHandlerList<T = Record<string, unknown>> = Array<WildcardHandler<T>>;
|
|
6
|
-
export type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<keyof Events | '*', EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>>;
|
|
7
|
-
export interface Emitter<Events extends Record<EventType, unknown>> {
|
|
8
|
-
all: EventHandlerMap<Events>;
|
|
9
|
-
on: (<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>) => void) & ((type: '*', handler: WildcardHandler<Events>) => void);
|
|
10
|
-
off: (<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>) => void) & ((type: '*', handler: WildcardHandler<Events>) => void);
|
|
11
|
-
emit: (<Key extends keyof Events>(type: Key, event: Events[Key]) => void) & (<Key extends keyof Events>(type: undefined extends Events[Key] ? Key : never) => void);
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* A tiny (~200b) functional event emitter / pubsub.
|
|
15
|
-
*/
|
|
16
|
-
export default function mitt<Events extends Record<EventType, unknown>>(all?: EventHandlerMap<Events>): Emitter<Events>;
|
|
17
|
-
declare const events: typeof mitt;
|
|
18
|
-
declare const emitter: Emitter<Record<EventType, unknown>>;
|
|
19
|
-
declare const useEvent: typeof emitter.emit;
|
|
20
|
-
declare const useEvents: Emitter<Record<EventType, unknown>>;
|
|
21
|
-
declare const dispatch: typeof emitter.emit;
|
|
22
|
-
declare const listen: typeof emitter.on;
|
|
23
|
-
declare const off: typeof emitter.off;
|
|
24
|
-
declare const all: typeof emitter.all;
|
|
25
|
-
export { all, dispatch, events, listen, mitt, off, useEvent, useEvents };
|