@pixotope/event-bus 0.3.0 → 0.5.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/.turbo/turbo-build.log +10 -10
- package/.turbo/turbo-check-types.log +1 -1
- package/.turbo/turbo-test.log +5 -5
- package/CHANGELOG.md +12 -0
- package/dist/index.cjs +21 -25
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +27 -5
- package/dist/index.d.ts +27 -5
- package/dist/index.js +20 -23
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/EventBus.test.ts +70 -43
- package/src/EventBus.ts +48 -35
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
> @pixotope/event-bus@0.
|
|
2
|
+
> @pixotope/event-bus@0.5.0 build /home/circleci/project/packages/event-bus
|
|
3
3
|
> tsup src/index.ts --format cjs,esm --dts --sourcemap
|
|
4
4
|
|
|
5
5
|
[34mCLI[39m Building entry: src/index.ts
|
|
@@ -8,13 +8,13 @@
|
|
|
8
8
|
[34mCLI[39m Target: es2020
|
|
9
9
|
[34mCJS[39m Build start
|
|
10
10
|
[34mESM[39m Build start
|
|
11
|
-
[
|
|
12
|
-
[
|
|
13
|
-
[
|
|
14
|
-
[
|
|
15
|
-
[
|
|
16
|
-
[
|
|
11
|
+
[32mESM[39m [1mdist/index.js [22m[32m1.40 KB[39m
|
|
12
|
+
[32mESM[39m [1mdist/index.js.map [22m[32m3.68 KB[39m
|
|
13
|
+
[32mESM[39m ⚡️ Build success in 894ms
|
|
14
|
+
[32mCJS[39m [1mdist/index.cjs [22m[32m2.36 KB[39m
|
|
15
|
+
[32mCJS[39m [1mdist/index.cjs.map [22m[32m3.77 KB[39m
|
|
16
|
+
[32mCJS[39m ⚡️ Build success in 895ms
|
|
17
17
|
[34mDTS[39m Build start
|
|
18
|
-
[32mDTS[39m ⚡️ Build success in
|
|
19
|
-
[32mDTS[39m [1mdist/index.d.cts [22m[32m1.
|
|
20
|
-
[32mDTS[39m [1mdist/index.d.ts [22m[32m1.
|
|
18
|
+
[32mDTS[39m ⚡️ Build success in 2035ms
|
|
19
|
+
[32mDTS[39m [1mdist/index.d.cts [22m[32m1.95 KB[39m
|
|
20
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m1.95 KB[39m
|
package/.turbo/turbo-test.log
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
|
|
2
|
-
> @pixotope/event-bus@0.
|
|
2
|
+
> @pixotope/event-bus@0.5.0 test /home/circleci/project/packages/event-bus
|
|
3
3
|
> vitest
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
[1m[46m RUN [49m[22m [36mv3.2.4 [39m[90m/home/circleci/project/packages/event-bus[39m
|
|
7
7
|
|
|
8
|
-
[32m✓[39m src/EventBus.test.ts [2m([22m[
|
|
8
|
+
[32m✓[39m src/EventBus.test.ts [2m([22m[2m8 tests[22m[2m)[22m[32m 9[2mms[22m[39m
|
|
9
9
|
|
|
10
10
|
[2m Test Files [22m [1m[32m1 passed[39m[22m[90m (1)[39m
|
|
11
|
-
[2m Tests [22m [1m[
|
|
12
|
-
[2m Start at [22m
|
|
13
|
-
[2m Duration [22m
|
|
11
|
+
[2m Tests [22m [1m[32m8 passed[39m[22m[90m (8)[39m
|
|
12
|
+
[2m Start at [22m 18:12:21
|
|
13
|
+
[2m Duration [22m 4.50s[2m (transform 473ms, setup 0ms, collect 298ms, tests 9ms, environment 0ms, prepare 1.32s)[22m
|
|
14
14
|
|
package/CHANGELOG.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -22,8 +22,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
22
22
|
// src/index.ts
|
|
23
23
|
var index_exports = {};
|
|
24
24
|
__export(index_exports, {
|
|
25
|
-
EventBus: () => EventBus
|
|
26
|
-
createEventBus: () => createEventBus
|
|
25
|
+
EventBus: () => EventBus
|
|
27
26
|
});
|
|
28
27
|
module.exports = __toCommonJS(index_exports);
|
|
29
28
|
|
|
@@ -42,42 +41,39 @@ var EventBus = class {
|
|
|
42
41
|
}
|
|
43
42
|
return () => this.off(type, handler);
|
|
44
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Remove an event handler
|
|
46
|
+
* @param type - The event type
|
|
47
|
+
* @param handler - The event handler
|
|
48
|
+
*/
|
|
45
49
|
off(type, handler) {
|
|
46
|
-
|
|
47
|
-
if (handlers) {
|
|
48
|
-
if (handler) {
|
|
49
|
-
handlers.delete(handler);
|
|
50
|
-
} else {
|
|
51
|
-
this.all.set(type, /* @__PURE__ */ new Set());
|
|
52
|
-
}
|
|
53
|
-
}
|
|
50
|
+
return this.all.get(type)?.delete(handler) ?? false;
|
|
54
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* Emit an event
|
|
54
|
+
* @param type - The event type
|
|
55
|
+
* @param args - The event arguments
|
|
56
|
+
*/
|
|
55
57
|
emit(type, ...args) {
|
|
56
58
|
const evt = args[0];
|
|
57
59
|
let handlers = this.all.get(type);
|
|
58
60
|
if (handlers) {
|
|
59
|
-
handlers.forEach(
|
|
60
|
-
(h) => h(evt)
|
|
61
|
-
);
|
|
61
|
+
handlers.forEach((h) => h(evt));
|
|
62
62
|
}
|
|
63
63
|
handlers = this.all.get("*");
|
|
64
64
|
if (handlers) {
|
|
65
|
-
handlers.forEach(
|
|
66
|
-
(h) => h(type, evt)
|
|
67
|
-
);
|
|
65
|
+
handlers.forEach((h) => h(type, evt));
|
|
68
66
|
}
|
|
69
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* Clear all event handlers
|
|
70
|
+
*/
|
|
71
|
+
dispose() {
|
|
72
|
+
this.all.clear();
|
|
73
|
+
}
|
|
70
74
|
};
|
|
71
|
-
function createEventBus(defaults = {}) {
|
|
72
|
-
return new EventBus(
|
|
73
|
-
new Map(
|
|
74
|
-
Object.entries(defaults).map(([key, value]) => [key, new Set(value)])
|
|
75
|
-
)
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
75
|
// Annotate the CommonJS export names for ESM import in node:
|
|
79
76
|
0 && (module.exports = {
|
|
80
|
-
EventBus
|
|
81
|
-
createEventBus
|
|
77
|
+
EventBus
|
|
82
78
|
});
|
|
83
79
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/EventBus.ts"],"sourcesContent":["export * from \"./EventBus\";\n","export type EventType = string | symbol;\n\nexport type EmitArgs<\n Events extends Record<EventType, unknown>,\n Key extends keyof Events,\n> = undefined extends Events[Key] ? [evt?: Events[Key]] : [evt: Events[Key]];\n\nexport type Handler<T = unknown> = (event: T) => void;\nexport type WildcardHandler<T = Record<string, unknown>> = (\n type: keyof T,\n event: T[keyof T]\n) => void;\n\nexport type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<\n keyof Events | \"*\",\n Set<GenericEventHandler<Events>>\n>;\n\
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/EventBus.ts"],"sourcesContent":["export * from \"./EventBus\";\n","export type EventType = string | symbol;\n\nexport type EmitArgs<\n Events extends Record<EventType, unknown>,\n Key extends keyof Events,\n> = undefined extends Events[Key] ? [evt?: Events[Key]] : [evt: Events[Key]];\n\nexport type Handler<T = unknown> = (event: T) => void;\nexport type WildcardHandler<T = Record<string, unknown>> = (\n type: keyof T,\n event: T[keyof T]\n) => void;\n\nexport type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<\n keyof Events | \"*\",\n Set<GenericEventHandler<Events>>\n>;\n\nexport type GenericEventHandler<Events extends Record<EventType, unknown>> =\n | Handler<Events[keyof Events]>\n | WildcardHandler<Events>;\n\ntype IsLooseEvents<Events extends Record<EventType, unknown>> =\n string extends keyof Events ? true : false;\n\nexport class EventBus<\n Events extends Record<EventType, unknown> = Record<EventType, unknown>,\n> {\n public readonly all: EventHandlerMap<Events> = new Map();\n\n public constructor(all?: EventHandlerMap<Events>) {\n this.all = all || new Map();\n }\n\n /**\n * Add an event handler\n * @param type - The event type\n * @param handler - The event handler\n * @returns A function to remove the event handler\n */\n public on<Key extends keyof Events>(\n type: Key,\n handler: Handler<Events[Key]>\n ): () => void;\n public on(type: \"*\", handler: WildcardHandler<Events>): () => void;\n public on<T extends string>(\n type: IsLooseEvents<Events> extends true ? T : never,\n handler: IsLooseEvents<Events> extends true\n ? (...args: any[]) => void\n : never\n ): () => void;\n public on(type: keyof Events | \"*\", handler: GenericEventHandler<Events>) {\n const handlers = this.all.get(type);\n if (handlers) {\n handlers.add(handler);\n } else {\n this.all.set(type, new Set([handler]));\n }\n\n return () => this.off(type, handler);\n }\n\n /**\n * Remove an event handler\n * @param type - The event type\n * @param handler - The event handler\n */\n public off(\n type: keyof Events | \"*\",\n handler: (...args: any[]) => any\n ): boolean {\n return this.all.get(type)?.delete(handler) ?? false;\n }\n\n /**\n * Emit an event\n * @param type - The event type\n * @param args - The event arguments\n */\n public emit<Key extends keyof Events>(\n type: Key,\n ...args: EmitArgs<Events, Key>\n ) {\n const evt = args[0] as Events[Key];\n let handlers = this.all.get(type);\n if (handlers) {\n (handlers as Set<Handler<Events[Key]>>).forEach((h) => h(evt!));\n }\n\n handlers = this.all.get(\"*\");\n if (handlers) {\n (handlers as Set<WildcardHandler<Events>>).forEach((h) => h(type, evt!));\n }\n }\n\n /**\n * Clear all event handlers\n */\n public dispose() {\n this.all.clear();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACyBO,IAAM,WAAN,MAEL;AAAA,EAGO,YAAY,KAA+B;AAFlD,wBAAgB,OAA+B,oBAAI,IAAI;AAGrD,SAAK,MAAM,OAAO,oBAAI,IAAI;AAAA,EAC5B;AAAA,EAmBO,GAAG,MAA0B,SAAsC;AACxE,UAAM,WAAW,KAAK,IAAI,IAAI,IAAI;AAClC,QAAI,UAAU;AACZ,eAAS,IAAI,OAAO;AAAA,IACtB,OAAO;AACL,WAAK,IAAI,IAAI,MAAM,oBAAI,IAAI,CAAC,OAAO,CAAC,CAAC;AAAA,IACvC;AAEA,WAAO,MAAM,KAAK,IAAI,MAAM,OAAO;AAAA,EACrC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOO,IACL,MACA,SACS;AACT,WAAO,KAAK,IAAI,IAAI,IAAI,GAAG,OAAO,OAAO,KAAK;AAAA,EAChD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOO,KACL,SACG,MACH;AACA,UAAM,MAAM,KAAK,CAAC;AAClB,QAAI,WAAW,KAAK,IAAI,IAAI,IAAI;AAChC,QAAI,UAAU;AACZ,MAAC,SAAuC,QAAQ,CAAC,MAAM,EAAE,GAAI,CAAC;AAAA,IAChE;AAEA,eAAW,KAAK,IAAI,IAAI,GAAG;AAC3B,QAAI,UAAU;AACZ,MAAC,SAA0C,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAI,CAAC;AAAA,IACzE;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKO,UAAU;AACf,SAAK,IAAI,MAAM;AAAA,EACjB;AACF;","names":[]}
|
package/dist/index.d.cts
CHANGED
|
@@ -4,13 +4,35 @@ type Handler<T = unknown> = (event: T) => void;
|
|
|
4
4
|
type WildcardHandler<T = Record<string, unknown>> = (type: keyof T, event: T[keyof T]) => void;
|
|
5
5
|
type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<keyof Events | "*", Set<GenericEventHandler<Events>>>;
|
|
6
6
|
type GenericEventHandler<Events extends Record<EventType, unknown>> = Handler<Events[keyof Events]> | WildcardHandler<Events>;
|
|
7
|
-
|
|
7
|
+
type IsLooseEvents<Events extends Record<EventType, unknown>> = string extends keyof Events ? true : false;
|
|
8
|
+
declare class EventBus<Events extends Record<EventType, unknown> = Record<EventType, unknown>> {
|
|
8
9
|
readonly all: EventHandlerMap<Events>;
|
|
9
10
|
constructor(all?: EventHandlerMap<Events>);
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Add an event handler
|
|
13
|
+
* @param type - The event type
|
|
14
|
+
* @param handler - The event handler
|
|
15
|
+
* @returns A function to remove the event handler
|
|
16
|
+
*/
|
|
17
|
+
on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): () => void;
|
|
18
|
+
on(type: "*", handler: WildcardHandler<Events>): () => void;
|
|
19
|
+
on<T extends string>(type: IsLooseEvents<Events> extends true ? T : never, handler: IsLooseEvents<Events> extends true ? (...args: any[]) => void : never): () => void;
|
|
20
|
+
/**
|
|
21
|
+
* Remove an event handler
|
|
22
|
+
* @param type - The event type
|
|
23
|
+
* @param handler - The event handler
|
|
24
|
+
*/
|
|
25
|
+
off(type: keyof Events | "*", handler: (...args: any[]) => any): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Emit an event
|
|
28
|
+
* @param type - The event type
|
|
29
|
+
* @param args - The event arguments
|
|
30
|
+
*/
|
|
12
31
|
emit<Key extends keyof Events>(type: Key, ...args: EmitArgs<Events, Key>): void;
|
|
32
|
+
/**
|
|
33
|
+
* Clear all event handlers
|
|
34
|
+
*/
|
|
35
|
+
dispose(): void;
|
|
13
36
|
}
|
|
14
|
-
declare function createEventBus<Events extends Record<EventType, unknown>>(defaults?: Record<keyof Events, GenericEventHandler<Events>[]>): EventBus<Events>;
|
|
15
37
|
|
|
16
|
-
export { type EmitArgs, EventBus, type EventHandlerMap, type EventType, type Handler, type WildcardHandler
|
|
38
|
+
export { type EmitArgs, EventBus, type EventHandlerMap, type EventType, type GenericEventHandler, type Handler, type WildcardHandler };
|
package/dist/index.d.ts
CHANGED
|
@@ -4,13 +4,35 @@ type Handler<T = unknown> = (event: T) => void;
|
|
|
4
4
|
type WildcardHandler<T = Record<string, unknown>> = (type: keyof T, event: T[keyof T]) => void;
|
|
5
5
|
type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<keyof Events | "*", Set<GenericEventHandler<Events>>>;
|
|
6
6
|
type GenericEventHandler<Events extends Record<EventType, unknown>> = Handler<Events[keyof Events]> | WildcardHandler<Events>;
|
|
7
|
-
|
|
7
|
+
type IsLooseEvents<Events extends Record<EventType, unknown>> = string extends keyof Events ? true : false;
|
|
8
|
+
declare class EventBus<Events extends Record<EventType, unknown> = Record<EventType, unknown>> {
|
|
8
9
|
readonly all: EventHandlerMap<Events>;
|
|
9
10
|
constructor(all?: EventHandlerMap<Events>);
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Add an event handler
|
|
13
|
+
* @param type - The event type
|
|
14
|
+
* @param handler - The event handler
|
|
15
|
+
* @returns A function to remove the event handler
|
|
16
|
+
*/
|
|
17
|
+
on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): () => void;
|
|
18
|
+
on(type: "*", handler: WildcardHandler<Events>): () => void;
|
|
19
|
+
on<T extends string>(type: IsLooseEvents<Events> extends true ? T : never, handler: IsLooseEvents<Events> extends true ? (...args: any[]) => void : never): () => void;
|
|
20
|
+
/**
|
|
21
|
+
* Remove an event handler
|
|
22
|
+
* @param type - The event type
|
|
23
|
+
* @param handler - The event handler
|
|
24
|
+
*/
|
|
25
|
+
off(type: keyof Events | "*", handler: (...args: any[]) => any): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Emit an event
|
|
28
|
+
* @param type - The event type
|
|
29
|
+
* @param args - The event arguments
|
|
30
|
+
*/
|
|
12
31
|
emit<Key extends keyof Events>(type: Key, ...args: EmitArgs<Events, Key>): void;
|
|
32
|
+
/**
|
|
33
|
+
* Clear all event handlers
|
|
34
|
+
*/
|
|
35
|
+
dispose(): void;
|
|
13
36
|
}
|
|
14
|
-
declare function createEventBus<Events extends Record<EventType, unknown>>(defaults?: Record<keyof Events, GenericEventHandler<Events>[]>): EventBus<Events>;
|
|
15
37
|
|
|
16
|
-
export { type EmitArgs, EventBus, type EventHandlerMap, type EventType, type Handler, type WildcardHandler
|
|
38
|
+
export { type EmitArgs, EventBus, type EventHandlerMap, type EventType, type GenericEventHandler, type Handler, type WildcardHandler };
|
package/dist/index.js
CHANGED
|
@@ -17,41 +17,38 @@ var EventBus = class {
|
|
|
17
17
|
}
|
|
18
18
|
return () => this.off(type, handler);
|
|
19
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* Remove an event handler
|
|
22
|
+
* @param type - The event type
|
|
23
|
+
* @param handler - The event handler
|
|
24
|
+
*/
|
|
20
25
|
off(type, handler) {
|
|
21
|
-
|
|
22
|
-
if (handlers) {
|
|
23
|
-
if (handler) {
|
|
24
|
-
handlers.delete(handler);
|
|
25
|
-
} else {
|
|
26
|
-
this.all.set(type, /* @__PURE__ */ new Set());
|
|
27
|
-
}
|
|
28
|
-
}
|
|
26
|
+
return this.all.get(type)?.delete(handler) ?? false;
|
|
29
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Emit an event
|
|
30
|
+
* @param type - The event type
|
|
31
|
+
* @param args - The event arguments
|
|
32
|
+
*/
|
|
30
33
|
emit(type, ...args) {
|
|
31
34
|
const evt = args[0];
|
|
32
35
|
let handlers = this.all.get(type);
|
|
33
36
|
if (handlers) {
|
|
34
|
-
handlers.forEach(
|
|
35
|
-
(h) => h(evt)
|
|
36
|
-
);
|
|
37
|
+
handlers.forEach((h) => h(evt));
|
|
37
38
|
}
|
|
38
39
|
handlers = this.all.get("*");
|
|
39
40
|
if (handlers) {
|
|
40
|
-
handlers.forEach(
|
|
41
|
-
(h) => h(type, evt)
|
|
42
|
-
);
|
|
41
|
+
handlers.forEach((h) => h(type, evt));
|
|
43
42
|
}
|
|
44
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Clear all event handlers
|
|
46
|
+
*/
|
|
47
|
+
dispose() {
|
|
48
|
+
this.all.clear();
|
|
49
|
+
}
|
|
45
50
|
};
|
|
46
|
-
function createEventBus(defaults = {}) {
|
|
47
|
-
return new EventBus(
|
|
48
|
-
new Map(
|
|
49
|
-
Object.entries(defaults).map(([key, value]) => [key, new Set(value)])
|
|
50
|
-
)
|
|
51
|
-
);
|
|
52
|
-
}
|
|
53
51
|
export {
|
|
54
|
-
EventBus
|
|
55
|
-
createEventBus
|
|
52
|
+
EventBus
|
|
56
53
|
};
|
|
57
54
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/EventBus.ts"],"sourcesContent":["export type EventType = string | symbol;\n\nexport type EmitArgs<\n Events extends Record<EventType, unknown>,\n Key extends keyof Events,\n> = undefined extends Events[Key] ? [evt?: Events[Key]] : [evt: Events[Key]];\n\nexport type Handler<T = unknown> = (event: T) => void;\nexport type WildcardHandler<T = Record<string, unknown>> = (\n type: keyof T,\n event: T[keyof T]\n) => void;\n\nexport type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<\n keyof Events | \"*\",\n Set<GenericEventHandler<Events>>\n>;\n\
|
|
1
|
+
{"version":3,"sources":["../src/EventBus.ts"],"sourcesContent":["export type EventType = string | symbol;\n\nexport type EmitArgs<\n Events extends Record<EventType, unknown>,\n Key extends keyof Events,\n> = undefined extends Events[Key] ? [evt?: Events[Key]] : [evt: Events[Key]];\n\nexport type Handler<T = unknown> = (event: T) => void;\nexport type WildcardHandler<T = Record<string, unknown>> = (\n type: keyof T,\n event: T[keyof T]\n) => void;\n\nexport type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<\n keyof Events | \"*\",\n Set<GenericEventHandler<Events>>\n>;\n\nexport type GenericEventHandler<Events extends Record<EventType, unknown>> =\n | Handler<Events[keyof Events]>\n | WildcardHandler<Events>;\n\ntype IsLooseEvents<Events extends Record<EventType, unknown>> =\n string extends keyof Events ? true : false;\n\nexport class EventBus<\n Events extends Record<EventType, unknown> = Record<EventType, unknown>,\n> {\n public readonly all: EventHandlerMap<Events> = new Map();\n\n public constructor(all?: EventHandlerMap<Events>) {\n this.all = all || new Map();\n }\n\n /**\n * Add an event handler\n * @param type - The event type\n * @param handler - The event handler\n * @returns A function to remove the event handler\n */\n public on<Key extends keyof Events>(\n type: Key,\n handler: Handler<Events[Key]>\n ): () => void;\n public on(type: \"*\", handler: WildcardHandler<Events>): () => void;\n public on<T extends string>(\n type: IsLooseEvents<Events> extends true ? T : never,\n handler: IsLooseEvents<Events> extends true\n ? (...args: any[]) => void\n : never\n ): () => void;\n public on(type: keyof Events | \"*\", handler: GenericEventHandler<Events>) {\n const handlers = this.all.get(type);\n if (handlers) {\n handlers.add(handler);\n } else {\n this.all.set(type, new Set([handler]));\n }\n\n return () => this.off(type, handler);\n }\n\n /**\n * Remove an event handler\n * @param type - The event type\n * @param handler - The event handler\n */\n public off(\n type: keyof Events | \"*\",\n handler: (...args: any[]) => any\n ): boolean {\n return this.all.get(type)?.delete(handler) ?? false;\n }\n\n /**\n * Emit an event\n * @param type - The event type\n * @param args - The event arguments\n */\n public emit<Key extends keyof Events>(\n type: Key,\n ...args: EmitArgs<Events, Key>\n ) {\n const evt = args[0] as Events[Key];\n let handlers = this.all.get(type);\n if (handlers) {\n (handlers as Set<Handler<Events[Key]>>).forEach((h) => h(evt!));\n }\n\n handlers = this.all.get(\"*\");\n if (handlers) {\n (handlers as Set<WildcardHandler<Events>>).forEach((h) => h(type, evt!));\n }\n }\n\n /**\n * Clear all event handlers\n */\n public dispose() {\n this.all.clear();\n }\n}\n"],"mappings":";;;;;AAyBO,IAAM,WAAN,MAEL;AAAA,EAGO,YAAY,KAA+B;AAFlD,wBAAgB,OAA+B,oBAAI,IAAI;AAGrD,SAAK,MAAM,OAAO,oBAAI,IAAI;AAAA,EAC5B;AAAA,EAmBO,GAAG,MAA0B,SAAsC;AACxE,UAAM,WAAW,KAAK,IAAI,IAAI,IAAI;AAClC,QAAI,UAAU;AACZ,eAAS,IAAI,OAAO;AAAA,IACtB,OAAO;AACL,WAAK,IAAI,IAAI,MAAM,oBAAI,IAAI,CAAC,OAAO,CAAC,CAAC;AAAA,IACvC;AAEA,WAAO,MAAM,KAAK,IAAI,MAAM,OAAO;AAAA,EACrC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOO,IACL,MACA,SACS;AACT,WAAO,KAAK,IAAI,IAAI,IAAI,GAAG,OAAO,OAAO,KAAK;AAAA,EAChD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOO,KACL,SACG,MACH;AACA,UAAM,MAAM,KAAK,CAAC;AAClB,QAAI,WAAW,KAAK,IAAI,IAAI,IAAI;AAChC,QAAI,UAAU;AACZ,MAAC,SAAuC,QAAQ,CAAC,MAAM,EAAE,GAAI,CAAC;AAAA,IAChE;AAEA,eAAW,KAAK,IAAI,IAAI,GAAG;AAC3B,QAAI,UAAU;AACZ,MAAC,SAA0C,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAI,CAAC;AAAA,IACzE;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKO,UAAU;AACf,SAAK,IAAI,MAAM;AAAA,EACjB;AACF;","names":[]}
|
package/package.json
CHANGED
package/src/EventBus.test.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, it, expect, vi } from "vitest";
|
|
2
|
-
import { EventBus
|
|
2
|
+
import { EventBus } from "./EventBus";
|
|
3
3
|
|
|
4
4
|
describe("EventBus", () => {
|
|
5
5
|
it("should create an event bus", () => {
|
|
@@ -11,8 +11,8 @@ describe("EventBus", () => {
|
|
|
11
11
|
const bus = new EventBus();
|
|
12
12
|
const handler = vi.fn();
|
|
13
13
|
bus.on("test", handler);
|
|
14
|
-
bus.emit("test");
|
|
15
|
-
expect(handler).
|
|
14
|
+
bus.emit("test", { payload: "test" });
|
|
15
|
+
expect(handler).toHaveBeenCalledWith({ payload: "test" });
|
|
16
16
|
});
|
|
17
17
|
|
|
18
18
|
it("should be able to off an event", () => {
|
|
@@ -24,24 +24,6 @@ describe("EventBus", () => {
|
|
|
24
24
|
expect(handler).not.toHaveBeenCalled();
|
|
25
25
|
});
|
|
26
26
|
|
|
27
|
-
it("should be able to off all events", () => {
|
|
28
|
-
const bus = new EventBus();
|
|
29
|
-
const handler = vi.fn();
|
|
30
|
-
bus.on("test", handler);
|
|
31
|
-
bus.off("test");
|
|
32
|
-
bus.emit("test");
|
|
33
|
-
expect(handler).not.toHaveBeenCalled();
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it("should be able to off a specific event", () => {
|
|
37
|
-
const bus = new EventBus();
|
|
38
|
-
const handler = vi.fn();
|
|
39
|
-
bus.on("test", handler);
|
|
40
|
-
bus.off("test", handler);
|
|
41
|
-
bus.emit("test");
|
|
42
|
-
expect(handler).not.toHaveBeenCalled();
|
|
43
|
-
});
|
|
44
|
-
|
|
45
27
|
it("should be able to emit an event with a wildcard handler", () => {
|
|
46
28
|
const bus = new EventBus();
|
|
47
29
|
const handler = vi.fn();
|
|
@@ -58,36 +40,81 @@ describe("EventBus", () => {
|
|
|
58
40
|
bus.emit("test");
|
|
59
41
|
expect(handler).not.toHaveBeenCalled();
|
|
60
42
|
});
|
|
61
|
-
});
|
|
62
43
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const
|
|
66
|
-
|
|
44
|
+
it("should be able to achieve once functionality", () => {
|
|
45
|
+
const bus = new EventBus();
|
|
46
|
+
const handler = vi.fn();
|
|
47
|
+
const off = bus.on("test", () => {
|
|
48
|
+
handler();
|
|
49
|
+
off();
|
|
50
|
+
});
|
|
51
|
+
bus.emit("test");
|
|
52
|
+
bus.emit("test");
|
|
53
|
+
expect(handler).toHaveBeenCalledTimes(1);
|
|
67
54
|
});
|
|
68
|
-
|
|
55
|
+
|
|
56
|
+
it("should work with correct event types", () => {
|
|
57
|
+
const bus = new EventBus<{
|
|
58
|
+
test: { payload: string };
|
|
59
|
+
test2: { payload: number };
|
|
60
|
+
test3: { payload: boolean };
|
|
61
|
+
}>();
|
|
69
62
|
const handler = vi.fn();
|
|
70
63
|
const handler2 = vi.fn();
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
"
|
|
76
|
-
}>({
|
|
77
|
-
test: [handler],
|
|
78
|
-
test2: [handler2],
|
|
79
|
-
"*": [catchAllHandler],
|
|
64
|
+
|
|
65
|
+
bus.on("test", handler);
|
|
66
|
+
bus.on("test", ({ payload }) => {
|
|
67
|
+
expect(payload).toBeTypeOf("string");
|
|
68
|
+
expect(payload).toBe("test");
|
|
80
69
|
});
|
|
81
|
-
|
|
82
|
-
|
|
70
|
+
bus.on("test2", ({ payload }) => {
|
|
71
|
+
expect(payload).toBeTypeOf("number");
|
|
72
|
+
expect(payload).toBe(1);
|
|
73
|
+
});
|
|
74
|
+
bus.on("test2", handler2);
|
|
75
|
+
bus.on("test3", ({ payload }) => {
|
|
76
|
+
expect(payload).toBeTypeOf("boolean");
|
|
77
|
+
expect(payload).toBe(true);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
bus.emit("test", { payload: "test" });
|
|
81
|
+
bus.emit("test2", { payload: 1 });
|
|
82
|
+
bus.emit("test3", { payload: true });
|
|
83
|
+
|
|
84
|
+
expect(handler).toHaveBeenCalledWith({ payload: "test" });
|
|
85
|
+
expect(handler2).toHaveBeenCalledWith({ payload: 1 });
|
|
83
86
|
|
|
87
|
+
const off = bus.off("test", handler);
|
|
88
|
+
expect(off).toBe(true);
|
|
89
|
+
bus.emit("test", { payload: "test" });
|
|
90
|
+
expect(handler).toHaveBeenCalledTimes(1);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("should be able to work without providing event types", () => {
|
|
94
|
+
const bus = new EventBus();
|
|
95
|
+
const handler = vi.fn();
|
|
96
|
+
const handler2 = vi.fn();
|
|
97
|
+
|
|
98
|
+
bus.on("test", ({ payload }: { payload: string }) => {
|
|
99
|
+
expect(payload).toBeTypeOf("string");
|
|
100
|
+
expect(payload).toBe("test");
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
bus.on("test", handler);
|
|
84
104
|
bus.emit("test", { payload: "test" });
|
|
85
105
|
expect(handler).toHaveBeenCalledWith({ payload: "test" });
|
|
86
|
-
bus.
|
|
87
|
-
expect(
|
|
106
|
+
const off = bus.off("test", handler);
|
|
107
|
+
expect(off).toBe(true);
|
|
108
|
+
bus.emit("test", { payload: "test" });
|
|
109
|
+
expect(handler).toHaveBeenCalledTimes(1);
|
|
110
|
+
|
|
111
|
+
bus.on("test2", handler2);
|
|
112
|
+
bus.emit("test2", { payload: "test2" });
|
|
113
|
+
bus.emit("test2", { payload: "test2" });
|
|
114
|
+
expect(handler2).toHaveBeenCalledTimes(2);
|
|
115
|
+
expect(bus.off("test2", handler2)).toBe(true);
|
|
88
116
|
|
|
89
|
-
|
|
90
|
-
expect(
|
|
91
|
-
expect(catchAllHandler).toHaveBeenCalledWith("test2", { payload: "test" });
|
|
117
|
+
const testTypedHandler = (_args: { payload: string }) => {};
|
|
118
|
+
expect(bus.off("randomChannel", testTypedHandler)).toBe(false);
|
|
92
119
|
});
|
|
93
120
|
});
|
package/src/EventBus.ts
CHANGED
|
@@ -16,23 +16,41 @@ export type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<
|
|
|
16
16
|
Set<GenericEventHandler<Events>>
|
|
17
17
|
>;
|
|
18
18
|
|
|
19
|
-
type GenericEventHandler<Events extends Record<EventType, unknown>> =
|
|
19
|
+
export type GenericEventHandler<Events extends Record<EventType, unknown>> =
|
|
20
20
|
| Handler<Events[keyof Events]>
|
|
21
21
|
| WildcardHandler<Events>;
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
type IsLooseEvents<Events extends Record<EventType, unknown>> =
|
|
24
|
+
string extends keyof Events ? true : false;
|
|
25
|
+
|
|
26
|
+
export class EventBus<
|
|
27
|
+
Events extends Record<EventType, unknown> = Record<EventType, unknown>,
|
|
28
|
+
> {
|
|
24
29
|
public readonly all: EventHandlerMap<Events> = new Map();
|
|
25
30
|
|
|
26
31
|
public constructor(all?: EventHandlerMap<Events>) {
|
|
27
32
|
this.all = all || new Map();
|
|
28
33
|
}
|
|
29
34
|
|
|
30
|
-
|
|
35
|
+
/**
|
|
36
|
+
* Add an event handler
|
|
37
|
+
* @param type - The event type
|
|
38
|
+
* @param handler - The event handler
|
|
39
|
+
* @returns A function to remove the event handler
|
|
40
|
+
*/
|
|
41
|
+
public on<Key extends keyof Events>(
|
|
31
42
|
type: Key,
|
|
32
|
-
handler:
|
|
33
|
-
)
|
|
34
|
-
|
|
35
|
-
|
|
43
|
+
handler: Handler<Events[Key]>
|
|
44
|
+
): () => void;
|
|
45
|
+
public on(type: "*", handler: WildcardHandler<Events>): () => void;
|
|
46
|
+
public on<T extends string>(
|
|
47
|
+
type: IsLooseEvents<Events> extends true ? T : never,
|
|
48
|
+
handler: IsLooseEvents<Events> extends true
|
|
49
|
+
? (...args: any[]) => void
|
|
50
|
+
: never
|
|
51
|
+
): () => void;
|
|
52
|
+
public on(type: keyof Events | "*", handler: GenericEventHandler<Events>) {
|
|
53
|
+
const handlers = this.all.get(type);
|
|
36
54
|
if (handlers) {
|
|
37
55
|
handlers.add(handler);
|
|
38
56
|
} else {
|
|
@@ -42,21 +60,23 @@ export class EventBus<Events extends Record<EventType, unknown>> {
|
|
|
42
60
|
return () => this.off(type, handler);
|
|
43
61
|
}
|
|
44
62
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
this.all.set(type, new Set());
|
|
56
|
-
}
|
|
57
|
-
}
|
|
63
|
+
/**
|
|
64
|
+
* Remove an event handler
|
|
65
|
+
* @param type - The event type
|
|
66
|
+
* @param handler - The event handler
|
|
67
|
+
*/
|
|
68
|
+
public off(
|
|
69
|
+
type: keyof Events | "*",
|
|
70
|
+
handler: (...args: any[]) => any
|
|
71
|
+
): boolean {
|
|
72
|
+
return this.all.get(type)?.delete(handler) ?? false;
|
|
58
73
|
}
|
|
59
74
|
|
|
75
|
+
/**
|
|
76
|
+
* Emit an event
|
|
77
|
+
* @param type - The event type
|
|
78
|
+
* @param args - The event arguments
|
|
79
|
+
*/
|
|
60
80
|
public emit<Key extends keyof Events>(
|
|
61
81
|
type: Key,
|
|
62
82
|
...args: EmitArgs<Events, Key>
|
|
@@ -64,26 +84,19 @@ export class EventBus<Events extends Record<EventType, unknown>> {
|
|
|
64
84
|
const evt = args[0] as Events[Key];
|
|
65
85
|
let handlers = this.all.get(type);
|
|
66
86
|
if (handlers) {
|
|
67
|
-
(handlers as Set<Handler<Events[Key]>>).forEach((h) =>
|
|
68
|
-
h(evt!)
|
|
69
|
-
);
|
|
87
|
+
(handlers as Set<Handler<Events[Key]>>).forEach((h) => h(evt!));
|
|
70
88
|
}
|
|
71
89
|
|
|
72
90
|
handlers = this.all.get("*");
|
|
73
91
|
if (handlers) {
|
|
74
|
-
(handlers as Set<WildcardHandler<Events>>).forEach((h) =>
|
|
75
|
-
h(type, evt!)
|
|
76
|
-
);
|
|
92
|
+
(handlers as Set<WildcardHandler<Events>>).forEach((h) => h(type, evt!));
|
|
77
93
|
}
|
|
78
94
|
}
|
|
79
|
-
}
|
|
80
95
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
)
|
|
88
|
-
);
|
|
96
|
+
/**
|
|
97
|
+
* Clear all event handlers
|
|
98
|
+
*/
|
|
99
|
+
public dispose() {
|
|
100
|
+
this.all.clear();
|
|
101
|
+
}
|
|
89
102
|
}
|