@craftile/event-bus 0.1.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/LICENSE.md +21 -0
- package/package.json +32 -0
- package/src/index.ts +100 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Craftile Team
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@craftile/event-bus",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Generic event bus utility for Craftile packages",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"craftile",
|
|
7
|
+
"event-bus",
|
|
8
|
+
"events",
|
|
9
|
+
"event-emitter"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"authors": [
|
|
13
|
+
{
|
|
14
|
+
"name": "Eldo Magan",
|
|
15
|
+
"email": "magan.eldo@gmail.com"
|
|
16
|
+
}
|
|
17
|
+
],
|
|
18
|
+
"type": "module",
|
|
19
|
+
"main": "src/index.ts",
|
|
20
|
+
"types": "src/index.ts",
|
|
21
|
+
"exports": "./src/index.ts",
|
|
22
|
+
"files": [
|
|
23
|
+
"src"
|
|
24
|
+
],
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/craftile/editor.git"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
export type EventListener<T = any> = (data: T) => void;
|
|
2
|
+
export type EventMap = {
|
|
3
|
+
[event: string]: any;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Type-safe EventBus with generic event map support
|
|
8
|
+
*
|
|
9
|
+
* @template T Event map interface that defines the events and their data types
|
|
10
|
+
*/
|
|
11
|
+
export class EventBus<T extends EventMap = EventMap> {
|
|
12
|
+
private listeners: Map<keyof T, Set<EventListener<any>>> = new Map();
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Add an event listener
|
|
16
|
+
*
|
|
17
|
+
* @param event Event name
|
|
18
|
+
* @param listener Event listener function
|
|
19
|
+
*
|
|
20
|
+
* @returns Cleanup function to remove the listener
|
|
21
|
+
*/
|
|
22
|
+
on<K extends keyof T>(event: K, listener: EventListener<T[K]>): () => void {
|
|
23
|
+
if (!this.listeners.has(event)) {
|
|
24
|
+
this.listeners.set(event, new Set());
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
this.listeners.get(event)!.add(listener);
|
|
28
|
+
|
|
29
|
+
return () => {
|
|
30
|
+
this.off(event, listener);
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Remove an event listener
|
|
36
|
+
*
|
|
37
|
+
* @param event Event name
|
|
38
|
+
* @param listener Event listener function to remove
|
|
39
|
+
*/
|
|
40
|
+
off<K extends keyof T>(event: K, listener: EventListener<T[K]>): void {
|
|
41
|
+
const eventListeners = this.listeners.get(event);
|
|
42
|
+
|
|
43
|
+
if (eventListeners) {
|
|
44
|
+
eventListeners.delete(listener);
|
|
45
|
+
|
|
46
|
+
if (eventListeners.size === 0) {
|
|
47
|
+
this.listeners.delete(event);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Add a one-time event listener
|
|
54
|
+
*
|
|
55
|
+
* @param event Event name
|
|
56
|
+
* @param listener Event listener function
|
|
57
|
+
*/
|
|
58
|
+
once<K extends keyof T>(event: K, listener: EventListener<T[K]>): void {
|
|
59
|
+
const onceWrapper = (data: T[K]) => {
|
|
60
|
+
listener(data);
|
|
61
|
+
this.off(event, onceWrapper);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
this.on(event, onceWrapper);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Emit an event
|
|
69
|
+
*
|
|
70
|
+
* @param event Event name
|
|
71
|
+
* @param args Event data (optional, depending on event type)
|
|
72
|
+
*/
|
|
73
|
+
emit<K extends keyof T>(event: K, ...args: T[K] extends void ? [] : [T[K]]): void {
|
|
74
|
+
const eventListeners = this.listeners.get(event);
|
|
75
|
+
|
|
76
|
+
if (eventListeners) {
|
|
77
|
+
const data = args[0];
|
|
78
|
+
eventListeners.forEach((listener) => {
|
|
79
|
+
try {
|
|
80
|
+
listener(data);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
console.error(`Error in event listener for '${String(event)}':`, error);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Remove all listeners for an event, or all listeners if no event specified
|
|
90
|
+
*
|
|
91
|
+
* @param event Optional event name. If not provided, removes all listeners
|
|
92
|
+
*/
|
|
93
|
+
removeAllListeners<K extends keyof T>(event?: K): void {
|
|
94
|
+
if (event) {
|
|
95
|
+
this.listeners.delete(event);
|
|
96
|
+
} else {
|
|
97
|
+
this.listeners.clear();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|