@stacksjs/events 0.64.5 → 0.65.0
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 +2 -2
- package/dist/index.js.map +3 -3
- package/package.json +8 -19
- package/src/index.ts +32 -21
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
function A(k){if(!k)k=new Map;return{all:k,on(w,j){const b=k.get(w);if(b)b.push(j);else k.set(w,[j])},off(w,j){const b=k.get(w);if(b)if(j){const z=b.indexOf(j);if(z>-1)b.splice(z,1)}else k.set(w,[])},emit(w,j){let b=k.get(w);if(b)b.slice().forEach((z)=>{if(j!==void 0)z(j)});if(b=k.get("*"),b)b.slice().forEach((z)=>{if(j!==void 0)z(w,j)})}}}var B=A,q=B
|
|
2
|
+
function A(k){if(!k)k=new Map;return{all:k,on(w,j){const b=k.get(w);if(b)b.push(j);else k.set(w,[j])},off(w,j){const b=k.get(w);if(b)if(j){const z=b.indexOf(j);if(z>-1)b.splice(z,1)}else k.set(w,[])},emit(w,j){let b=k.get(w);if(b)b.slice().forEach((z)=>{if(j!==void 0)z(j)});if(b=k.get("*"),b)b.slice().forEach((z)=>{if(j!==void 0)z(w,j)})}}}var B=A(),q=B,C=q.emit.bind(q),F=B,G=q.emit.bind(q),H=q.on.bind(q),I=q.off.bind(q),J=q.all;export{F as useEvents,C as useEvent,I as off,A as mitt,H as listen,B as events,G as dispatch,A as default,J as all};
|
|
3
3
|
|
|
4
|
-
//# debugId=
|
|
4
|
+
//# debugId=2911EB3D08A281C964756E2164756E21
|
|
5
5
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"// thanks to mitt for the base of this wonderful functional event emitter\n\nexport type EventType = string | symbol\n\n// An event handler can take an optional event argument\n// and should not return a value\nexport type Handler<T = unknown> = (event: T) => void\nexport type WildcardHandler<T = Record<string, unknown>> = (type: keyof T, event: T[keyof T]) => void\n\n// An array of all currently registered event handlers for a type\nexport type EventHandlerList<T = unknown> = Array<Handler<T>>\nexport type WildCardEventHandlerList<T = Record<string, unknown>> = Array<WildcardHandler<T>>\n\n// A map of event types and their corresponding event handlers.\nexport type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<\n keyof Events | '*',\n EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>\n>\n\nexport interface Emitter<Events extends Record<EventType, unknown>> {\n all: EventHandlerMap<Events>\n\n on: (<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>) => void) &\n ((type: '*', handler: WildcardHandler<Events>) => void)\n\n off: (<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>) => void) &\n ((type: '*', handler?: WildcardHandler<Events>) => void)\n\n emit: (<Key extends keyof Events>(type: Key, event: Events[Key]) => void) &\n (<Key extends keyof Events>(type: undefined extends Events[Key] ? Key : never) => void)\n}\n\n/**\n * Tiny (~200b) functional event emitter / pubsub.\n */\nexport default function mitt<Events extends Record<EventType, unknown>>(\n all?: EventHandlerMap<Events>,\n): Emitter<Events> {\n if (!all)
|
|
5
|
+
"// thanks to mitt for the base of this wonderful functional event emitter\n\nimport type { UserModel } from '../../../orm/src/models/User'\n\nexport type EventType = string | symbol\n\n// An event handler can take an optional event argument\n// and should not return a value\nexport type Handler<T = unknown> = (event: T) => void\nexport type WildcardHandler<T = Record<string, unknown>> = (type: keyof T, event: T[keyof T]) => void\n\n// An array of all currently registered event handlers for a type\nexport type EventHandlerList<T = unknown> = Array<Handler<T>>\nexport type WildCardEventHandlerList<T = Record<string, unknown>> = Array<WildcardHandler<T>>\n\n// A map of event types and their corresponding event handlers.\nexport type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<\n keyof Events | '*',\n EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>\n>\n\nexport interface Emitter<Events extends Record<EventType, unknown>> {\n all: EventHandlerMap<Events>\n\n on: (<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>) => void) &\n ((type: '*', handler: WildcardHandler<Events>) => void)\n\n off: (<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>) => void) &\n ((type: '*', handler?: WildcardHandler<Events>) => void)\n\n emit: (<Key extends keyof Events>(type: Key, event: Events[Key]) => void) &\n (<Key extends keyof Events>(type: undefined extends Events[Key] ? Key : never) => void)\n}\n\n/**\n * Tiny (~200b) functional event emitter / pubsub.\n */\nexport default function mitt<Events extends Record<EventType, unknown>>(\n all?: EventHandlerMap<Events>,\n): Emitter<Events> {\n if (!all)\n all = new Map()\n\n return {\n /**\n * A Map of event names to registered handler functions.\n */\n all,\n\n /**\n * Register an event handler for the given type.\n * @param {string|symbol} type Type of event to listen for, or `'*'` for all events\n * @param {Function} handler Function to call in response to given event\n * @memberOf mitt\n */\n on<Key extends keyof Events>(type: Key | '*', handler: Handler<Events[Key]> | WildcardHandler<Events>) {\n const handlers: Array<Handler<Events[Key]> | WildcardHandler<Events>> | undefined = (\n all as EventHandlerMap<Events>\n ).get(type)\n if (handlers) {\n handlers.push(handler as Handler<Events[Key]> | WildcardHandler<Events>)\n }\n // Explicitly assert the type of the handler array being set in the map. Unsure if there is a better way to do this\n else {\n (all as EventHandlerMap<Events>).set(type, [handler] as\n | EventHandlerList<Events[keyof Events]>\n | WildCardEventHandlerList<Events>)\n }\n },\n\n /**\n * Remove an event handler for the given type.\n * If `handler` is omitted, all handlers of the given type are removed.\n * @param {string|symbol} type Type of event to unregister `handler` from (`'*'` to remove a wildcard handler)\n * @param {Function} [handler] Handler function to remove\n * @memberOf mitt\n */\n off<Key extends keyof Events>(type: Key | '*', handler?: Handler<Events[Key]> | WildcardHandler<Events>) {\n const handlers: Array<Handler<Events[Key]> | WildcardHandler<Events>> | undefined = (\n all as EventHandlerMap<Events>\n ).get(type)\n if (handlers) {\n if (handler) {\n const index = handlers.indexOf(handler as Handler<Events[Key]> | WildcardHandler<Events>)\n if (index > -1)\n handlers.splice(index, 1)\n }\n else {\n ;(all as EventHandlerMap<Events>).set(type, [])\n }\n }\n },\n\n /**\n * Invoke all handlers for the given type.\n * If present, `'*'` handlers are invoked after type-matched handlers.\n *\n * Note: Manually firing '*' handlers is not supported.\n *\n * @param {string|symbol} type The event type to invoke\n * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler\n * @memberOf mitt\n */\n emit<Key extends keyof Events>(type: Key, evt?: Events[Key]) {\n let handlers = (all as EventHandlerMap<Events>).get(type)\n if (handlers) {\n ;(handlers as EventHandlerList<Events[keyof Events]>).slice().forEach((handler) => {\n if (evt !== undefined)\n handler(evt)\n })\n }\n handlers = (all as EventHandlerMap<Events>).get('*')\n if (handlers) {\n ;(handlers as WildCardEventHandlerList<Events>).slice().forEach((handler) => {\n if (evt !== undefined)\n handler(type, evt)\n })\n }\n },\n }\n}\n\n/**\n * This module provides a simple, yet powerful, event bus for the application.\n *\n * TODO: https://vitejs.dev/guide/api-plugin.html#typescript-for-custom-events\n *\n * @example To fire an event, you may use any of the following approaches:\n * ```ts\n * useEvent('user:registered', { name: 'Chris'})\n * dispatch('user:registered', { name: 'Chris'})\n *\n * // alternatively, you may use the following:\n * events.emit('user:registered', { name: 'Chris'})\n * useEvents.emit('user:registered', { name: 'Chris'})\n * ````\n *\n * @example To capture an event, you may use any of the following approaches:\n * ```ts\n * useListen('user:registered', (user) => console.log(user))\n * listen('user:registered', (user) => console.log(user))\n * ```\n */\n\n// TODO: need to create an action that auto generates this Events type from the ./app/Events\ninterface StacksEvents {\n 'user:registered': object\n 'user:logged-in': object\n 'user:logged-out': object\n 'user:updated': Partial<UserModel>\n 'user:created': Partial<UserModel>\n 'user:deleted': Partial<UserModel>\n 'user:password-reset': object\n 'user:password-changed': object\n}\n\nconst events: Emitter<StacksEvents> = mitt<StacksEvents>()\nconst emitter = events\nconst useEvent: typeof emitter.emit = emitter.emit.bind(emitter)\nconst useEvents = events\nconst dispatch: typeof emitter.emit = emitter.emit.bind(emitter)\n\nconst listen: typeof emitter.on = emitter.on.bind(emitter)\nconst off: typeof emitter.off = emitter.off.bind(emitter)\nconst all: typeof emitter.all = emitter.all\n\nexport { all, dispatch, events, listen, mitt, off, useEvent, useEvents }\n"
|
|
6
6
|
],
|
|
7
|
-
"mappings": ";
|
|
8
|
-
"debugId": "
|
|
7
|
+
"mappings": ";AAqCA,SAAwB,CAA+C,CACrE,EACiB,CACjB,IAAK,EACH,EAAM,IAAI,IAEZ,MAAO,CAIL,MAQA,EAA4B,CAAC,EAAiB,EAAyD,CACrG,MAAM,EACJ,EACA,IAAI,CAAI,EACV,GAAI,EACF,EAAS,KAAK,CAAyD,MAIvE,CAAC,EAAgC,IAAI,EAAM,CAAC,CAAO,CAEjB,GAWtC,GAA6B,CAAC,EAAiB,EAA0D,CACvG,MAAM,EACJ,EACA,IAAI,CAAI,EACV,GAAI,EACF,GAAI,EAAS,CACX,MAAM,EAAQ,EAAS,QAAQ,CAAyD,EACxF,GAAI,EAAQ,GACV,EAAS,OAAO,EAAO,CAAC,MAGzB,CAAC,EAAgC,IAAI,EAAM,CAAC,CAAC,GAepD,IAA8B,CAAC,EAAW,EAAmB,CAC3D,IAAI,EAAY,EAAgC,IAAI,CAAI,EACxD,GAAI,EACD,AAAC,EAAoD,MAAM,EAAE,QAAQ,CAAC,IAAY,CACjF,GAAI,IAAQ,OACV,EAAQ,CAAG,EACd,EAGH,GADA,EAAY,EAAgC,IAAI,GAAG,EAC/C,EACD,AAAC,EAA8C,MAAM,EAAE,QAAQ,CAAC,IAAY,CAC3E,GAAI,IAAQ,OACV,EAAQ,EAAM,CAAG,EACpB,EAGP,EAqCF,IAAM,EAAgC,EAAmB,EACnD,EAAU,EACV,EAAgC,EAAQ,KAAK,KAAK,CAAO,EACzD,EAAY,EACZ,EAAgC,EAAQ,KAAK,KAAK,CAAO,EAEzD,EAA4B,EAAQ,GAAG,KAAK,CAAO,EACnD,EAA0B,EAAQ,IAAI,KAAK,CAAO,EAClD,EAA0B,EAAQ",
|
|
8
|
+
"debugId": "2911EB3D08A281C964756E2164756E21",
|
|
9
9
|
"names": []
|
|
10
10
|
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/events",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.65.0",
|
|
5
5
|
"description": "Functional event emitting.",
|
|
6
6
|
"author": "Chris Breuer",
|
|
7
|
+
"contributors": ["Chris Breuer <chris@stacksjs.org>"],
|
|
7
8
|
"license": "MIT",
|
|
8
9
|
"funding": "https://github.com/sponsors/chrisbbreuer",
|
|
9
10
|
"homepage": "https://github.com/stacksjs/stacks/tree/main/storage/framework/core/events#readme",
|
|
@@ -15,13 +16,7 @@
|
|
|
15
16
|
"bugs": {
|
|
16
17
|
"url": "https://github.com/stacksjs/stacks/issues"
|
|
17
18
|
},
|
|
18
|
-
"keywords": [
|
|
19
|
-
"events",
|
|
20
|
-
"functional",
|
|
21
|
-
"functions",
|
|
22
|
-
"mitt",
|
|
23
|
-
"stacks"
|
|
24
|
-
],
|
|
19
|
+
"keywords": ["events", "functional", "functions", "mitt", "stacks"],
|
|
25
20
|
"exports": {
|
|
26
21
|
".": {
|
|
27
22
|
"bun": "./src/index.ts",
|
|
@@ -34,20 +29,14 @@
|
|
|
34
29
|
},
|
|
35
30
|
"module": "dist/index.js",
|
|
36
31
|
"types": "dist/index.d.ts",
|
|
37
|
-
"
|
|
38
|
-
"Chris Breuer <chris@stacksjs.org>"
|
|
39
|
-
],
|
|
40
|
-
"files": [
|
|
41
|
-
"README.md",
|
|
42
|
-
"dist",
|
|
43
|
-
"src"
|
|
44
|
-
],
|
|
32
|
+
"files": ["README.md", "dist", "src"],
|
|
45
33
|
"scripts": {
|
|
46
|
-
"build": "bun
|
|
47
|
-
"typecheck": "bun
|
|
34
|
+
"build": "bun build.ts",
|
|
35
|
+
"typecheck": "bun tsc --noEmit",
|
|
36
|
+
"test": "bun test.ts",
|
|
48
37
|
"prepublishOnly": "bun run build"
|
|
49
38
|
},
|
|
50
39
|
"devDependencies": {
|
|
51
|
-
"@stacksjs/development": "
|
|
40
|
+
"@stacksjs/development": "0.64.6"
|
|
52
41
|
}
|
|
53
42
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// thanks to mitt for the base of this wonderful functional event emitter
|
|
2
2
|
|
|
3
|
+
import type { UserModel } from '../../../orm/src/models/User'
|
|
4
|
+
|
|
3
5
|
export type EventType = string | symbol
|
|
4
6
|
|
|
5
7
|
// An event handler can take an optional event argument
|
|
@@ -36,7 +38,8 @@ export interface Emitter<Events extends Record<EventType, unknown>> {
|
|
|
36
38
|
export default function mitt<Events extends Record<EventType, unknown>>(
|
|
37
39
|
all?: EventHandlerMap<Events>,
|
|
38
40
|
): Emitter<Events> {
|
|
39
|
-
if (!all)
|
|
41
|
+
if (!all)
|
|
42
|
+
all = new Map()
|
|
40
43
|
|
|
41
44
|
return {
|
|
42
45
|
/**
|
|
@@ -54,12 +57,15 @@ export default function mitt<Events extends Record<EventType, unknown>>(
|
|
|
54
57
|
const handlers: Array<Handler<Events[Key]> | WildcardHandler<Events>> | undefined = (
|
|
55
58
|
all as EventHandlerMap<Events>
|
|
56
59
|
).get(type)
|
|
57
|
-
if (handlers)
|
|
60
|
+
if (handlers) {
|
|
61
|
+
handlers.push(handler as Handler<Events[Key]> | WildcardHandler<Events>)
|
|
62
|
+
}
|
|
58
63
|
// Explicitly assert the type of the handler array being set in the map. Unsure if there is a better way to do this
|
|
59
|
-
else
|
|
64
|
+
else {
|
|
60
65
|
(all as EventHandlerMap<Events>).set(type, [handler] as
|
|
61
|
-
|
|
62
|
-
|
|
66
|
+
| EventHandlerList<Events[keyof Events]>
|
|
67
|
+
| WildCardEventHandlerList<Events>)
|
|
68
|
+
}
|
|
63
69
|
},
|
|
64
70
|
|
|
65
71
|
/**
|
|
@@ -76,8 +82,10 @@ export default function mitt<Events extends Record<EventType, unknown>>(
|
|
|
76
82
|
if (handlers) {
|
|
77
83
|
if (handler) {
|
|
78
84
|
const index = handlers.indexOf(handler as Handler<Events[Key]> | WildcardHandler<Events>)
|
|
79
|
-
if (index > -1)
|
|
80
|
-
|
|
85
|
+
if (index > -1)
|
|
86
|
+
handlers.splice(index, 1)
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
81
89
|
;(all as EventHandlerMap<Events>).set(type, [])
|
|
82
90
|
}
|
|
83
91
|
}
|
|
@@ -97,13 +105,15 @@ export default function mitt<Events extends Record<EventType, unknown>>(
|
|
|
97
105
|
let handlers = (all as EventHandlerMap<Events>).get(type)
|
|
98
106
|
if (handlers) {
|
|
99
107
|
;(handlers as EventHandlerList<Events[keyof Events]>).slice().forEach((handler) => {
|
|
100
|
-
if (evt !== undefined)
|
|
108
|
+
if (evt !== undefined)
|
|
109
|
+
handler(evt)
|
|
101
110
|
})
|
|
102
111
|
}
|
|
103
112
|
handlers = (all as EventHandlerMap<Events>).get('*')
|
|
104
113
|
if (handlers) {
|
|
105
114
|
;(handlers as WildCardEventHandlerList<Events>).slice().forEach((handler) => {
|
|
106
|
-
if (evt !== undefined)
|
|
115
|
+
if (evt !== undefined)
|
|
116
|
+
handler(type, evt)
|
|
107
117
|
})
|
|
108
118
|
}
|
|
109
119
|
},
|
|
@@ -133,22 +143,23 @@ export default function mitt<Events extends Record<EventType, unknown>>(
|
|
|
133
143
|
*/
|
|
134
144
|
|
|
135
145
|
// TODO: need to create an action that auto generates this Events type from the ./app/Events
|
|
136
|
-
|
|
137
|
-
'user:registered':
|
|
138
|
-
'user:logged-in':
|
|
139
|
-
'user:logged-out':
|
|
140
|
-
'user:updated':
|
|
141
|
-
'user:created':
|
|
142
|
-
'user:deleted':
|
|
143
|
-
'user:password-reset':
|
|
144
|
-
'user:password-changed':
|
|
146
|
+
interface StacksEvents {
|
|
147
|
+
'user:registered': object
|
|
148
|
+
'user:logged-in': object
|
|
149
|
+
'user:logged-out': object
|
|
150
|
+
'user:updated': Partial<UserModel>
|
|
151
|
+
'user:created': Partial<UserModel>
|
|
152
|
+
'user:deleted': Partial<UserModel>
|
|
153
|
+
'user:password-reset': object
|
|
154
|
+
'user:password-changed': object
|
|
145
155
|
}
|
|
146
156
|
|
|
147
|
-
const events = mitt<StacksEvents>
|
|
148
|
-
const emitter = events
|
|
157
|
+
const events: Emitter<StacksEvents> = mitt<StacksEvents>()
|
|
158
|
+
const emitter = events
|
|
149
159
|
const useEvent: typeof emitter.emit = emitter.emit.bind(emitter)
|
|
150
|
-
const useEvents = events
|
|
160
|
+
const useEvents = events
|
|
151
161
|
const dispatch: typeof emitter.emit = emitter.emit.bind(emitter)
|
|
162
|
+
|
|
152
163
|
const listen: typeof emitter.on = emitter.on.bind(emitter)
|
|
153
164
|
const off: typeof emitter.off = emitter.off.bind(emitter)
|
|
154
165
|
const all: typeof emitter.all = emitter.all
|