@pixotope/event-bus 0.2.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.
@@ -0,0 +1,20 @@
1
+
2
+ > @pixotope/event-bus@0.2.0 build /home/circleci/project/packages/event-bus
3
+ > tsup src/index.ts --format cjs,esm --dts --sourcemap
4
+
5
+ CLI Building entry: src/index.ts
6
+ CLI Using tsconfig: tsconfig.json
7
+ CLI tsup v8.5.0
8
+ CLI Target: es2020
9
+ CJS Build start
10
+ ESM Build start
11
+ CJS dist/index.cjs 2.39 KB
12
+ CJS dist/index.cjs.map 3.90 KB
13
+ CJS ⚡️ Build success in 393ms
14
+ ESM dist/index.js 1.39 KB
15
+ ESM dist/index.js.map 3.81 KB
16
+ ESM ⚡️ Build success in 394ms
17
+ DTS Build start
18
+ DTS ⚡️ Build success in 1702ms
19
+ DTS dist/index.d.cts 1.53 KB
20
+ DTS dist/index.d.ts 1.53 KB
@@ -0,0 +1,4 @@
1
+
2
+ > @pixotope/event-bus@0.2.0 check-types /home/circleci/project/packages/event-bus
3
+ > tsc --noEmit
4
+
@@ -0,0 +1,14 @@
1
+
2
+ > @pixotope/event-bus@0.2.0 test /home/circleci/project/packages/event-bus
3
+ > vitest
4
+
5
+
6
+  RUN  v3.2.4 /home/circleci/project/packages/event-bus
7
+
8
+ ✓ src/EventBus.test.ts (8 tests) 7ms
9
+
10
+  Test Files  1 passed (1)
11
+  Tests  8 passed (8)
12
+  Start at  17:44:39
13
+  Duration  4.21s (transform 500ms, setup 0ms, collect 286ms, tests 7ms, environment 0ms, prepare 1.11s)
14
+
package/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ # @pixotope/event-bus
2
+
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - baa9a61: - added new `event-bus` package
8
+ - in `packages/query` update to pass more info to `queryFn`
package/dist/index.cjs ADDED
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
21
+
22
+ // src/index.ts
23
+ var index_exports = {};
24
+ __export(index_exports, {
25
+ EventBus: () => EventBus,
26
+ createEventBus: () => createEventBus
27
+ });
28
+ module.exports = __toCommonJS(index_exports);
29
+
30
+ // src/EventBus.ts
31
+ var EventBus = class {
32
+ constructor(all) {
33
+ __publicField(this, "all", /* @__PURE__ */ new Map());
34
+ this.all = all || /* @__PURE__ */ new Map();
35
+ }
36
+ on(type, handler) {
37
+ const handlers = this.all.get(type);
38
+ if (handlers) {
39
+ handlers.push(handler);
40
+ } else {
41
+ this.all.set(type, [handler]);
42
+ }
43
+ return () => this.off(type, handler);
44
+ }
45
+ off(type, handler) {
46
+ const handlers = this.all.get(type);
47
+ if (handlers) {
48
+ if (handler) {
49
+ handlers.splice(handlers.indexOf(handler) >>> 0, 1);
50
+ } else {
51
+ this.all.set(type, []);
52
+ }
53
+ }
54
+ }
55
+ emit(type, ...args) {
56
+ const evt = args[0];
57
+ let handlers = this.all.get(type);
58
+ if (handlers) {
59
+ handlers.forEach(
60
+ (h) => h(evt)
61
+ );
62
+ }
63
+ handlers = this.all.get("*");
64
+ if (handlers) {
65
+ handlers.forEach(
66
+ (h) => h(type, evt)
67
+ );
68
+ }
69
+ }
70
+ };
71
+ function createEventBus(defaults) {
72
+ return new EventBus(
73
+ new Map(
74
+ Object.entries(defaults || {})
75
+ )
76
+ );
77
+ }
78
+ // Annotate the CommonJS export names for ESM import in node:
79
+ 0 && (module.exports = {
80
+ EventBus,
81
+ createEventBus
82
+ });
83
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +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 EventHandlerList<T = unknown> = Array<Handler<T>>;\nexport type WildCardEventHandlerList<T = Record<string, unknown>> = Array<\n WildcardHandler<T>\n>;\n\nexport type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<\n keyof Events | \"*\",\n EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>\n>;\n\ntype GenericEventHandler<Events extends Record<EventType, unknown>> =\n | Handler<Events[keyof Events]>\n | WildcardHandler<Events>;\n\nexport class EventBus<Events extends Record<EventType, unknown>> {\n public readonly all: EventHandlerMap<Events> = new Map();\n\n public constructor(all?: EventHandlerMap<Events>) {\n this.all = all || new Map();\n }\n\n public on<Key extends keyof Events | \"*\">(\n type: Key,\n handler: GenericEventHandler<Events>\n ) {\n const handlers: Array<GenericEventHandler<Events>> | undefined =\n this.all.get(type);\n if (handlers) {\n handlers.push(handler);\n } else {\n this.all.set(type, [handler] as EventHandlerList<Events[keyof Events]>);\n }\n\n return () => this.off(type, handler);\n }\n\n public off<Key extends keyof Events | \"*\">(\n type: Key,\n handler?: GenericEventHandler<Events>\n ) {\n const handlers: Array<GenericEventHandler<Events>> | undefined =\n this.all.get(type);\n if (handlers) {\n if (handler) {\n handlers.splice(handlers.indexOf(handler) >>> 0, 1);\n } else {\n this.all.set(type, []);\n }\n }\n }\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 EventHandlerList<Events[keyof Events]>).forEach((h) =>\n h(evt!)\n );\n }\n\n handlers = this.all.get(\"*\");\n if (handlers) {\n (handlers as WildCardEventHandlerList<Events>).forEach((h) =>\n h(type, evt!)\n );\n }\n }\n}\n\nexport function createEventBus<Events extends Record<EventType, unknown>>(\n defaults?: Record<keyof Events | \"*\", GenericEventHandler<Events>[]>\n) {\n return new EventBus<Events>(\n new Map(\n Object.entries(defaults || {}) as [\n keyof Events | \"*\",\n (\n | EventHandlerList<Events[keyof Events]>\n | WildCardEventHandlerList<Events>\n ),\n ][]\n )\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;AC2BO,IAAM,WAAN,MAA0D;AAAA,EAGxD,YAAY,KAA+B;AAFlD,wBAAgB,OAA+B,oBAAI,IAAI;AAGrD,SAAK,MAAM,OAAO,oBAAI,IAAI;AAAA,EAC5B;AAAA,EAEO,GACL,MACA,SACA;AACA,UAAM,WACJ,KAAK,IAAI,IAAI,IAAI;AACnB,QAAI,UAAU;AACZ,eAAS,KAAK,OAAO;AAAA,IACvB,OAAO;AACL,WAAK,IAAI,IAAI,MAAM,CAAC,OAAO,CAA2C;AAAA,IACxE;AAEA,WAAO,MAAM,KAAK,IAAI,MAAM,OAAO;AAAA,EACrC;AAAA,EAEO,IACL,MACA,SACA;AACA,UAAM,WACJ,KAAK,IAAI,IAAI,IAAI;AACnB,QAAI,UAAU;AACZ,UAAI,SAAS;AACX,iBAAS,OAAO,SAAS,QAAQ,OAAO,MAAM,GAAG,CAAC;AAAA,MACpD,OAAO;AACL,aAAK,IAAI,IAAI,MAAM,CAAC,CAAC;AAAA,MACvB;AAAA,IACF;AAAA,EACF;AAAA,EAEO,KACL,SACG,MACH;AACA,UAAM,MAAM,KAAK,CAAC;AAClB,QAAI,WAAW,KAAK,IAAI,IAAI,IAAI;AAChC,QAAI,UAAU;AACZ,MAAC,SAAoD;AAAA,QAAQ,CAAC,MAC5D,EAAE,GAAI;AAAA,MACR;AAAA,IACF;AAEA,eAAW,KAAK,IAAI,IAAI,GAAG;AAC3B,QAAI,UAAU;AACZ,MAAC,SAA8C;AAAA,QAAQ,CAAC,MACtD,EAAE,MAAM,GAAI;AAAA,MACd;AAAA,IACF;AAAA,EACF;AACF;AAEO,SAAS,eACd,UACA;AACA,SAAO,IAAI;AAAA,IACT,IAAI;AAAA,MACF,OAAO,QAAQ,YAAY,CAAC,CAAC;AAAA,IAO/B;AAAA,EACF;AACF;","names":[]}
@@ -0,0 +1,18 @@
1
+ type EventType = string | symbol;
2
+ type EmitArgs<Events extends Record<EventType, unknown>, Key extends keyof Events> = undefined extends Events[Key] ? [evt?: Events[Key]] : [evt: Events[Key]];
3
+ type Handler<T = unknown> = (event: T) => void;
4
+ type WildcardHandler<T = Record<string, unknown>> = (type: keyof T, event: T[keyof T]) => void;
5
+ type EventHandlerList<T = unknown> = Array<Handler<T>>;
6
+ type WildCardEventHandlerList<T = Record<string, unknown>> = Array<WildcardHandler<T>>;
7
+ type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<keyof Events | "*", EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>>;
8
+ type GenericEventHandler<Events extends Record<EventType, unknown>> = Handler<Events[keyof Events]> | WildcardHandler<Events>;
9
+ declare class EventBus<Events extends Record<EventType, unknown>> {
10
+ readonly all: EventHandlerMap<Events>;
11
+ constructor(all?: EventHandlerMap<Events>);
12
+ on<Key extends keyof Events | "*">(type: Key, handler: GenericEventHandler<Events>): () => void;
13
+ off<Key extends keyof Events | "*">(type: Key, handler?: GenericEventHandler<Events>): void;
14
+ emit<Key extends keyof Events | "*">(type: Key, ...args: EmitArgs<Events, Key>): void;
15
+ }
16
+ declare function createEventBus<Events extends Record<EventType, unknown>>(defaults?: Record<keyof Events | "*", GenericEventHandler<Events>[]>): EventBus<Events>;
17
+
18
+ export { type EmitArgs, EventBus, type EventHandlerList, type EventHandlerMap, type EventType, type Handler, type WildCardEventHandlerList, type WildcardHandler, createEventBus };
@@ -0,0 +1,18 @@
1
+ type EventType = string | symbol;
2
+ type EmitArgs<Events extends Record<EventType, unknown>, Key extends keyof Events> = undefined extends Events[Key] ? [evt?: Events[Key]] : [evt: Events[Key]];
3
+ type Handler<T = unknown> = (event: T) => void;
4
+ type WildcardHandler<T = Record<string, unknown>> = (type: keyof T, event: T[keyof T]) => void;
5
+ type EventHandlerList<T = unknown> = Array<Handler<T>>;
6
+ type WildCardEventHandlerList<T = Record<string, unknown>> = Array<WildcardHandler<T>>;
7
+ type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<keyof Events | "*", EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>>;
8
+ type GenericEventHandler<Events extends Record<EventType, unknown>> = Handler<Events[keyof Events]> | WildcardHandler<Events>;
9
+ declare class EventBus<Events extends Record<EventType, unknown>> {
10
+ readonly all: EventHandlerMap<Events>;
11
+ constructor(all?: EventHandlerMap<Events>);
12
+ on<Key extends keyof Events | "*">(type: Key, handler: GenericEventHandler<Events>): () => void;
13
+ off<Key extends keyof Events | "*">(type: Key, handler?: GenericEventHandler<Events>): void;
14
+ emit<Key extends keyof Events | "*">(type: Key, ...args: EmitArgs<Events, Key>): void;
15
+ }
16
+ declare function createEventBus<Events extends Record<EventType, unknown>>(defaults?: Record<keyof Events | "*", GenericEventHandler<Events>[]>): EventBus<Events>;
17
+
18
+ export { type EmitArgs, EventBus, type EventHandlerList, type EventHandlerMap, type EventType, type Handler, type WildCardEventHandlerList, type WildcardHandler, createEventBus };
package/dist/index.js ADDED
@@ -0,0 +1,57 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
+
5
+ // src/EventBus.ts
6
+ var EventBus = class {
7
+ constructor(all) {
8
+ __publicField(this, "all", /* @__PURE__ */ new Map());
9
+ this.all = all || /* @__PURE__ */ new Map();
10
+ }
11
+ on(type, handler) {
12
+ const handlers = this.all.get(type);
13
+ if (handlers) {
14
+ handlers.push(handler);
15
+ } else {
16
+ this.all.set(type, [handler]);
17
+ }
18
+ return () => this.off(type, handler);
19
+ }
20
+ off(type, handler) {
21
+ const handlers = this.all.get(type);
22
+ if (handlers) {
23
+ if (handler) {
24
+ handlers.splice(handlers.indexOf(handler) >>> 0, 1);
25
+ } else {
26
+ this.all.set(type, []);
27
+ }
28
+ }
29
+ }
30
+ emit(type, ...args) {
31
+ const evt = args[0];
32
+ let handlers = this.all.get(type);
33
+ if (handlers) {
34
+ handlers.forEach(
35
+ (h) => h(evt)
36
+ );
37
+ }
38
+ handlers = this.all.get("*");
39
+ if (handlers) {
40
+ handlers.forEach(
41
+ (h) => h(type, evt)
42
+ );
43
+ }
44
+ }
45
+ };
46
+ function createEventBus(defaults) {
47
+ return new EventBus(
48
+ new Map(
49
+ Object.entries(defaults || {})
50
+ )
51
+ );
52
+ }
53
+ export {
54
+ EventBus,
55
+ createEventBus
56
+ };
57
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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 EventHandlerList<T = unknown> = Array<Handler<T>>;\nexport type WildCardEventHandlerList<T = Record<string, unknown>> = Array<\n WildcardHandler<T>\n>;\n\nexport type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<\n keyof Events | \"*\",\n EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>\n>;\n\ntype GenericEventHandler<Events extends Record<EventType, unknown>> =\n | Handler<Events[keyof Events]>\n | WildcardHandler<Events>;\n\nexport class EventBus<Events extends Record<EventType, unknown>> {\n public readonly all: EventHandlerMap<Events> = new Map();\n\n public constructor(all?: EventHandlerMap<Events>) {\n this.all = all || new Map();\n }\n\n public on<Key extends keyof Events | \"*\">(\n type: Key,\n handler: GenericEventHandler<Events>\n ) {\n const handlers: Array<GenericEventHandler<Events>> | undefined =\n this.all.get(type);\n if (handlers) {\n handlers.push(handler);\n } else {\n this.all.set(type, [handler] as EventHandlerList<Events[keyof Events]>);\n }\n\n return () => this.off(type, handler);\n }\n\n public off<Key extends keyof Events | \"*\">(\n type: Key,\n handler?: GenericEventHandler<Events>\n ) {\n const handlers: Array<GenericEventHandler<Events>> | undefined =\n this.all.get(type);\n if (handlers) {\n if (handler) {\n handlers.splice(handlers.indexOf(handler) >>> 0, 1);\n } else {\n this.all.set(type, []);\n }\n }\n }\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 EventHandlerList<Events[keyof Events]>).forEach((h) =>\n h(evt!)\n );\n }\n\n handlers = this.all.get(\"*\");\n if (handlers) {\n (handlers as WildCardEventHandlerList<Events>).forEach((h) =>\n h(type, evt!)\n );\n }\n }\n}\n\nexport function createEventBus<Events extends Record<EventType, unknown>>(\n defaults?: Record<keyof Events | \"*\", GenericEventHandler<Events>[]>\n) {\n return new EventBus<Events>(\n new Map(\n Object.entries(defaults || {}) as [\n keyof Events | \"*\",\n (\n | EventHandlerList<Events[keyof Events]>\n | WildCardEventHandlerList<Events>\n ),\n ][]\n )\n );\n}\n"],"mappings":";;;;;AA2BO,IAAM,WAAN,MAA0D;AAAA,EAGxD,YAAY,KAA+B;AAFlD,wBAAgB,OAA+B,oBAAI,IAAI;AAGrD,SAAK,MAAM,OAAO,oBAAI,IAAI;AAAA,EAC5B;AAAA,EAEO,GACL,MACA,SACA;AACA,UAAM,WACJ,KAAK,IAAI,IAAI,IAAI;AACnB,QAAI,UAAU;AACZ,eAAS,KAAK,OAAO;AAAA,IACvB,OAAO;AACL,WAAK,IAAI,IAAI,MAAM,CAAC,OAAO,CAA2C;AAAA,IACxE;AAEA,WAAO,MAAM,KAAK,IAAI,MAAM,OAAO;AAAA,EACrC;AAAA,EAEO,IACL,MACA,SACA;AACA,UAAM,WACJ,KAAK,IAAI,IAAI,IAAI;AACnB,QAAI,UAAU;AACZ,UAAI,SAAS;AACX,iBAAS,OAAO,SAAS,QAAQ,OAAO,MAAM,GAAG,CAAC;AAAA,MACpD,OAAO;AACL,aAAK,IAAI,IAAI,MAAM,CAAC,CAAC;AAAA,MACvB;AAAA,IACF;AAAA,EACF;AAAA,EAEO,KACL,SACG,MACH;AACA,UAAM,MAAM,KAAK,CAAC;AAClB,QAAI,WAAW,KAAK,IAAI,IAAI,IAAI;AAChC,QAAI,UAAU;AACZ,MAAC,SAAoD;AAAA,QAAQ,CAAC,MAC5D,EAAE,GAAI;AAAA,MACR;AAAA,IACF;AAEA,eAAW,KAAK,IAAI,IAAI,GAAG;AAC3B,QAAI,UAAU;AACZ,MAAC,SAA8C;AAAA,QAAQ,CAAC,MACtD,EAAE,MAAM,GAAI;AAAA,MACd;AAAA,IACF;AAAA,EACF;AACF;AAEO,SAAS,eACd,UACA;AACA,SAAO,IAAI;AAAA,IACT,IAAI;AAAA,MACF,OAAO,QAAQ,YAAY,CAAC,CAAC;AAAA,IAO/B;AAAA,EACF;AACF;","names":[]}
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@pixotope/event-bus",
3
+ "version": "0.2.0",
4
+ "description": "Typed in-memory event bus for Pixotope Foundation",
5
+ "private": false,
6
+ "type": "module",
7
+ "main": "dist/index.js",
8
+ "module": "dist/index.js",
9
+ "types": "dist/index.d.ts",
10
+ "devDependencies": {
11
+ "tsup": "^8.5.0",
12
+ "typescript": "^5.7.2",
13
+ "vitest": "^3.2.4"
14
+ },
15
+ "scripts": {
16
+ "build": "tsup src/index.ts --format cjs,esm --dts --sourcemap",
17
+ "check-types": "tsc --noEmit",
18
+ "test": "vitest"
19
+ }
20
+ }
@@ -0,0 +1,68 @@
1
+ import { describe, it, expect, vi } from "vitest";
2
+ import { EventBus, createEventBus } from "./EventBus";
3
+
4
+ describe("EventBus", () => {
5
+ it("should create an event bus", () => {
6
+ const bus = new EventBus();
7
+ expect(bus).toBeDefined();
8
+ });
9
+
10
+ it("should be able to emit an event", () => {
11
+ const bus = new EventBus();
12
+ const handler = vi.fn();
13
+ bus.on("test", handler);
14
+ bus.emit("test");
15
+ expect(handler).toHaveBeenCalled();
16
+ });
17
+
18
+ it("should be able to off an event", () => {
19
+ const bus = new EventBus();
20
+ const handler = vi.fn();
21
+ bus.on("test", handler);
22
+ bus.off("test", handler);
23
+ bus.emit("test");
24
+ expect(handler).not.toHaveBeenCalled();
25
+ });
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
+ it("should be able to emit an event with a wildcard handler", () => {
46
+ const bus = new EventBus();
47
+ const handler = vi.fn();
48
+ bus.on("*", handler);
49
+ bus.emit("test");
50
+ expect(handler).toHaveBeenCalled();
51
+ });
52
+
53
+ it("should return off function when on is called", () => {
54
+ const bus = new EventBus();
55
+ const handler = vi.fn();
56
+ const off = bus.on("test", handler);
57
+ off();
58
+ bus.emit("test");
59
+ expect(handler).not.toHaveBeenCalled();
60
+ });
61
+ });
62
+
63
+ describe("createEventBus", () => {
64
+ it("should create an event bus with a specific type", () => {
65
+ const bus = createEventBus();
66
+ expect(bus).toBeDefined();
67
+ });
68
+ });
@@ -0,0 +1,100 @@
1
+ export type EventType = string | symbol;
2
+
3
+ export type EmitArgs<
4
+ Events extends Record<EventType, unknown>,
5
+ Key extends keyof Events,
6
+ > = undefined extends Events[Key] ? [evt?: Events[Key]] : [evt: Events[Key]];
7
+
8
+ export type Handler<T = unknown> = (event: T) => void;
9
+ export type WildcardHandler<T = Record<string, unknown>> = (
10
+ type: keyof T,
11
+ event: T[keyof T]
12
+ ) => void;
13
+
14
+ export type EventHandlerList<T = unknown> = Array<Handler<T>>;
15
+ export type WildCardEventHandlerList<T = Record<string, unknown>> = Array<
16
+ WildcardHandler<T>
17
+ >;
18
+
19
+ export type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<
20
+ keyof Events | "*",
21
+ EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>
22
+ >;
23
+
24
+ type GenericEventHandler<Events extends Record<EventType, unknown>> =
25
+ | Handler<Events[keyof Events]>
26
+ | WildcardHandler<Events>;
27
+
28
+ export class EventBus<Events extends Record<EventType, unknown>> {
29
+ public readonly all: EventHandlerMap<Events> = new Map();
30
+
31
+ public constructor(all?: EventHandlerMap<Events>) {
32
+ this.all = all || new Map();
33
+ }
34
+
35
+ public on<Key extends keyof Events | "*">(
36
+ type: Key,
37
+ handler: GenericEventHandler<Events>
38
+ ) {
39
+ const handlers: Array<GenericEventHandler<Events>> | undefined =
40
+ this.all.get(type);
41
+ if (handlers) {
42
+ handlers.push(handler);
43
+ } else {
44
+ this.all.set(type, [handler] as EventHandlerList<Events[keyof Events]>);
45
+ }
46
+
47
+ return () => this.off(type, handler);
48
+ }
49
+
50
+ public off<Key extends keyof Events | "*">(
51
+ type: Key,
52
+ handler?: GenericEventHandler<Events>
53
+ ) {
54
+ const handlers: Array<GenericEventHandler<Events>> | undefined =
55
+ this.all.get(type);
56
+ if (handlers) {
57
+ if (handler) {
58
+ handlers.splice(handlers.indexOf(handler) >>> 0, 1);
59
+ } else {
60
+ this.all.set(type, []);
61
+ }
62
+ }
63
+ }
64
+
65
+ public emit<Key extends keyof Events | "*">(
66
+ type: Key,
67
+ ...args: EmitArgs<Events, Key>
68
+ ) {
69
+ const evt = args[0] as Events[Key];
70
+ let handlers = this.all.get(type);
71
+ if (handlers) {
72
+ (handlers as EventHandlerList<Events[keyof Events]>).forEach((h) =>
73
+ h(evt!)
74
+ );
75
+ }
76
+
77
+ handlers = this.all.get("*");
78
+ if (handlers) {
79
+ (handlers as WildCardEventHandlerList<Events>).forEach((h) =>
80
+ h(type, evt!)
81
+ );
82
+ }
83
+ }
84
+ }
85
+
86
+ export function createEventBus<Events extends Record<EventType, unknown>>(
87
+ defaults?: Record<keyof Events | "*", GenericEventHandler<Events>[]>
88
+ ) {
89
+ return new EventBus<Events>(
90
+ new Map(
91
+ Object.entries(defaults || {}) as [
92
+ keyof Events | "*",
93
+ (
94
+ | EventHandlerList<Events[keyof Events]>
95
+ | WildCardEventHandlerList<Events>
96
+ ),
97
+ ][]
98
+ )
99
+ );
100
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./EventBus";
package/tsconfig.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "compilerOptions": {
3
+ "baseUrl": "./src",
4
+ "rootDir": "./src",
5
+ "target": "ES2020",
6
+ "module": "ES2020",
7
+ "moduleResolution": "bundler",
8
+ "lib": ["ES2020"],
9
+ "types": [],
10
+ "allowJs": true,
11
+ "allowSyntheticDefaultImports": true,
12
+ "useDefineForClassFields": true,
13
+ "strict": true,
14
+ "skipLibCheck": true,
15
+ "sourceMap": true,
16
+ "resolveJsonModule": true,
17
+ "isolatedModules": true,
18
+ "esModuleInterop": true,
19
+ "noEmit": true,
20
+ "noUnusedLocals": false,
21
+ "noUnusedParameters": false,
22
+ "noImplicitReturns": false,
23
+ "forceConsistentCasingInFileNames": true
24
+ },
25
+ "include": ["src"],
26
+ "exclude": ["node_modules", "dist"]
27
+ }
@@ -0,0 +1,8 @@
1
+ import { defineConfig } from "vitest/config";
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ environment: "node",
6
+ include: ["src/**/*.{test,spec}.ts"],
7
+ },
8
+ });