@asaidimu/utils-events 1.2.2 → 1.2.3
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 +200 -0
- package/index.d.ts +130 -154
- package/index.js +1 -1
- package/index.mjs +1 -1
- package/package.json +5 -3
- package/index.d.mts +0 -196
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
|
-
|
|
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
|
-
*
|
|
50
|
-
*
|
|
11
|
+
* Maximum number of events to accumulate before forcing a synchronous
|
|
12
|
+
* flush, regardless of the batch delay timer.
|
|
51
13
|
*/
|
|
52
|
-
|
|
14
|
+
size: number;
|
|
53
15
|
/**
|
|
54
|
-
*
|
|
55
|
-
* Only applies
|
|
16
|
+
* Milliseconds to wait (quiet period) before flushing a batch.
|
|
17
|
+
* Only applies when `deferred: true`.
|
|
18
|
+
* @default 16
|
|
56
19
|
*/
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
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
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
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,
|
|
171
|
+
//#endregion
|
|
172
|
+
export { type DebouncerResult, EventBus, EventBusOptions, EventError, EventMetrics, Events, SubscribeOptions, createEventBus };
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
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.
|
|
3
|
+
"version": "1.2.3",
|
|
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": "^1.0.0"
|
|
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 };
|