@homer0/events-hub 2.0.4 → 2.0.6

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.
Files changed (2) hide show
  1. package/dist/index.d.mts +123 -0
  2. package/package.json +8 -8
@@ -0,0 +1,123 @@
1
+ type GenericFn = (...args: any[]) => any;
2
+ type GenericParams = Parameters<GenericFn>;
3
+ type OnceWrapperFn = GenericFn & {
4
+ once: boolean;
5
+ };
6
+ /**
7
+ * When the service receives a "once subscription", it needs to track the listener so it
8
+ * gets removed after being called once, and to avoid modifying the original function, it
9
+ * creates a wrapper function that has the "once" property set to true. The wrapper and
10
+ * the original function are stored in case `off` is called before the wrapper gets
11
+ * triggered; it will receive the original function, not the wrapper, so the class needs a
12
+ * way to map them together.
13
+ */
14
+ type OnceWrapper = {
15
+ /**
16
+ * A simple wrapper for the original listener, with the difference that it has a `once`
17
+ * property set to `true`.
18
+ */
19
+ wrapper: OnceWrapperFn;
20
+ /**
21
+ * The original listener that will be called once.
22
+ */
23
+ original: GenericFn;
24
+ };
25
+ /**
26
+ * A minimal implementation of an events handler service.
27
+ */
28
+ declare class EventsHub {
29
+ /**
30
+ * A dictionary of the events and their listeners.
31
+ */
32
+ protected events: Record<string, GenericFn[]>;
33
+ /**
34
+ * A dictionary of wrappers that were created for "one time subscriptions". This is
35
+ * used by the {@link EventsHub.off}: if it doesn't find the subscriber as it is, it
36
+ * will look for a wrapper and remove it.
37
+ */
38
+ protected onceWrappers: Record<string, OnceWrapper[]>;
39
+ /**
40
+ * Gets all the listeners for a specific event.
41
+ * The list is returned by reference, so it can be modified once obtained.
42
+ *
43
+ * @param event The name of the event.
44
+ */
45
+ protected getSubscribers(event: string): (GenericFn | OnceWrapperFn)[];
46
+ on<ListenerFn extends GenericFn = GenericFn>(event: string, listener: ListenerFn): () => boolean;
47
+ on<ListenerFn extends GenericFn = GenericFn>(event: string[], listener: ListenerFn): () => boolean[];
48
+ on<ListenerFn extends GenericFn = GenericFn>(event: string | string[], listener: ListenerFn): () => boolean | boolean[];
49
+ once<ListenerFn extends GenericFn = GenericFn>(event: string, listener: ListenerFn): () => boolean;
50
+ once<ListenerFn extends GenericFn = GenericFn>(event: string[], listener: ListenerFn): () => boolean[];
51
+ once<ListenerFn extends GenericFn = GenericFn>(event: string | string[], listener: ListenerFn): () => boolean | boolean[];
52
+ off<ListenerFn extends GenericFn | OnceWrapperFn = GenericFn>(event: string[], listener: ListenerFn): boolean[];
53
+ off<ListenerFn extends GenericFn | OnceWrapperFn = GenericFn>(event: string, listener: ListenerFn): boolean;
54
+ off<ListenerFn extends GenericFn | OnceWrapperFn = GenericFn>(event: string | string[], listener: ListenerFn): boolean | boolean[];
55
+ /**
56
+ * Emits an event and call all its listeners.
57
+ *
58
+ * @param event An event name or a list of them.
59
+ * @param args A list of parameters to send to the listeners.
60
+ * @template Args The type of the parameters to send to the listeners.
61
+ * @example
62
+ *
63
+ * const events = new EventsHub();
64
+ * events.on('event', (arg0) => {
65
+ * console.log(`Event received: ${arg0}`);
66
+ * });
67
+ * events.emit('event', 'Hello'); // prints "Event received: Hello"
68
+ *
69
+ */
70
+ emit<Args extends GenericParams>(event: string | string[], ...args: Args): void;
71
+ /**
72
+ * Asynchronously reduces a target using an event. It's like emit, but the events
73
+ * listener return a modified (or not) version of the `target`.
74
+ *
75
+ * @param event An event name or a list of them.
76
+ * @param target The variable to reduce with the reducers/listeners.
77
+ * @param args A list of parameters to send to the reducers/listeners.
78
+ * @returns A version of the `target` processed by the listeners.
79
+ * @template Target The type of the target.
80
+ * @template Args The type of the parameters to send to the reducers/listeners.
81
+ * @example
82
+ *
83
+ * const events = new EventsHub();
84
+ * events.on('event', async (target, arg0) => {
85
+ * const data = await fetch(`https://api.example.com/${arg0}`);
86
+ * target.push(data);
87
+ * return target;
88
+ * });
89
+ * const result = await events.reduce('event', [], 'Hello');
90
+ * // result would be a list of data fetched from the API.
91
+ *
92
+ */
93
+ reduce<Target, Args extends GenericParams>(event: string | string[], target: Target, ...args: Args): Promise<Target>;
94
+ /**
95
+ * Synchronously reduces a target using an event. It's like emit, but the events
96
+ * listener return a modified (or not) version of the `target`.
97
+ *
98
+ * @param event An event name or a list of them.
99
+ * @param target The variable to reduce with the reducers/listeners.
100
+ * @param args A list of parameters to send to the reducers/listeners.
101
+ * @returns A version of the `target` processed by the listeners.
102
+ * @template Target The type of the target.
103
+ * @template Args The type of the parameters to send to the reducers/listeners.
104
+ * @example
105
+ *
106
+ * const events = new EventsHub();
107
+ * events.on('event', (target, arg0) => {
108
+ * target.push(arg0);
109
+ * return target;
110
+ * });
111
+ * events.reduce('event', [], 'Hello'); // returns ['Hello']
112
+ *
113
+ */
114
+ reduceSync<Target, ReducerArgs extends GenericParams>(event: string | string[], target: Target, ...args: ReducerArgs): Target;
115
+ }
116
+ /**
117
+ * Shorthand for `new EventsHub()`.
118
+ *
119
+ * @returns A new instance of {@link EventsHub}.
120
+ */
121
+ declare const eventsHub: () => EventsHub;
122
+
123
+ export { EventsHub, eventsHub };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@homer0/events-hub",
3
3
  "description": "A simple implementation of a pubsub service for handling events",
4
- "version": "2.0.4",
4
+ "version": "2.0.6",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/homer0/packages.git",
@@ -23,12 +23,12 @@
23
23
  "dist/"
24
24
  ],
25
25
  "devDependencies": {
26
- "@homer0/eslint-plugin": "10.3.0",
27
- "@homer0/tsconfig": "1.0.9",
28
- "jest": "^29.5.0",
29
- "ts-jest": "^29.1.0",
30
- "tsup": "^7.0.0",
31
- "typescript": "^5.1.3"
26
+ "@homer0/eslint-plugin": "10.3.2",
27
+ "@homer0/tsconfig": "1.0.11",
28
+ "jest": "^29.6.1",
29
+ "ts-jest": "^29.1.1",
30
+ "tsup": "^7.1.0",
31
+ "typescript": "^5.1.6"
32
32
  },
33
33
  "engine-strict": true,
34
34
  "engines": {
@@ -44,5 +44,5 @@
44
44
  "build": "tsup src/index.ts",
45
45
  "prepublishOnly": "pnpm run build"
46
46
  },
47
- "gitHead": "d632c6e05b583ceeceff43ed7a52e1e7afbf1265"
47
+ "gitHead": "bab3ed6af9cf27c0d64e4169dc1f4825a978a717"
48
48
  }