@asaidimu/utils-events 1.2.2 → 1.2.4

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/index.d.cts ADDED
@@ -0,0 +1,200 @@
1
+ //#region src/sync/debouncer.d.ts
2
+ /**
3
+ * Result when the debounced function successfully executed.
4
+ */
5
+ type DebouncerOk<T> = {
6
+ status: "ok";
7
+ value: T;
8
+ };
9
+ /**
10
+ * Result when the debounced function threw an error.
11
+ */
12
+ type DebouncerError = {
13
+ status: "error";
14
+ error: unknown;
15
+ };
16
+ /**
17
+ * Result when the debounced call was cancelled before execution.
18
+ */
19
+ type DebouncerCancelled = {
20
+ status: "cancelled";
21
+ };
22
+ /**
23
+ * Discriminated union of all possible Debouncer outcomes.
24
+ *
25
+ * - `DebouncerOk<T>`: function ran and returned a value.
26
+ * - `DebouncerError`: function ran and threw an error.
27
+ * - `DebouncerCancelled`: call was cancelled before it ran.
28
+ */
29
+ type DebouncerResult<T> = DebouncerOk<T> | DebouncerError | DebouncerCancelled;
30
+ //#endregion
31
+ //#region src/events/types.d.ts
32
+ /**
33
+ * Options for configuring the EventBus.
34
+ * Each field has an independent default — passing a partial object is safe.
35
+ */
36
+ interface EventBusOptions {
37
+ batch?: {
38
+ /**
39
+ * Maximum number of events to accumulate before forcing a synchronous
40
+ * flush, regardless of the batch delay timer.
41
+ */
42
+ size: number;
43
+ /**
44
+ * Milliseconds to wait (quiet period) before flushing a batch.
45
+ * Only applies when `deferred: true`.
46
+ * @default 16
47
+ */
48
+ delay?: number;
49
+ };
50
+ /**
51
+ * Called when a subscriber callback throws. Defaults to console.error.
52
+ * Errors in one subscriber do not prevent other subscribers from running.
53
+ */
54
+ errorHandler?: (error: EventError) => void;
55
+ /**
56
+ * When set, events are broadcast to other instances via BroadcastChannel.
57
+ * Only applies in environments that support BroadcastChannel.
58
+ */
59
+ broadcast?: {
60
+ /**
61
+ * The BroadcastChannel name used for cross-tab communication.
62
+ * Must be unique per logical bus if you run multiple buses.
63
+ * Only applies when `crossTab: true`.
64
+ */
65
+ channel: string;
66
+ };
67
+ }
68
+ /**
69
+ * Interface defining the shape of the EventBus.
70
+ * @template TEventMap - A record mapping event names to their respective payload types.
71
+ */
72
+ interface EventBus<TEventMap extends Record<string, any>> {
73
+ /**
74
+ * Subscribes to a specific event by name.
75
+ * @param eventName - The name of the event to subscribe to.
76
+ * @param callback - The function to call when the event is emitted.
77
+ * @param options - Extra options to determine the behaviour of the
78
+ * subscription
79
+ * @returns A function to unsubscribe from the event.
80
+ */
81
+ subscribe<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap], event: keyof TEventMap) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
82
+ /**
83
+ * Subscribes to an event and automatically unsubscribes after it fires once.
84
+ * @param eventName - The name of the event to subscribe to.
85
+ * @param callback - The function to call when the event is emitted.
86
+ * @returns A function to cancel the one-shot subscription before it fires.
87
+ */
88
+ once<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap], event: keyof TEventMap) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
89
+ /**
90
+ * Emits an event with a payload to all subscribed listeners.
91
+ * @param event - An object containing the event name and payload.
92
+ */
93
+ emit: <TEventName extends keyof TEventMap>(event: {
94
+ name: TEventName;
95
+ payload: TEventMap[TEventName];
96
+ }) => void;
97
+ /**
98
+ * Retrieves metrics about event bus usage.
99
+ * @returns An object containing various metrics.
100
+ */
101
+ metrics: () => EventMetrics;
102
+ /**
103
+ * Clears all subscriptions and resets metrics.
104
+ *
105
+ * After calling `clear()`, the bus is fully reset and can be reused
106
+ * cross-tab communication is re-established if it was previously enabled.
107
+ *
108
+ * @param options - Optional configuration object.
109
+ * @param options.permanent - If `true`, the bus becomes permanently unusable after clearing.
110
+ * Defaults to `false`.
111
+ * @returns {void}
112
+ */
113
+ clear: (options?: {
114
+ permanent?: boolean;
115
+ }) => void;
116
+ }
117
+ /**
118
+ * Interface defining the metrics tracked by the EventBus.
119
+ */
120
+ interface EventMetrics {
121
+ /** Total number of events emitted (both sync and deferred paths). */
122
+ totalEvents: number;
123
+ /** Number of active subscriptions across all event names. */
124
+ activeSubscriptions: number;
125
+ /** Map of event names to their emission counts. */
126
+ eventCounts: Map<string, number>;
127
+ /** Average duration of event dispatch in milliseconds. */
128
+ averageEmitDuration: number;
129
+ }
130
+ /**
131
+ * Custom error interface for event bus errors.
132
+ * @extends Error
133
+ */
134
+ interface EventError extends Error {
135
+ /** Optional name of the event that caused the error. */
136
+ eventName?: string;
137
+ /** Optional payload that caused the error. */
138
+ payload?: unknown;
139
+ }
140
+ interface SubscribeOptions {
141
+ /**
142
+ * Debounce delay in milliseconds. When multiple events arrive in quick
143
+ * succession, the callback runs only after the quiet period ends, using the
144
+ * latest payload. Default = no debouncing.
145
+ */
146
+ debounce?: number;
147
+ }
148
+ //#endregion
149
+ //#region src/events/events.d.ts
150
+ /**
151
+ * Creates a typed event bus.
152
+ *
153
+ * Options are validated eagerly so misconfiguration surfaces at construction
154
+ * time rather than silently producing wrong behaviour at runtime.
155
+ *
156
+ * The bus now supports a universal wildcard event named `*`. Subscribing to
157
+ * `*` will receive all event payloads. Emitting to `*` is forbidden.
158
+ */
159
+ declare function createEventBus<TEventMap extends Record<string, any>>(options?: EventBusOptions): EventBus<TEventMap>;
160
+ //#endregion
161
+ //#region src/events/event-class.d.ts
162
+ declare class Events<TEventMap extends Record<string, any>> implements EventBus<TEventMap> {
163
+ private bus;
164
+ constructor(options?: EventBusOptions);
165
+ /**
166
+ * Subscribes to a specific event by name.
167
+ * @param eventName - The name of the event to subscribe to.
168
+ * @param callback - The function to call when the event is emitted.
169
+ * @returns A function to unsubscribe from the event.
170
+ */
171
+ subscribe<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap], event: keyof TEventMap) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
172
+ /**
173
+ * Subscribes to an event and automatically unsubscribes after it fires once.
174
+ * @param eventName - The name of the event to subscribe to.
175
+ * @param callback - The function to call when the event is emitted.
176
+ * @returns A function to cancel the one-shot subscription before it fires.
177
+ */
178
+ once<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap], event: keyof TEventMap) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
179
+ /**
180
+ * Emits an event with a payload to all subscribed listeners.
181
+ * @param event - An object containing the event name and payload.
182
+ */
183
+ emit<TEventName extends keyof TEventMap>(event: {
184
+ name: TEventName;
185
+ payload: TEventMap[TEventName];
186
+ }): void;
187
+ /**
188
+ * Retrieves metrics about event bus usage.
189
+ * @returns An object containing various metrics.
190
+ */
191
+ metrics(): EventMetrics;
192
+ /**
193
+ * Clears all subscriptions and resets metrics.
194
+ * After calling clear(), the bus is fully reset and can be reused —
195
+ * cross-tab communication is re-established if it was previously enabled.
196
+ */
197
+ clear(): void;
198
+ }
199
+ //#endregion
200
+ export { type DebouncerResult, EventBus, EventBusOptions, EventError, EventMetrics, Events, SubscribeOptions, createEventBus };
package/index.d.ts CHANGED
@@ -1,149 +1,124 @@
1
- /**
2
- * Result when the debounced function successfully executed.
3
- */
4
- type DebouncerOk<T> = {
5
- status: "ok";
6
- value: T;
7
- };
8
- /**
9
- * Result when the debounced function threw an error.
10
- */
11
- type DebouncerError = {
12
- status: "error";
13
- error: unknown;
14
- };
15
- /**
16
- * Result when the debounced call was cancelled before execution.
17
- */
18
- type DebouncerCancelled = {
19
- status: "cancelled";
20
- };
21
- /**
22
- * Discriminated union of all possible Debouncer outcomes.
23
- *
24
- * - `DebouncerOk<T>`: function ran and returned a value.
25
- * - `DebouncerError`: function ran and threw an error.
26
- * - `DebouncerCancelled`: call was cancelled before it ran.
27
- */
28
- type DebouncerResult<T> = DebouncerOk<T> | DebouncerError | DebouncerCancelled;
1
+ import { DebouncerResult } from "@asaidimu/utils-sync";
29
2
 
3
+ //#region src/events/types.d.ts
30
4
  /**
31
5
  * Options for configuring the EventBus.
32
6
  * Each field has an independent default — passing a partial object is safe.
33
7
  */
34
8
  interface EventBusOptions {
35
- batch?: {
36
- /**
37
- * Maximum number of events to accumulate before forcing a synchronous
38
- * flush, regardless of the batch delay timer.
39
- */
40
- size: number;
41
- /**
42
- * Milliseconds to wait (quiet period) before flushing a batch.
43
- * Only applies when `deferred: true`.
44
- * @default 16
45
- */
46
- delay?: number;
47
- };
9
+ batch?: {
48
10
  /**
49
- * Called when a subscriber callback throws. Defaults to console.error.
50
- * Errors in one subscriber do not prevent other subscribers from running.
11
+ * Maximum number of events to accumulate before forcing a synchronous
12
+ * flush, regardless of the batch delay timer.
51
13
  */
52
- errorHandler?: (error: EventError) => void;
14
+ size: number;
53
15
  /**
54
- * When set, events are broadcast to other instances via BroadcastChannel.
55
- * Only applies in environments that support BroadcastChannel.
16
+ * Milliseconds to wait (quiet period) before flushing a batch.
17
+ * Only applies when `deferred: true`.
18
+ * @default 16
56
19
  */
57
- broadcast?: {
58
- /**
59
- * The BroadcastChannel name used for cross-tab communication.
60
- * Must be unique per logical bus if you run multiple buses.
61
- * Only applies when `crossTab: true`.
62
- */
63
- channel: string;
64
- };
20
+ delay?: number;
21
+ };
22
+ /**
23
+ * Called when a subscriber callback throws. Defaults to console.error.
24
+ * Errors in one subscriber do not prevent other subscribers from running.
25
+ */
26
+ errorHandler?: (error: EventError) => void;
27
+ /**
28
+ * When set, events are broadcast to other instances via BroadcastChannel.
29
+ * Only applies in environments that support BroadcastChannel.
30
+ */
31
+ broadcast?: {
32
+ /**
33
+ * The BroadcastChannel name used for cross-tab communication.
34
+ * Must be unique per logical bus if you run multiple buses.
35
+ * Only applies when `crossTab: true`.
36
+ */
37
+ channel: string;
38
+ };
65
39
  }
66
40
  /**
67
41
  * Interface defining the shape of the EventBus.
68
42
  * @template TEventMap - A record mapping event names to their respective payload types.
69
43
  */
70
44
  interface EventBus<TEventMap extends Record<string, any>> {
71
- /**
72
- * Subscribes to a specific event by name.
73
- * @param eventName - The name of the event to subscribe to.
74
- * @param callback - The function to call when the event is emitted.
75
- * @param options - Extra options to determine the behaviour of the
76
- * subscription
77
- * @returns A function to unsubscribe from the event.
78
- */
79
- subscribe<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap], event: keyof TEventMap) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
80
- /**
81
- * Subscribes to an event and automatically unsubscribes after it fires once.
82
- * @param eventName - The name of the event to subscribe to.
83
- * @param callback - The function to call when the event is emitted.
84
- * @returns A function to cancel the one-shot subscription before it fires.
85
- */
86
- once<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap], event: keyof TEventMap) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
87
- /**
88
- * Emits an event with a payload to all subscribed listeners.
89
- * @param event - An object containing the event name and payload.
90
- */
91
- emit: <TEventName extends keyof TEventMap>(event: {
92
- name: TEventName;
93
- payload: TEventMap[TEventName];
94
- }) => void;
95
- /**
96
- * Retrieves metrics about event bus usage.
97
- * @returns An object containing various metrics.
98
- */
99
- metrics: () => EventMetrics;
100
- /**
101
- * Clears all subscriptions and resets metrics.
102
- *
103
- * After calling `clear()`, the bus is fully reset and can be reused
104
- * cross-tab communication is re-established if it was previously enabled.
105
- *
106
- * @param options - Optional configuration object.
107
- * @param options.permanent - If `true`, the bus becomes permanently unusable after clearing.
108
- * Defaults to `false`.
109
- * @returns {void}
110
- */
111
- clear: (options?: {
112
- permanent?: boolean;
113
- }) => void;
45
+ /**
46
+ * Subscribes to a specific event by name.
47
+ * @param eventName - The name of the event to subscribe to.
48
+ * @param callback - The function to call when the event is emitted.
49
+ * @param options - Extra options to determine the behaviour of the
50
+ * subscription
51
+ * @returns A function to unsubscribe from the event.
52
+ */
53
+ subscribe<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap], event: keyof TEventMap) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
54
+ /**
55
+ * Subscribes to an event and automatically unsubscribes after it fires once.
56
+ * @param eventName - The name of the event to subscribe to.
57
+ * @param callback - The function to call when the event is emitted.
58
+ * @returns A function to cancel the one-shot subscription before it fires.
59
+ */
60
+ once<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap], event: keyof TEventMap) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
61
+ /**
62
+ * Emits an event with a payload to all subscribed listeners.
63
+ * @param event - An object containing the event name and payload.
64
+ */
65
+ emit: <TEventName extends keyof TEventMap>(event: {
66
+ name: TEventName;
67
+ payload: TEventMap[TEventName];
68
+ }) => void;
69
+ /**
70
+ * Retrieves metrics about event bus usage.
71
+ * @returns An object containing various metrics.
72
+ */
73
+ metrics: () => EventMetrics;
74
+ /**
75
+ * Clears all subscriptions and resets metrics.
76
+ *
77
+ * After calling `clear()`, the bus is fully reset and can be reused
78
+ * cross-tab communication is re-established if it was previously enabled.
79
+ *
80
+ * @param options - Optional configuration object.
81
+ * @param options.permanent - If `true`, the bus becomes permanently unusable after clearing.
82
+ * Defaults to `false`.
83
+ * @returns {void}
84
+ */
85
+ clear: (options?: {
86
+ permanent?: boolean;
87
+ }) => void;
114
88
  }
115
89
  /**
116
90
  * Interface defining the metrics tracked by the EventBus.
117
91
  */
118
92
  interface EventMetrics {
119
- /** Total number of events emitted (both sync and deferred paths). */
120
- totalEvents: number;
121
- /** Number of active subscriptions across all event names. */
122
- activeSubscriptions: number;
123
- /** Map of event names to their emission counts. */
124
- eventCounts: Map<string, number>;
125
- /** Average duration of event dispatch in milliseconds. */
126
- averageEmitDuration: number;
93
+ /** Total number of events emitted (both sync and deferred paths). */
94
+ totalEvents: number;
95
+ /** Number of active subscriptions across all event names. */
96
+ activeSubscriptions: number;
97
+ /** Map of event names to their emission counts. */
98
+ eventCounts: Map<string, number>;
99
+ /** Average duration of event dispatch in milliseconds. */
100
+ averageEmitDuration: number;
127
101
  }
128
102
  /**
129
103
  * Custom error interface for event bus errors.
130
104
  * @extends Error
131
105
  */
132
106
  interface EventError extends Error {
133
- /** Optional name of the event that caused the error. */
134
- eventName?: string;
135
- /** Optional payload that caused the error. */
136
- payload?: unknown;
107
+ /** Optional name of the event that caused the error. */
108
+ eventName?: string;
109
+ /** Optional payload that caused the error. */
110
+ payload?: unknown;
137
111
  }
138
112
  interface SubscribeOptions {
139
- /**
140
- * Debounce delay in milliseconds. When multiple events arrive in quick
141
- * succession, the callback runs only after the quiet period ends, using the
142
- * latest payload. Default = no debouncing.
143
- */
144
- debounce?: number;
113
+ /**
114
+ * Debounce delay in milliseconds. When multiple events arrive in quick
115
+ * succession, the callback runs only after the quiet period ends, using the
116
+ * latest payload. Default = no debouncing.
117
+ */
118
+ debounce?: number;
145
119
  }
146
-
120
+ //#endregion
121
+ //#region src/events/events.d.ts
147
122
  /**
148
123
  * Creates a typed event bus.
149
124
  *
@@ -154,43 +129,44 @@ interface SubscribeOptions {
154
129
  * `*` will receive all event payloads. Emitting to `*` is forbidden.
155
130
  */
156
131
  declare function createEventBus<TEventMap extends Record<string, any>>(options?: EventBusOptions): EventBus<TEventMap>;
157
-
132
+ //#endregion
133
+ //#region src/events/event-class.d.ts
158
134
  declare class Events<TEventMap extends Record<string, any>> implements EventBus<TEventMap> {
159
- private bus;
160
- constructor(options?: EventBusOptions);
161
- /**
162
- * Subscribes to a specific event by name.
163
- * @param eventName - The name of the event to subscribe to.
164
- * @param callback - The function to call when the event is emitted.
165
- * @returns A function to unsubscribe from the event.
166
- */
167
- subscribe<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap], event: keyof TEventMap) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
168
- /**
169
- * Subscribes to an event and automatically unsubscribes after it fires once.
170
- * @param eventName - The name of the event to subscribe to.
171
- * @param callback - The function to call when the event is emitted.
172
- * @returns A function to cancel the one-shot subscription before it fires.
173
- */
174
- once<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap], event: keyof TEventMap) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
175
- /**
176
- * Emits an event with a payload to all subscribed listeners.
177
- * @param event - An object containing the event name and payload.
178
- */
179
- emit<TEventName extends keyof TEventMap>(event: {
180
- name: TEventName;
181
- payload: TEventMap[TEventName];
182
- }): void;
183
- /**
184
- * Retrieves metrics about event bus usage.
185
- * @returns An object containing various metrics.
186
- */
187
- metrics(): EventMetrics;
188
- /**
189
- * Clears all subscriptions and resets metrics.
190
- * After calling clear(), the bus is fully reset and can be reused —
191
- * cross-tab communication is re-established if it was previously enabled.
192
- */
193
- clear(): void;
135
+ private bus;
136
+ constructor(options?: EventBusOptions);
137
+ /**
138
+ * Subscribes to a specific event by name.
139
+ * @param eventName - The name of the event to subscribe to.
140
+ * @param callback - The function to call when the event is emitted.
141
+ * @returns A function to unsubscribe from the event.
142
+ */
143
+ subscribe<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap], event: keyof TEventMap) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
144
+ /**
145
+ * Subscribes to an event and automatically unsubscribes after it fires once.
146
+ * @param eventName - The name of the event to subscribe to.
147
+ * @param callback - The function to call when the event is emitted.
148
+ * @returns A function to cancel the one-shot subscription before it fires.
149
+ */
150
+ once<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap], event: keyof TEventMap) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
151
+ /**
152
+ * Emits an event with a payload to all subscribed listeners.
153
+ * @param event - An object containing the event name and payload.
154
+ */
155
+ emit<TEventName extends keyof TEventMap>(event: {
156
+ name: TEventName;
157
+ payload: TEventMap[TEventName];
158
+ }): void;
159
+ /**
160
+ * Retrieves metrics about event bus usage.
161
+ * @returns An object containing various metrics.
162
+ */
163
+ metrics(): EventMetrics;
164
+ /**
165
+ * Clears all subscriptions and resets metrics.
166
+ * After calling clear(), the bus is fully reset and can be reused —
167
+ * cross-tab communication is re-established if it was previously enabled.
168
+ */
169
+ clear(): void;
194
170
  }
195
-
196
- export { type DebouncerResult, type EventBus, type EventBusOptions, type EventError, type EventMetrics, Events, type SubscribeOptions, createEventBus };
171
+ //#endregion
172
+ export { type DebouncerResult, EventBus, EventBusOptions, EventError, EventMetrics, Events, SubscribeOptions, createEventBus };
package/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var e=class{_delay;_leading;_timer;_pendingFn;_pendingResolvers=[];_leadingFired=!1;constructor(e){this._delay=e?.delay??300,this._leading=e?.leading??!1}do(e){return new Promise(t=>{this._enqueue(e,t)})}fire(e){this._enqueue(e,void 0)}_enqueue(e,t){if(this._pendingFn=e,t&&this._pendingResolvers.push(t),this._leading&&!this._leadingFired)return this._leadingFired=!0,this._fire(),clearTimeout(this._timer),void(this._timer=setTimeout(()=>{this._leadingFired=!1,void 0!==this._pendingFn&&this._fire()},this._delay));clearTimeout(this._timer),this._timer=setTimeout(()=>{this._leadingFired=!1,this._fire()},this._delay)}cancel(){clearTimeout(this._timer),this._timer=void 0,this._pendingFn=void 0,this._leadingFired=!1;const e=this._pendingResolvers.splice(0);for(const t of e)t({status:"cancelled"})}async flush(){return this._pendingFn?(clearTimeout(this._timer),this._timer=void 0,this._leadingFired=!1,this._fire()):null}pending(){return void 0!==this._pendingFn}async _fire(){const e=this._pendingFn,t=this._pendingResolvers.splice(0);let n;this._pendingFn=void 0;try{n={status:"ok",value:await e()}}catch(e){n={status:"error",error:e}}for(const e of t)e(n);return n}};function t(t){const n=t??{},s=n.errorHandler??(e=>console.error("EventBus Error:",e)),r=null!=n.batch?.size,i=n.batch?.size,o=n.batch?.delay??16;if(r&&(i<=0||!Number.isFinite(i)))throw new Error(`EventBus: batch.size must be a positive finite number, got ${i}.`);if(r&&(o<0||!Number.isFinite(o)))throw new Error(`EventBus: batch.delay must be a non-negative finite number, got ${o}.`);const a=n.broadcast?.channel,c=null!=a&&a.length>0,l=new Map,u=new Map;let d=[];const h=r?new e({delay:o}):null;let f=0,m=0;const g=new Map,p=()=>{if(!c)return null;if("undefined"==typeof BroadcastChannel)return console.warn("EventBus: BroadcastChannel is not supported in this environment. Cross-tab notifications are disabled."),null;const e=new BroadcastChannel(a);return e.onmessage=e=>{const{name:t,payload:n}=e.data;v(t,n)},e};let b=p();const _=(e,t)=>{f++,m+=t,g.set(e,(g.get(e)??0)+1)},v=(e,t)=>{const n=l.get(e);if(n&&n.size>0)for(const{callback:r}of Array.from(n.values()))try{r(t)}catch(n){const r=n instanceof Error?n:Object.assign(new Error(String(n)),{cause:n}),i=Object.assign(r,{eventName:e,payload:t});s(i)}if(u.size>0)for(const{callback:n}of Array.from(u.values()))try{n(t&&"object"==typeof t?{...t}:t,e)}catch(e){const n=e instanceof Error?e:Object.assign(new Error(String(e)),{cause:e}),r=Object.assign(n,{eventName:"*",payload:t});s(r)}},y=(t,n,s)=>{let r,i;void 0!==s&&s>0?(i=new e({delay:s,leading:!1}),r=(e,t)=>{i.fire(()=>{void 0!==t?n(e,t):n(e)})}):r=n;const o={callback:r,debouncer:i};if("*"===t)return u.set(n,o),()=>{const e=u.get(n);e&&(e.debouncer?.cancel(),u.delete(n))};{l.has(t)||l.set(t,new Map);const e=l.get(t);return e.set(n,o),()=>{const s=e.get(n);s&&(s.debouncer?.cancel(),e.delete(n),0===e.size&&l.delete(t))}}},w=()=>{const e=d;d=[];for(const{name:t,payload:n}of e){const e=performance.now();v(t,n),_(t,performance.now()-e)}};return{subscribe:(e,t,n)=>y(e,t,n?.debounce),once(e,t,n){let s;return s=y(e,(e,n)=>{s(),n?t(e,n):t(e)},n?.debounce),s},emit({name:e,payload:t}){if("*"===e)throw new Error('EventBus: Emitting to wildcard event "*" is forbidden.');if(r)return d.push({name:e,payload:t}),d.length>=i?(h.cancel(),void w()):(h.fire(()=>w()),void b?.postMessage({name:e,payload:t}));const n=performance.now();v(e,t),_(e,performance.now()-n),b?.postMessage({name:e,payload:t})},metrics:()=>({totalEvents:f,activeSubscriptions:Array.from(l.values()).reduce((e,t)=>e+t.size,0)+u.size,eventCounts:new Map(g),averageEmitDuration:f>0?m/f:0}),clear:e=>{h?.cancel(),d=[];for(const e of l.values())for(const{debouncer:t}of e.values())t?.cancel();l.clear();for(const{debouncer:e}of u.values())e?.cancel();u.clear(),f=0,m=0,g.clear(),b?.close(),b=e?.permanent?null:p()}}}exports.Events=class{bus;constructor(e){this.bus=t(e)}subscribe(e,t,n){return this.bus.subscribe(e,t,n)}once(e,t,n){return this.bus.once(e,t,n)}emit(e){return this.bus.emit(e)}metrics(){return this.bus.metrics()}clear(){return this.bus.clear()}},exports.createEventBus=t;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("@asaidimu/utils-sync");function t(t){let n=t??{},r=n.errorHandler??(e=>console.error(`EventBus Error:`,e)),i=n.batch?.size!=null,a=n.batch?.size,o=n.batch?.delay??16;if(i&&(a<=0||!Number.isFinite(a)))throw Error(`EventBus: batch.size must be a positive finite number, got ${a}.`);if(i&&(o<0||!Number.isFinite(o)))throw Error(`EventBus: batch.delay must be a non-negative finite number, got ${o}.`);let s=n.broadcast?.channel,c=s!=null&&s.length>0,l=new Map,u=new Map,d=[],f=i?new e.Debouncer({delay:o}):null,p=0,m=0,h=new Map,g=()=>{if(!c)return null;if(typeof BroadcastChannel>`u`)return console.warn(`EventBus: BroadcastChannel is not supported in this environment. Cross-tab notifications are disabled.`),null;let e=new BroadcastChannel(s);return e.onmessage=e=>{let{name:t,payload:n}=e.data;y(t,n)},e},_=g(),v=(e,t)=>{p++,m+=t,h.set(e,(h.get(e)??0)+1)},y=(e,t)=>{let n=l.get(e);if(n&&n.size>0)for(let{callback:i}of Array.from(n.values()))try{i(t)}catch(n){let i=n instanceof Error?n:Object.assign(Error(String(n)),{cause:n});r(Object.assign(i,{eventName:e,payload:t}))}if(u.size>0)for(let{callback:n}of Array.from(u.values()))try{n(t&&typeof t==`object`?{...t}:t,e)}catch(e){let n=e instanceof Error?e:Object.assign(Error(String(e)),{cause:e});r(Object.assign(n,{eventName:`*`,payload:t}))}},b=(t,n,r)=>{let i,a;r!==void 0&&r>0?(a=new e.Debouncer({delay:r,leading:!1}),i=(e,t)=>{a.fire(()=>{t===void 0?n(e):n(e,t)})}):i=n;let o={callback:i,debouncer:a};if(t===`*`)return u.set(n,o),()=>{let e=u.get(n);e&&(e.debouncer?.cancel(),u.delete(n))};{l.has(t)||l.set(t,new Map);let e=l.get(t);return e.set(n,o),()=>{let r=e.get(n);r&&(r.debouncer?.cancel(),e.delete(n),e.size===0&&l.delete(t))}}},x=()=>{let e=d;d=[];for(let{name:t,payload:n}of e){let e=performance.now();y(t,n),v(t,performance.now()-e)}};return{subscribe(e,t,n){return b(e,t,n?.debounce)},once(e,t,n){let r;return r=b(e,(e,n)=>{r(),n?t(e,n):t(e)},n?.debounce),r},emit({name:e,payload:t}){if(e===`*`)throw Error(`EventBus: Emitting to wildcard event "*" is forbidden.`);if(i){if(d.push({name:e,payload:t}),d.length>=a){f.cancel(),x();return}f.fire(()=>x()),_?.postMessage({name:e,payload:t});return}let n=performance.now();y(e,t),v(e,performance.now()-n),_?.postMessage({name:e,payload:t})},metrics:()=>({totalEvents:p,activeSubscriptions:Array.from(l.values()).reduce((e,t)=>e+t.size,0)+u.size,eventCounts:new Map(h),averageEmitDuration:p>0?m/p:0}),clear:e=>{f?.cancel(),d=[];for(let e of l.values())for(let{debouncer:t}of e.values())t?.cancel();l.clear();for(let{debouncer:e}of u.values())e?.cancel();u.clear(),p=0,m=0,h.clear(),_?.close(),_=e?.permanent?null:g()}}}var n=class{bus;constructor(e){this.bus=t(e)}subscribe(e,t,n){return this.bus.subscribe(e,t,n)}once(e,t,n){return this.bus.once(e,t,n)}emit(e){return this.bus.emit(e)}metrics(){return this.bus.metrics()}clear(){return this.bus.clear()}};exports.Events=n,exports.createEventBus=t;
package/index.mjs CHANGED
@@ -1 +1 @@
1
- var e=class{_delay;_leading;_timer;_pendingFn;_pendingResolvers=[];_leadingFired=!1;constructor(e){this._delay=e?.delay??300,this._leading=e?.leading??!1}do(e){return new Promise(n=>{this._enqueue(e,n)})}fire(e){this._enqueue(e,void 0)}_enqueue(e,n){if(this._pendingFn=e,n&&this._pendingResolvers.push(n),this._leading&&!this._leadingFired)return this._leadingFired=!0,this._fire(),clearTimeout(this._timer),void(this._timer=setTimeout(()=>{this._leadingFired=!1,void 0!==this._pendingFn&&this._fire()},this._delay));clearTimeout(this._timer),this._timer=setTimeout(()=>{this._leadingFired=!1,this._fire()},this._delay)}cancel(){clearTimeout(this._timer),this._timer=void 0,this._pendingFn=void 0,this._leadingFired=!1;const e=this._pendingResolvers.splice(0);for(const n of e)n({status:"cancelled"})}async flush(){return this._pendingFn?(clearTimeout(this._timer),this._timer=void 0,this._leadingFired=!1,this._fire()):null}pending(){return void 0!==this._pendingFn}async _fire(){const e=this._pendingFn,n=this._pendingResolvers.splice(0);let t;this._pendingFn=void 0;try{t={status:"ok",value:await e()}}catch(e){t={status:"error",error:e}}for(const e of n)e(t);return t}};function n(n){const t=n??{},r=t.errorHandler??(e=>console.error("EventBus Error:",e)),s=null!=t.batch?.size,i=t.batch?.size,o=t.batch?.delay??16;if(s&&(i<=0||!Number.isFinite(i)))throw new Error(`EventBus: batch.size must be a positive finite number, got ${i}.`);if(s&&(o<0||!Number.isFinite(o)))throw new Error(`EventBus: batch.delay must be a non-negative finite number, got ${o}.`);const a=t.broadcast?.channel,c=null!=a&&a.length>0,l=new Map,d=new Map;let u=[];const h=s?new e({delay:o}):null;let f=0,m=0;const g=new Map,b=()=>{if(!c)return null;if("undefined"==typeof BroadcastChannel)return console.warn("EventBus: BroadcastChannel is not supported in this environment. Cross-tab notifications are disabled."),null;const e=new BroadcastChannel(a);return e.onmessage=e=>{const{name:n,payload:t}=e.data;v(n,t)},e};let p=b();const _=(e,n)=>{f++,m+=n,g.set(e,(g.get(e)??0)+1)},v=(e,n)=>{const t=l.get(e);if(t&&t.size>0)for(const{callback:s}of Array.from(t.values()))try{s(n)}catch(t){const s=t instanceof Error?t:Object.assign(new Error(String(t)),{cause:t}),i=Object.assign(s,{eventName:e,payload:n});r(i)}if(d.size>0)for(const{callback:t}of Array.from(d.values()))try{t(n&&"object"==typeof n?{...n}:n,e)}catch(e){const t=e instanceof Error?e:Object.assign(new Error(String(e)),{cause:e}),s=Object.assign(t,{eventName:"*",payload:n});r(s)}},y=(n,t,r)=>{let s,i;void 0!==r&&r>0?(i=new e({delay:r,leading:!1}),s=(e,n)=>{i.fire(()=>{void 0!==n?t(e,n):t(e)})}):s=t;const o={callback:s,debouncer:i};if("*"===n)return d.set(t,o),()=>{const e=d.get(t);e&&(e.debouncer?.cancel(),d.delete(t))};{l.has(n)||l.set(n,new Map);const e=l.get(n);return e.set(t,o),()=>{const r=e.get(t);r&&(r.debouncer?.cancel(),e.delete(t),0===e.size&&l.delete(n))}}},w=()=>{const e=u;u=[];for(const{name:n,payload:t}of e){const e=performance.now();v(n,t),_(n,performance.now()-e)}};return{subscribe:(e,n,t)=>y(e,n,t?.debounce),once(e,n,t){let r;return r=y(e,(e,t)=>{r(),t?n(e,t):n(e)},t?.debounce),r},emit({name:e,payload:n}){if("*"===e)throw new Error('EventBus: Emitting to wildcard event "*" is forbidden.');if(s)return u.push({name:e,payload:n}),u.length>=i?(h.cancel(),void w()):(h.fire(()=>w()),void p?.postMessage({name:e,payload:n}));const t=performance.now();v(e,n),_(e,performance.now()-t),p?.postMessage({name:e,payload:n})},metrics:()=>({totalEvents:f,activeSubscriptions:Array.from(l.values()).reduce((e,n)=>e+n.size,0)+d.size,eventCounts:new Map(g),averageEmitDuration:f>0?m/f:0}),clear:e=>{h?.cancel(),u=[];for(const e of l.values())for(const{debouncer:n}of e.values())n?.cancel();l.clear();for(const{debouncer:e}of d.values())e?.cancel();d.clear(),f=0,m=0,g.clear(),p?.close(),p=e?.permanent?null:b()}}}var t=class{bus;constructor(e){this.bus=n(e)}subscribe(e,n,t){return this.bus.subscribe(e,n,t)}once(e,n,t){return this.bus.once(e,n,t)}emit(e){return this.bus.emit(e)}metrics(){return this.bus.metrics()}clear(){return this.bus.clear()}};export{t as Events,n as createEventBus};
1
+ import{Debouncer as e}from"@asaidimu/utils-sync";function t(t){let n=t??{},r=n.errorHandler??(e=>console.error(`EventBus Error:`,e)),i=n.batch?.size!=null,a=n.batch?.size,o=n.batch?.delay??16;if(i&&(a<=0||!Number.isFinite(a)))throw Error(`EventBus: batch.size must be a positive finite number, got ${a}.`);if(i&&(o<0||!Number.isFinite(o)))throw Error(`EventBus: batch.delay must be a non-negative finite number, got ${o}.`);let s=n.broadcast?.channel,c=s!=null&&s.length>0,l=new Map,u=new Map,d=[],f=i?new e({delay:o}):null,p=0,m=0,h=new Map,g=()=>{if(!c)return null;if(typeof BroadcastChannel>`u`)return console.warn(`EventBus: BroadcastChannel is not supported in this environment. Cross-tab notifications are disabled.`),null;let e=new BroadcastChannel(s);return e.onmessage=e=>{let{name:t,payload:n}=e.data;y(t,n)},e},_=g(),v=(e,t)=>{p++,m+=t,h.set(e,(h.get(e)??0)+1)},y=(e,t)=>{let n=l.get(e);if(n&&n.size>0)for(let{callback:i}of Array.from(n.values()))try{i(t)}catch(n){let i=n instanceof Error?n:Object.assign(Error(String(n)),{cause:n});r(Object.assign(i,{eventName:e,payload:t}))}if(u.size>0)for(let{callback:n}of Array.from(u.values()))try{n(t&&typeof t==`object`?{...t}:t,e)}catch(e){let n=e instanceof Error?e:Object.assign(Error(String(e)),{cause:e});r(Object.assign(n,{eventName:`*`,payload:t}))}},b=(t,n,r)=>{let i,a;r!==void 0&&r>0?(a=new e({delay:r,leading:!1}),i=(e,t)=>{a.fire(()=>{t===void 0?n(e):n(e,t)})}):i=n;let o={callback:i,debouncer:a};if(t===`*`)return u.set(n,o),()=>{let e=u.get(n);e&&(e.debouncer?.cancel(),u.delete(n))};{l.has(t)||l.set(t,new Map);let e=l.get(t);return e.set(n,o),()=>{let r=e.get(n);r&&(r.debouncer?.cancel(),e.delete(n),e.size===0&&l.delete(t))}}},x=()=>{let e=d;d=[];for(let{name:t,payload:n}of e){let e=performance.now();y(t,n),v(t,performance.now()-e)}};return{subscribe(e,t,n){return b(e,t,n?.debounce)},once(e,t,n){let r;return r=b(e,(e,n)=>{r(),n?t(e,n):t(e)},n?.debounce),r},emit({name:e,payload:t}){if(e===`*`)throw Error(`EventBus: Emitting to wildcard event "*" is forbidden.`);if(i){if(d.push({name:e,payload:t}),d.length>=a){f.cancel(),x();return}f.fire(()=>x()),_?.postMessage({name:e,payload:t});return}let n=performance.now();y(e,t),v(e,performance.now()-n),_?.postMessage({name:e,payload:t})},metrics:()=>({totalEvents:p,activeSubscriptions:Array.from(l.values()).reduce((e,t)=>e+t.size,0)+u.size,eventCounts:new Map(h),averageEmitDuration:p>0?m/p:0}),clear:e=>{f?.cancel(),d=[];for(let e of l.values())for(let{debouncer:t}of e.values())t?.cancel();l.clear();for(let{debouncer:e}of u.values())e?.cancel();u.clear(),p=0,m=0,h.clear(),_?.close(),_=e?.permanent?null:g()}}}var n=class{bus;constructor(e){this.bus=t(e)}subscribe(e,t,n){return this.bus.subscribe(e,t,n)}once(e,t,n){return this.bus.once(e,t,n)}emit(e){return this.bus.emit(e)}metrics(){return this.bus.metrics()}clear(){return this.bus.clear()}};export{n as Events,t as createEventBus};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asaidimu/utils-events",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
4
4
  "description": "A lightweight, type-safe event bus implementation for TypeScript applications.",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",
@@ -35,7 +35,8 @@
35
35
  }
36
36
  },
37
37
  "dependencies": {
38
- "uuid": "^14.0.0"
38
+ "uuid": "^14.0.0",
39
+ "@asaidimu/utils-sync": "^2.3.2"
39
40
  },
40
41
  "publishConfig": {
41
42
  "registry": "https://registry.npmjs.org/",
@@ -61,5 +62,6 @@
61
62
  }
62
63
  ]
63
64
  ]
64
- }
65
+ },
66
+ "peerDependencies": {}
65
67
  }
package/index.d.mts DELETED
@@ -1,196 +0,0 @@
1
- /**
2
- * Result when the debounced function successfully executed.
3
- */
4
- type DebouncerOk<T> = {
5
- status: "ok";
6
- value: T;
7
- };
8
- /**
9
- * Result when the debounced function threw an error.
10
- */
11
- type DebouncerError = {
12
- status: "error";
13
- error: unknown;
14
- };
15
- /**
16
- * Result when the debounced call was cancelled before execution.
17
- */
18
- type DebouncerCancelled = {
19
- status: "cancelled";
20
- };
21
- /**
22
- * Discriminated union of all possible Debouncer outcomes.
23
- *
24
- * - `DebouncerOk<T>`: function ran and returned a value.
25
- * - `DebouncerError`: function ran and threw an error.
26
- * - `DebouncerCancelled`: call was cancelled before it ran.
27
- */
28
- type DebouncerResult<T> = DebouncerOk<T> | DebouncerError | DebouncerCancelled;
29
-
30
- /**
31
- * Options for configuring the EventBus.
32
- * Each field has an independent default — passing a partial object is safe.
33
- */
34
- interface EventBusOptions {
35
- batch?: {
36
- /**
37
- * Maximum number of events to accumulate before forcing a synchronous
38
- * flush, regardless of the batch delay timer.
39
- */
40
- size: number;
41
- /**
42
- * Milliseconds to wait (quiet period) before flushing a batch.
43
- * Only applies when `deferred: true`.
44
- * @default 16
45
- */
46
- delay?: number;
47
- };
48
- /**
49
- * Called when a subscriber callback throws. Defaults to console.error.
50
- * Errors in one subscriber do not prevent other subscribers from running.
51
- */
52
- errorHandler?: (error: EventError) => void;
53
- /**
54
- * When set, events are broadcast to other instances via BroadcastChannel.
55
- * Only applies in environments that support BroadcastChannel.
56
- */
57
- broadcast?: {
58
- /**
59
- * The BroadcastChannel name used for cross-tab communication.
60
- * Must be unique per logical bus if you run multiple buses.
61
- * Only applies when `crossTab: true`.
62
- */
63
- channel: string;
64
- };
65
- }
66
- /**
67
- * Interface defining the shape of the EventBus.
68
- * @template TEventMap - A record mapping event names to their respective payload types.
69
- */
70
- interface EventBus<TEventMap extends Record<string, any>> {
71
- /**
72
- * Subscribes to a specific event by name.
73
- * @param eventName - The name of the event to subscribe to.
74
- * @param callback - The function to call when the event is emitted.
75
- * @param options - Extra options to determine the behaviour of the
76
- * subscription
77
- * @returns A function to unsubscribe from the event.
78
- */
79
- subscribe<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap], event: keyof TEventMap) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
80
- /**
81
- * Subscribes to an event and automatically unsubscribes after it fires once.
82
- * @param eventName - The name of the event to subscribe to.
83
- * @param callback - The function to call when the event is emitted.
84
- * @returns A function to cancel the one-shot subscription before it fires.
85
- */
86
- once<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap], event: keyof TEventMap) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
87
- /**
88
- * Emits an event with a payload to all subscribed listeners.
89
- * @param event - An object containing the event name and payload.
90
- */
91
- emit: <TEventName extends keyof TEventMap>(event: {
92
- name: TEventName;
93
- payload: TEventMap[TEventName];
94
- }) => void;
95
- /**
96
- * Retrieves metrics about event bus usage.
97
- * @returns An object containing various metrics.
98
- */
99
- metrics: () => EventMetrics;
100
- /**
101
- * Clears all subscriptions and resets metrics.
102
- *
103
- * After calling `clear()`, the bus is fully reset and can be reused
104
- * cross-tab communication is re-established if it was previously enabled.
105
- *
106
- * @param options - Optional configuration object.
107
- * @param options.permanent - If `true`, the bus becomes permanently unusable after clearing.
108
- * Defaults to `false`.
109
- * @returns {void}
110
- */
111
- clear: (options?: {
112
- permanent?: boolean;
113
- }) => void;
114
- }
115
- /**
116
- * Interface defining the metrics tracked by the EventBus.
117
- */
118
- interface EventMetrics {
119
- /** Total number of events emitted (both sync and deferred paths). */
120
- totalEvents: number;
121
- /** Number of active subscriptions across all event names. */
122
- activeSubscriptions: number;
123
- /** Map of event names to their emission counts. */
124
- eventCounts: Map<string, number>;
125
- /** Average duration of event dispatch in milliseconds. */
126
- averageEmitDuration: number;
127
- }
128
- /**
129
- * Custom error interface for event bus errors.
130
- * @extends Error
131
- */
132
- interface EventError extends Error {
133
- /** Optional name of the event that caused the error. */
134
- eventName?: string;
135
- /** Optional payload that caused the error. */
136
- payload?: unknown;
137
- }
138
- interface SubscribeOptions {
139
- /**
140
- * Debounce delay in milliseconds. When multiple events arrive in quick
141
- * succession, the callback runs only after the quiet period ends, using the
142
- * latest payload. Default = no debouncing.
143
- */
144
- debounce?: number;
145
- }
146
-
147
- /**
148
- * Creates a typed event bus.
149
- *
150
- * Options are validated eagerly so misconfiguration surfaces at construction
151
- * time rather than silently producing wrong behaviour at runtime.
152
- *
153
- * The bus now supports a universal wildcard event named `*`. Subscribing to
154
- * `*` will receive all event payloads. Emitting to `*` is forbidden.
155
- */
156
- declare function createEventBus<TEventMap extends Record<string, any>>(options?: EventBusOptions): EventBus<TEventMap>;
157
-
158
- declare class Events<TEventMap extends Record<string, any>> implements EventBus<TEventMap> {
159
- private bus;
160
- constructor(options?: EventBusOptions);
161
- /**
162
- * Subscribes to a specific event by name.
163
- * @param eventName - The name of the event to subscribe to.
164
- * @param callback - The function to call when the event is emitted.
165
- * @returns A function to unsubscribe from the event.
166
- */
167
- subscribe<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap], event: keyof TEventMap) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
168
- /**
169
- * Subscribes to an event and automatically unsubscribes after it fires once.
170
- * @param eventName - The name of the event to subscribe to.
171
- * @param callback - The function to call when the event is emitted.
172
- * @returns A function to cancel the one-shot subscription before it fires.
173
- */
174
- once<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap], event: keyof TEventMap) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
175
- /**
176
- * Emits an event with a payload to all subscribed listeners.
177
- * @param event - An object containing the event name and payload.
178
- */
179
- emit<TEventName extends keyof TEventMap>(event: {
180
- name: TEventName;
181
- payload: TEventMap[TEventName];
182
- }): void;
183
- /**
184
- * Retrieves metrics about event bus usage.
185
- * @returns An object containing various metrics.
186
- */
187
- metrics(): EventMetrics;
188
- /**
189
- * Clears all subscriptions and resets metrics.
190
- * After calling clear(), the bus is fully reset and can be reused —
191
- * cross-tab communication is re-established if it was previously enabled.
192
- */
193
- clear(): void;
194
- }
195
-
196
- export { type DebouncerResult, type EventBus, type EventBusOptions, type EventError, type EventMetrics, Events, type SubscribeOptions, createEventBus };