@pixotope/event-bus 0.4.0 → 0.6.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.
@@ -1,5 +1,5 @@
1
1
 
2
- > @pixotope/event-bus@0.4.0 build /home/circleci/project/packages/event-bus
2
+ > @pixotope/event-bus@0.6.0 build /home/circleci/project/packages/event-bus
3
3
  > tsup src/index.ts --format cjs,esm --dts --sourcemap
4
4
 
5
5
  CLI Building entry: src/index.ts
@@ -8,13 +8,13 @@
8
8
  CLI Target: es2020
9
9
  CJS Build start
10
10
  ESM Build start
11
- ESM dist/index.js 1.40 KB
12
- ESM dist/index.js.map 3.67 KB
13
- ESM ⚡️ Build success in 198ms
14
- CJS dist/index.cjs 2.36 KB
15
- CJS dist/index.cjs.map 3.76 KB
16
- CJS ⚡️ Build success in 199ms
11
+ ESM dist/index.js 1.59 KB
12
+ ESM dist/index.js.map 4.81 KB
13
+ ESM ⚡️ Build success in 533ms
14
+ CJS dist/index.cjs 2.55 KB
15
+ CJS dist/index.cjs.map 4.91 KB
16
+ CJS ⚡️ Build success in 534ms
17
17
  DTS Build start
18
- DTS ⚡️ Build success in 2192ms
19
- DTS dist/index.d.cts 1.94 KB
20
- DTS dist/index.d.ts 1.94 KB
18
+ DTS ⚡️ Build success in 1944ms
19
+ DTS dist/index.d.cts 3.06 KB
20
+ DTS dist/index.d.ts 3.06 KB
@@ -1,4 +1,4 @@
1
1
 
2
- > @pixotope/event-bus@0.4.0 check-types /home/circleci/project/packages/event-bus
2
+ > @pixotope/event-bus@0.6.0 check-types /home/circleci/project/packages/event-bus
3
3
  > tsc --noEmit
4
4
 
@@ -1,14 +1,14 @@
1
1
 
2
- > @pixotope/event-bus@0.4.0 test /home/circleci/project/packages/event-bus
2
+ > @pixotope/event-bus@0.6.0 test /home/circleci/project/packages/event-bus
3
3
  > vitest
4
4
 
5
5
 
6
6
   RUN  v3.2.4 /home/circleci/project/packages/event-bus
7
7
 
8
- ✓ src/EventBus.test.ts (8 tests) 99ms
8
+ ✓ src/EventBus.test.ts (8 tests) 157ms
9
9
 
10
10
   Test Files  1 passed (1)
11
11
   Tests  8 passed (8)
12
-  Start at  16:16:57
13
-  Duration  4.18s (transform 712ms, setup 0ms, collect 893ms, tests 99ms, environment 0ms, prepare 1.21s)
12
+  Start at  13:46:44
13
+  Duration  4.70s (transform 950ms, setup 0ms, collect 689ms, tests 157ms, environment 0ms, prepare 1.79s)
14
14
 
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @pixotope/event-bus
2
2
 
3
+ ## 0.6.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 8a28444: Include detailed documentation for every package and setup `typedoc`
8
+
9
+ ## 0.5.0
10
+
11
+ ### Minor Changes
12
+
13
+ - aeed4b1: stronger `on` event-bus types
14
+
3
15
  ## 0.4.0
4
16
 
5
17
  ### Minor Changes
package/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # @pixotope/event-bus
2
+
3
+ Typed in-memory event bus for Pixotope Foundation.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ pnpm add @pixotope/event-bus
9
+ ```
10
+
11
+ ## Quick start
12
+
13
+ ```ts
14
+ import { EventBus } from "@pixotope/event-bus";
15
+
16
+ // Define the events this bus can carry and their payload types.
17
+ type Events = {
18
+ test: { payload: string };
19
+ test2: { payload: number };
20
+ };
21
+
22
+ const bus = new EventBus<Events>();
23
+
24
+ // Subscribe. `on` returns an unsubscribe function.
25
+ const off = bus.on("test", ({ payload }) => {
26
+ console.log("got test:", payload);
27
+ });
28
+
29
+ // Emit an event to all matching handlers.
30
+ bus.emit("test", { payload: "hello" });
31
+
32
+ // Unsubscribe either by calling the returned function...
33
+ off();
34
+
35
+ // ...or by calling `off` with the same type and handler reference.
36
+ const handler = ({ payload }: { payload: number }) => console.log(payload);
37
+ bus.on("test2", handler);
38
+ bus.off("test2", handler);
39
+
40
+ // Remove every handler on the bus.
41
+ bus.dispose();
42
+ ```
43
+
44
+ ## API overview
45
+
46
+ - `EventBus<Events>` - typed, mitt-like, in-memory pub/sub event bus.
47
+ - `on(type, handler)` - subscribe to an event type (or `"*"` for every event); returns a function that unsubscribes.
48
+ - `off(type, handler)` - unsubscribe a specific handler from an event type.
49
+ - `emit(type, event?)` - emit an event, invoking every matching handler and any wildcard handlers.
50
+ - `dispose()` - remove all registered handlers.
51
+
52
+ See the generated API reference (`pnpm typedoc`) for full type signatures.
package/dist/index.cjs CHANGED
@@ -28,7 +28,12 @@ module.exports = __toCommonJS(index_exports);
28
28
 
29
29
  // src/EventBus.ts
30
30
  var EventBus = class {
31
+ /**
32
+ * Create a new event bus.
33
+ * @param all - An optional existing handler map to use instead of a new, empty one.
34
+ */
31
35
  constructor(all) {
36
+ /** The underlying map of event type to registered handlers. */
32
37
  __publicField(this, "all", /* @__PURE__ */ new Map());
33
38
  this.all = all || /* @__PURE__ */ new Map();
34
39
  }
@@ -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\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(\n type: keyof Events,\n handler: Handler<Events[typeof type]>\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":[]}
1
+ {"version":3,"sources":["../src/index.ts","../src/EventBus.ts"],"sourcesContent":["export * from \"./EventBus\";\n","/** The key used to identify an event: either a string or a symbol. */\nexport type EventType = string | symbol;\n\n/** The arguments accepted by {@link EventBus.emit} for a given event key: optional if the event's payload type allows `undefined`, required otherwise. */\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\n/** A handler invoked with the payload of a single event type. */\nexport type Handler<T = unknown> = (event: T) => void;\n/** A handler invoked for every emitted event, receiving the event type and its payload. */\nexport type WildcardHandler<T = Record<string, unknown>> = (\n type: keyof T,\n event: T[keyof T]\n) => void;\n\n/** A map from event type (or `\"*\"` for wildcard handlers) to the set of handlers registered for it. */\nexport type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<\n keyof Events | \"*\",\n Set<GenericEventHandler<Events>>\n>;\n\n/** Either a typed event {@link Handler} or a {@link WildcardHandler} for the given event map. */\nexport type GenericEventHandler<Events extends Record<EventType, unknown>> =\n | Handler<Events[keyof Events]>\n | WildcardHandler<Events>;\n\n/** Resolves to `true` when `Events` is a loose (string-keyed, not literally typed) event map, enabling {@link EventBus.on}'s untyped overload. */\nexport type IsLooseEvents<Events extends Record<EventType, unknown>> =\n string extends keyof Events ? true : false;\n\n/**\n * Typed, mitt-like, in-memory pub/sub event bus.\n *\n * @typeParam Events - A map of event type to its payload type. Defaults to\n * an untyped, loose event map when omitted.\n */\nexport class EventBus<\n Events extends Record<EventType, unknown> = Record<EventType, unknown>,\n> {\n /** The underlying map of event type to registered handlers. */\n public readonly all: EventHandlerMap<Events> = new Map();\n\n /**\n * Create a new event bus.\n * @param all - An optional existing handler map to use instead of a new, empty one.\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;;;ACsCO,IAAM,WAAN,MAEL;AAAA;AAAA;AAAA;AAAA;AAAA,EAQO,YAAY,KAA+B;AANlD;AAAA,wBAAgB,OAA+B,oBAAI,IAAI;AAOrD,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
@@ -1,12 +1,30 @@
1
+ /** The key used to identify an event: either a string or a symbol. */
1
2
  type EventType = string | symbol;
3
+ /** The arguments accepted by {@link EventBus.emit} for a given event key: optional if the event's payload type allows `undefined`, required otherwise. */
2
4
  type EmitArgs<Events extends Record<EventType, unknown>, Key extends keyof Events> = undefined extends Events[Key] ? [evt?: Events[Key]] : [evt: Events[Key]];
5
+ /** A handler invoked with the payload of a single event type. */
3
6
  type Handler<T = unknown> = (event: T) => void;
7
+ /** A handler invoked for every emitted event, receiving the event type and its payload. */
4
8
  type WildcardHandler<T = Record<string, unknown>> = (type: keyof T, event: T[keyof T]) => void;
9
+ /** A map from event type (or `"*"` for wildcard handlers) to the set of handlers registered for it. */
5
10
  type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<keyof Events | "*", Set<GenericEventHandler<Events>>>;
11
+ /** Either a typed event {@link Handler} or a {@link WildcardHandler} for the given event map. */
6
12
  type GenericEventHandler<Events extends Record<EventType, unknown>> = Handler<Events[keyof Events]> | WildcardHandler<Events>;
13
+ /** Resolves to `true` when `Events` is a loose (string-keyed, not literally typed) event map, enabling {@link EventBus.on}'s untyped overload. */
7
14
  type IsLooseEvents<Events extends Record<EventType, unknown>> = string extends keyof Events ? true : false;
15
+ /**
16
+ * Typed, mitt-like, in-memory pub/sub event bus.
17
+ *
18
+ * @typeParam Events - A map of event type to its payload type. Defaults to
19
+ * an untyped, loose event map when omitted.
20
+ */
8
21
  declare class EventBus<Events extends Record<EventType, unknown> = Record<EventType, unknown>> {
22
+ /** The underlying map of event type to registered handlers. */
9
23
  readonly all: EventHandlerMap<Events>;
24
+ /**
25
+ * Create a new event bus.
26
+ * @param all - An optional existing handler map to use instead of a new, empty one.
27
+ */
10
28
  constructor(all?: EventHandlerMap<Events>);
11
29
  /**
12
30
  * Add an event handler
@@ -14,7 +32,7 @@ declare class EventBus<Events extends Record<EventType, unknown> = Record<EventT
14
32
  * @param handler - The event handler
15
33
  * @returns A function to remove the event handler
16
34
  */
17
- on(type: keyof Events, handler: Handler<Events[typeof type]>): () => void;
35
+ on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): () => void;
18
36
  on(type: "*", handler: WildcardHandler<Events>): () => void;
19
37
  on<T extends string>(type: IsLooseEvents<Events> extends true ? T : never, handler: IsLooseEvents<Events> extends true ? (...args: any[]) => void : never): () => void;
20
38
  /**
@@ -35,4 +53,4 @@ declare class EventBus<Events extends Record<EventType, unknown> = Record<EventT
35
53
  dispose(): void;
36
54
  }
37
55
 
38
- export { type EmitArgs, EventBus, type EventHandlerMap, type EventType, type GenericEventHandler, type Handler, type WildcardHandler };
56
+ export { type EmitArgs, EventBus, type EventHandlerMap, type EventType, type GenericEventHandler, type Handler, type IsLooseEvents, type WildcardHandler };
package/dist/index.d.ts CHANGED
@@ -1,12 +1,30 @@
1
+ /** The key used to identify an event: either a string or a symbol. */
1
2
  type EventType = string | symbol;
3
+ /** The arguments accepted by {@link EventBus.emit} for a given event key: optional if the event's payload type allows `undefined`, required otherwise. */
2
4
  type EmitArgs<Events extends Record<EventType, unknown>, Key extends keyof Events> = undefined extends Events[Key] ? [evt?: Events[Key]] : [evt: Events[Key]];
5
+ /** A handler invoked with the payload of a single event type. */
3
6
  type Handler<T = unknown> = (event: T) => void;
7
+ /** A handler invoked for every emitted event, receiving the event type and its payload. */
4
8
  type WildcardHandler<T = Record<string, unknown>> = (type: keyof T, event: T[keyof T]) => void;
9
+ /** A map from event type (or `"*"` for wildcard handlers) to the set of handlers registered for it. */
5
10
  type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<keyof Events | "*", Set<GenericEventHandler<Events>>>;
11
+ /** Either a typed event {@link Handler} or a {@link WildcardHandler} for the given event map. */
6
12
  type GenericEventHandler<Events extends Record<EventType, unknown>> = Handler<Events[keyof Events]> | WildcardHandler<Events>;
13
+ /** Resolves to `true` when `Events` is a loose (string-keyed, not literally typed) event map, enabling {@link EventBus.on}'s untyped overload. */
7
14
  type IsLooseEvents<Events extends Record<EventType, unknown>> = string extends keyof Events ? true : false;
15
+ /**
16
+ * Typed, mitt-like, in-memory pub/sub event bus.
17
+ *
18
+ * @typeParam Events - A map of event type to its payload type. Defaults to
19
+ * an untyped, loose event map when omitted.
20
+ */
8
21
  declare class EventBus<Events extends Record<EventType, unknown> = Record<EventType, unknown>> {
22
+ /** The underlying map of event type to registered handlers. */
9
23
  readonly all: EventHandlerMap<Events>;
24
+ /**
25
+ * Create a new event bus.
26
+ * @param all - An optional existing handler map to use instead of a new, empty one.
27
+ */
10
28
  constructor(all?: EventHandlerMap<Events>);
11
29
  /**
12
30
  * Add an event handler
@@ -14,7 +32,7 @@ declare class EventBus<Events extends Record<EventType, unknown> = Record<EventT
14
32
  * @param handler - The event handler
15
33
  * @returns A function to remove the event handler
16
34
  */
17
- on(type: keyof Events, handler: Handler<Events[typeof type]>): () => void;
35
+ on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): () => void;
18
36
  on(type: "*", handler: WildcardHandler<Events>): () => void;
19
37
  on<T extends string>(type: IsLooseEvents<Events> extends true ? T : never, handler: IsLooseEvents<Events> extends true ? (...args: any[]) => void : never): () => void;
20
38
  /**
@@ -35,4 +53,4 @@ declare class EventBus<Events extends Record<EventType, unknown> = Record<EventT
35
53
  dispose(): void;
36
54
  }
37
55
 
38
- export { type EmitArgs, EventBus, type EventHandlerMap, type EventType, type GenericEventHandler, type Handler, type WildcardHandler };
56
+ export { type EmitArgs, EventBus, type EventHandlerMap, type EventType, type GenericEventHandler, type Handler, type IsLooseEvents, type WildcardHandler };
package/dist/index.js CHANGED
@@ -4,7 +4,12 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
4
4
 
5
5
  // src/EventBus.ts
6
6
  var EventBus = class {
7
+ /**
8
+ * Create a new event bus.
9
+ * @param all - An optional existing handler map to use instead of a new, empty one.
10
+ */
7
11
  constructor(all) {
12
+ /** The underlying map of event type to registered handlers. */
8
13
  __publicField(this, "all", /* @__PURE__ */ new Map());
9
14
  this.all = all || /* @__PURE__ */ new Map();
10
15
  }
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\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(\n type: keyof Events,\n handler: Handler<Events[typeof type]>\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":[]}
1
+ {"version":3,"sources":["../src/EventBus.ts"],"sourcesContent":["/** The key used to identify an event: either a string or a symbol. */\nexport type EventType = string | symbol;\n\n/** The arguments accepted by {@link EventBus.emit} for a given event key: optional if the event's payload type allows `undefined`, required otherwise. */\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\n/** A handler invoked with the payload of a single event type. */\nexport type Handler<T = unknown> = (event: T) => void;\n/** A handler invoked for every emitted event, receiving the event type and its payload. */\nexport type WildcardHandler<T = Record<string, unknown>> = (\n type: keyof T,\n event: T[keyof T]\n) => void;\n\n/** A map from event type (or `\"*\"` for wildcard handlers) to the set of handlers registered for it. */\nexport type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<\n keyof Events | \"*\",\n Set<GenericEventHandler<Events>>\n>;\n\n/** Either a typed event {@link Handler} or a {@link WildcardHandler} for the given event map. */\nexport type GenericEventHandler<Events extends Record<EventType, unknown>> =\n | Handler<Events[keyof Events]>\n | WildcardHandler<Events>;\n\n/** Resolves to `true` when `Events` is a loose (string-keyed, not literally typed) event map, enabling {@link EventBus.on}'s untyped overload. */\nexport type IsLooseEvents<Events extends Record<EventType, unknown>> =\n string extends keyof Events ? true : false;\n\n/**\n * Typed, mitt-like, in-memory pub/sub event bus.\n *\n * @typeParam Events - A map of event type to its payload type. Defaults to\n * an untyped, loose event map when omitted.\n */\nexport class EventBus<\n Events extends Record<EventType, unknown> = Record<EventType, unknown>,\n> {\n /** The underlying map of event type to registered handlers. */\n public readonly all: EventHandlerMap<Events> = new Map();\n\n /**\n * Create a new event bus.\n * @param all - An optional existing handler map to use instead of a new, empty one.\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":";;;;;AAsCO,IAAM,WAAN,MAEL;AAAA;AAAA;AAAA;AAAA;AAAA,EAQO,YAAY,KAA+B;AANlD;AAAA,wBAAgB,OAA+B,oBAAI,IAAI;AAOrD,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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pixotope/event-bus",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "Typed in-memory event bus for Pixotope Foundation",
5
5
  "private": false,
6
6
  "type": "module",
package/src/EventBus.ts CHANGED
@@ -1,33 +1,51 @@
1
+ /** The key used to identify an event: either a string or a symbol. */
1
2
  export type EventType = string | symbol;
2
3
 
4
+ /** The arguments accepted by {@link EventBus.emit} for a given event key: optional if the event's payload type allows `undefined`, required otherwise. */
3
5
  export type EmitArgs<
4
6
  Events extends Record<EventType, unknown>,
5
7
  Key extends keyof Events,
6
8
  > = undefined extends Events[Key] ? [evt?: Events[Key]] : [evt: Events[Key]];
7
9
 
10
+ /** A handler invoked with the payload of a single event type. */
8
11
  export type Handler<T = unknown> = (event: T) => void;
12
+ /** A handler invoked for every emitted event, receiving the event type and its payload. */
9
13
  export type WildcardHandler<T = Record<string, unknown>> = (
10
14
  type: keyof T,
11
15
  event: T[keyof T]
12
16
  ) => void;
13
17
 
18
+ /** A map from event type (or `"*"` for wildcard handlers) to the set of handlers registered for it. */
14
19
  export type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<
15
20
  keyof Events | "*",
16
21
  Set<GenericEventHandler<Events>>
17
22
  >;
18
23
 
24
+ /** Either a typed event {@link Handler} or a {@link WildcardHandler} for the given event map. */
19
25
  export type GenericEventHandler<Events extends Record<EventType, unknown>> =
20
26
  | Handler<Events[keyof Events]>
21
27
  | WildcardHandler<Events>;
22
28
 
23
- type IsLooseEvents<Events extends Record<EventType, unknown>> =
29
+ /** Resolves to `true` when `Events` is a loose (string-keyed, not literally typed) event map, enabling {@link EventBus.on}'s untyped overload. */
30
+ export type IsLooseEvents<Events extends Record<EventType, unknown>> =
24
31
  string extends keyof Events ? true : false;
25
32
 
33
+ /**
34
+ * Typed, mitt-like, in-memory pub/sub event bus.
35
+ *
36
+ * @typeParam Events - A map of event type to its payload type. Defaults to
37
+ * an untyped, loose event map when omitted.
38
+ */
26
39
  export class EventBus<
27
40
  Events extends Record<EventType, unknown> = Record<EventType, unknown>,
28
41
  > {
42
+ /** The underlying map of event type to registered handlers. */
29
43
  public readonly all: EventHandlerMap<Events> = new Map();
30
44
 
45
+ /**
46
+ * Create a new event bus.
47
+ * @param all - An optional existing handler map to use instead of a new, empty one.
48
+ */
31
49
  public constructor(all?: EventHandlerMap<Events>) {
32
50
  this.all = all || new Map();
33
51
  }
@@ -38,9 +56,9 @@ export class EventBus<
38
56
  * @param handler - The event handler
39
57
  * @returns A function to remove the event handler
40
58
  */
41
- public on(
42
- type: keyof Events,
43
- handler: Handler<Events[typeof type]>
59
+ public on<Key extends keyof Events>(
60
+ type: Key,
61
+ handler: Handler<Events[Key]>
44
62
  ): () => void;
45
63
  public on(type: "*", handler: WildcardHandler<Events>): () => void;
46
64
  public on<T extends string>(
package/typedoc.json ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "entryPoints": ["src/index.ts"]
3
+ }