@stacksjs/events 0.65.0 → 0.67.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 +1 -4
- package/package.json +3 -5
- package/dist/index.js.map +0 -10
- package/src/index.ts +0 -167
package/dist/index.js
CHANGED
|
@@ -1,5 +1,2 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
function A(k){if(!k)k=new Map;return{all:k,on(w,j){
|
|
3
|
-
|
|
4
|
-
//# debugId=2911EB3D08A281C964756E2164756E21
|
|
5
|
-
//# sourceMappingURL=index.js.map
|
|
2
|
+
function A(k){if(!k)k=new Map;return{all:k,on(w,j){let b=k.get(w);if(b)b.push(j);else k.set(w,[j])},off(w,j){let b=k.get(w);if(b)if(j){let 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};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/events",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.67.0",
|
|
5
5
|
"description": "Functional event emitting.",
|
|
6
6
|
"author": "Chris Breuer",
|
|
7
7
|
"contributors": ["Chris Breuer <chris@stacksjs.org>"],
|
|
@@ -19,17 +19,15 @@
|
|
|
19
19
|
"keywords": ["events", "functional", "functions", "mitt", "stacks"],
|
|
20
20
|
"exports": {
|
|
21
21
|
".": {
|
|
22
|
-
"bun": "./src/index.ts",
|
|
23
22
|
"import": "./dist/index.js"
|
|
24
23
|
},
|
|
25
24
|
"./*": {
|
|
26
|
-
"bun": "./src/*",
|
|
27
25
|
"import": "./dist/*"
|
|
28
26
|
}
|
|
29
27
|
},
|
|
30
28
|
"module": "dist/index.js",
|
|
31
29
|
"types": "dist/index.d.ts",
|
|
32
|
-
"files": ["README.md", "dist"
|
|
30
|
+
"files": ["README.md", "dist"],
|
|
33
31
|
"scripts": {
|
|
34
32
|
"build": "bun build.ts",
|
|
35
33
|
"typecheck": "bun tsc --noEmit",
|
|
@@ -37,6 +35,6 @@
|
|
|
37
35
|
"prepublishOnly": "bun run build"
|
|
38
36
|
},
|
|
39
37
|
"devDependencies": {
|
|
40
|
-
"@stacksjs/development": "0.
|
|
38
|
+
"@stacksjs/development": "0.67.0"
|
|
41
39
|
}
|
|
42
40
|
}
|
package/dist/index.js.map
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../src/index.ts"],
|
|
4
|
-
"sourcesContent": [
|
|
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
|
-
],
|
|
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
|
-
"names": []
|
|
10
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
// thanks to mitt for the base of this wonderful functional event emitter
|
|
2
|
-
|
|
3
|
-
import type { UserModel } from '../../../orm/src/models/User'
|
|
4
|
-
|
|
5
|
-
export type EventType = string | symbol
|
|
6
|
-
|
|
7
|
-
// An event handler can take an optional event argument
|
|
8
|
-
// and should not return a value
|
|
9
|
-
export type Handler<T = unknown> = (event: T) => void
|
|
10
|
-
export type WildcardHandler<T = Record<string, unknown>> = (type: keyof T, event: T[keyof T]) => void
|
|
11
|
-
|
|
12
|
-
// An array of all currently registered event handlers for a type
|
|
13
|
-
export type EventHandlerList<T = unknown> = Array<Handler<T>>
|
|
14
|
-
export type WildCardEventHandlerList<T = Record<string, unknown>> = Array<WildcardHandler<T>>
|
|
15
|
-
|
|
16
|
-
// A map of event types and their corresponding event handlers.
|
|
17
|
-
export type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<
|
|
18
|
-
keyof Events | '*',
|
|
19
|
-
EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>
|
|
20
|
-
>
|
|
21
|
-
|
|
22
|
-
export interface Emitter<Events extends Record<EventType, unknown>> {
|
|
23
|
-
all: EventHandlerMap<Events>
|
|
24
|
-
|
|
25
|
-
on: (<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>) => void) &
|
|
26
|
-
((type: '*', handler: WildcardHandler<Events>) => void)
|
|
27
|
-
|
|
28
|
-
off: (<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>) => void) &
|
|
29
|
-
((type: '*', handler?: WildcardHandler<Events>) => void)
|
|
30
|
-
|
|
31
|
-
emit: (<Key extends keyof Events>(type: Key, event: Events[Key]) => void) &
|
|
32
|
-
(<Key extends keyof Events>(type: undefined extends Events[Key] ? Key : never) => void)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Tiny (~200b) functional event emitter / pubsub.
|
|
37
|
-
*/
|
|
38
|
-
export default function mitt<Events extends Record<EventType, unknown>>(
|
|
39
|
-
all?: EventHandlerMap<Events>,
|
|
40
|
-
): Emitter<Events> {
|
|
41
|
-
if (!all)
|
|
42
|
-
all = new Map()
|
|
43
|
-
|
|
44
|
-
return {
|
|
45
|
-
/**
|
|
46
|
-
* A Map of event names to registered handler functions.
|
|
47
|
-
*/
|
|
48
|
-
all,
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Register an event handler for the given type.
|
|
52
|
-
* @param {string|symbol} type Type of event to listen for, or `'*'` for all events
|
|
53
|
-
* @param {Function} handler Function to call in response to given event
|
|
54
|
-
* @memberOf mitt
|
|
55
|
-
*/
|
|
56
|
-
on<Key extends keyof Events>(type: Key | '*', handler: Handler<Events[Key]> | WildcardHandler<Events>) {
|
|
57
|
-
const handlers: Array<Handler<Events[Key]> | WildcardHandler<Events>> | undefined = (
|
|
58
|
-
all as EventHandlerMap<Events>
|
|
59
|
-
).get(type)
|
|
60
|
-
if (handlers) {
|
|
61
|
-
handlers.push(handler as Handler<Events[Key]> | WildcardHandler<Events>)
|
|
62
|
-
}
|
|
63
|
-
// Explicitly assert the type of the handler array being set in the map. Unsure if there is a better way to do this
|
|
64
|
-
else {
|
|
65
|
-
(all as EventHandlerMap<Events>).set(type, [handler] as
|
|
66
|
-
| EventHandlerList<Events[keyof Events]>
|
|
67
|
-
| WildCardEventHandlerList<Events>)
|
|
68
|
-
}
|
|
69
|
-
},
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Remove an event handler for the given type.
|
|
73
|
-
* If `handler` is omitted, all handlers of the given type are removed.
|
|
74
|
-
* @param {string|symbol} type Type of event to unregister `handler` from (`'*'` to remove a wildcard handler)
|
|
75
|
-
* @param {Function} [handler] Handler function to remove
|
|
76
|
-
* @memberOf mitt
|
|
77
|
-
*/
|
|
78
|
-
off<Key extends keyof Events>(type: Key | '*', handler?: Handler<Events[Key]> | WildcardHandler<Events>) {
|
|
79
|
-
const handlers: Array<Handler<Events[Key]> | WildcardHandler<Events>> | undefined = (
|
|
80
|
-
all as EventHandlerMap<Events>
|
|
81
|
-
).get(type)
|
|
82
|
-
if (handlers) {
|
|
83
|
-
if (handler) {
|
|
84
|
-
const index = handlers.indexOf(handler as Handler<Events[Key]> | WildcardHandler<Events>)
|
|
85
|
-
if (index > -1)
|
|
86
|
-
handlers.splice(index, 1)
|
|
87
|
-
}
|
|
88
|
-
else {
|
|
89
|
-
;(all as EventHandlerMap<Events>).set(type, [])
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
},
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Invoke all handlers for the given type.
|
|
96
|
-
* If present, `'*'` handlers are invoked after type-matched handlers.
|
|
97
|
-
*
|
|
98
|
-
* Note: Manually firing '*' handlers is not supported.
|
|
99
|
-
*
|
|
100
|
-
* @param {string|symbol} type The event type to invoke
|
|
101
|
-
* @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler
|
|
102
|
-
* @memberOf mitt
|
|
103
|
-
*/
|
|
104
|
-
emit<Key extends keyof Events>(type: Key, evt?: Events[Key]) {
|
|
105
|
-
let handlers = (all as EventHandlerMap<Events>).get(type)
|
|
106
|
-
if (handlers) {
|
|
107
|
-
;(handlers as EventHandlerList<Events[keyof Events]>).slice().forEach((handler) => {
|
|
108
|
-
if (evt !== undefined)
|
|
109
|
-
handler(evt)
|
|
110
|
-
})
|
|
111
|
-
}
|
|
112
|
-
handlers = (all as EventHandlerMap<Events>).get('*')
|
|
113
|
-
if (handlers) {
|
|
114
|
-
;(handlers as WildCardEventHandlerList<Events>).slice().forEach((handler) => {
|
|
115
|
-
if (evt !== undefined)
|
|
116
|
-
handler(type, evt)
|
|
117
|
-
})
|
|
118
|
-
}
|
|
119
|
-
},
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* This module provides a simple, yet powerful, event bus for the application.
|
|
125
|
-
*
|
|
126
|
-
* TODO: https://vitejs.dev/guide/api-plugin.html#typescript-for-custom-events
|
|
127
|
-
*
|
|
128
|
-
* @example To fire an event, you may use any of the following approaches:
|
|
129
|
-
* ```ts
|
|
130
|
-
* useEvent('user:registered', { name: 'Chris'})
|
|
131
|
-
* dispatch('user:registered', { name: 'Chris'})
|
|
132
|
-
*
|
|
133
|
-
* // alternatively, you may use the following:
|
|
134
|
-
* events.emit('user:registered', { name: 'Chris'})
|
|
135
|
-
* useEvents.emit('user:registered', { name: 'Chris'})
|
|
136
|
-
* ````
|
|
137
|
-
*
|
|
138
|
-
* @example To capture an event, you may use any of the following approaches:
|
|
139
|
-
* ```ts
|
|
140
|
-
* useListen('user:registered', (user) => console.log(user))
|
|
141
|
-
* listen('user:registered', (user) => console.log(user))
|
|
142
|
-
* ```
|
|
143
|
-
*/
|
|
144
|
-
|
|
145
|
-
// TODO: need to create an action that auto generates this Events type from the ./app/Events
|
|
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
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
const events: Emitter<StacksEvents> = mitt<StacksEvents>()
|
|
158
|
-
const emitter = events
|
|
159
|
-
const useEvent: typeof emitter.emit = emitter.emit.bind(emitter)
|
|
160
|
-
const useEvents = events
|
|
161
|
-
const dispatch: typeof emitter.emit = emitter.emit.bind(emitter)
|
|
162
|
-
|
|
163
|
-
const listen: typeof emitter.on = emitter.on.bind(emitter)
|
|
164
|
-
const off: typeof emitter.off = emitter.off.bind(emitter)
|
|
165
|
-
const all: typeof emitter.all = emitter.all
|
|
166
|
-
|
|
167
|
-
export { all, dispatch, events, listen, mitt, off, useEvent, useEvents }
|