evnty 2.1.105 → 3.0.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.
- package/README.md +130 -19
- package/build/index.cjs +167 -28
- package/build/index.cjs.map +1 -1
- package/build/index.d.ts +356 -98
- package/build/index.js +161 -25
- package/build/index.js.map +1 -1
- package/package.json +17 -10
- package/src/index.ts +514 -131
package/build/index.d.ts
CHANGED
|
@@ -5,10 +5,6 @@ export interface Callback<R = void> {
|
|
|
5
5
|
export interface Listener<T, R = unknown> {
|
|
6
6
|
(event: T): MaybePromise<R | void>;
|
|
7
7
|
}
|
|
8
|
-
export interface Result<T, E> {
|
|
9
|
-
ok: boolean;
|
|
10
|
-
result: T | E;
|
|
11
|
-
}
|
|
12
8
|
export interface FilterFunction<T> {
|
|
13
9
|
(event: T): MaybePromise<boolean>;
|
|
14
10
|
}
|
|
@@ -22,7 +18,27 @@ export interface Mapper<T, R> {
|
|
|
22
18
|
export interface Reducer<T, R> {
|
|
23
19
|
(result: R, event: T): MaybePromise<R>;
|
|
24
20
|
}
|
|
25
|
-
export
|
|
21
|
+
export interface Expander<T, R> {
|
|
22
|
+
(event: T): MaybePromise<R>;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Removes a listener from the provided array of listeners. It searches for the listener and removes all instances of it from the array.
|
|
26
|
+
* This method ensures that the listener is fully unregistered, preventing any residual calls to a potentially deprecated handler.
|
|
27
|
+
*
|
|
28
|
+
* @param {Listener<T, R>[]} listeners - The array of listeners from which to remove the listener.
|
|
29
|
+
* @param {Listener<T, R>} listener - The listener function to remove from the list of listeners.
|
|
30
|
+
* @returns {boolean} - Returns `true` if the listener was found and removed, `false` otherwise.
|
|
31
|
+
*
|
|
32
|
+
* @template T - The type of the event that listeners are associated with.
|
|
33
|
+
* @template R - The type of the return value that listeners are expected to return.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* // Assuming an array of listeners for click events
|
|
37
|
+
* const listeners = [onClickHandler1, onClickHandler2];
|
|
38
|
+
* const wasRemoved = removeListener(listeners, onClickHandler1);
|
|
39
|
+
* console.log(wasRemoved); // Output: true
|
|
40
|
+
*/
|
|
41
|
+
export declare const removeListener: <T, R>(listeners: Listener<T, R>[], listener: Listener<T, R>) => boolean;
|
|
26
42
|
/**
|
|
27
43
|
* An abstract class that extends the built-in Function class. It allows instances of the class
|
|
28
44
|
* to be called as functions. When an instance of FunctionExt is called as a function, it will
|
|
@@ -32,30 +48,60 @@ export type Listeners<T, R> = Listener<T, R>[];
|
|
|
32
48
|
export declare abstract class FunctionExt extends Function {
|
|
33
49
|
constructor(func: Function);
|
|
34
50
|
}
|
|
35
|
-
export interface
|
|
51
|
+
export interface Unsubscribe {
|
|
36
52
|
(): MaybePromise<void>;
|
|
37
53
|
}
|
|
38
54
|
/**
|
|
39
55
|
* @internal
|
|
40
56
|
*/
|
|
41
|
-
export declare class
|
|
57
|
+
export declare class Unsubscribe extends FunctionExt {
|
|
42
58
|
constructor(callback: Callback);
|
|
43
|
-
pre(callback: Callback):
|
|
44
|
-
post(callback: Callback):
|
|
45
|
-
countdown(count: number):
|
|
59
|
+
pre(callback: Callback): Unsubscribe;
|
|
60
|
+
post(callback: Callback): Unsubscribe;
|
|
61
|
+
countdown(count: number): Unsubscribe;
|
|
46
62
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
63
|
+
export interface Event<T = any, R = any> {
|
|
64
|
+
(event: T): Promise<(void | Awaited<R>)[]>;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Represents a pair of events handling both successful outcomes and errors.
|
|
68
|
+
* This interface is used to manage asynchronous operations where events can either
|
|
69
|
+
* result in a successful output or an error.
|
|
70
|
+
*
|
|
71
|
+
* The `value` event is triggered when the operation succeeds and emits a result.
|
|
72
|
+
* The `error` event is triggered when the operation encounters an error, allowing
|
|
73
|
+
* error handling mechanisms to process or log the error accordingly.
|
|
74
|
+
*
|
|
75
|
+
* @template T The type of data emitted by the successful outcome of the event.
|
|
76
|
+
* @template R The type of data (if any) emitted by the error event.
|
|
77
|
+
* @template E The type of error information emitted by the error event, usually an Error object or string.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* // Assume an asynchronous function that fetches user data
|
|
81
|
+
* function fetchUserData(): ResultEvents<UserData, Error> {
|
|
82
|
+
* const success = new Event<UserData>();
|
|
83
|
+
* const failure = new Event<Error>();
|
|
84
|
+
*
|
|
85
|
+
* fetch('/api/user')
|
|
86
|
+
* .then(response => response.json())
|
|
87
|
+
* .then(data => success(data))
|
|
88
|
+
* .catch(error => failure(error));
|
|
89
|
+
*
|
|
90
|
+
* return { value: success, error: failure };
|
|
91
|
+
* }
|
|
92
|
+
*
|
|
93
|
+
* const userDataEvent = fetchUserData();
|
|
94
|
+
* userDataEvent.value.on(data => console.log('User data received:', data));
|
|
95
|
+
* userDataEvent.error.on(error => console.error('An error occurred:', error));
|
|
96
|
+
*/
|
|
97
|
+
export interface ResultEvents<T, R, E = unknown> {
|
|
98
|
+
value: Event<T, R>;
|
|
99
|
+
error: Event<E, void>;
|
|
100
|
+
}
|
|
101
|
+
export interface Queue<T> {
|
|
102
|
+
pop(): MaybePromise<T | undefined>;
|
|
103
|
+
stop(): MaybePromise<void>;
|
|
50
104
|
}
|
|
51
|
-
export type EventParameters<T> = T extends Event<infer P, unknown> ? P : never;
|
|
52
|
-
export type EventResult<T> = T extends Event<unknown, infer R> ? R : never;
|
|
53
|
-
export type AllEventsParameters<T extends Event<unknown, unknown>[]> = {
|
|
54
|
-
[K in keyof T]: EventParameters<T[K]>;
|
|
55
|
-
}[number];
|
|
56
|
-
export type AllEventsResults<T extends Event<unknown, unknown>[]> = {
|
|
57
|
-
[K in keyof T]: EventResult<T[K]>;
|
|
58
|
-
}[number];
|
|
59
105
|
/**
|
|
60
106
|
* A class representing an anonymous event that can be listened to or triggered.
|
|
61
107
|
*
|
|
@@ -67,6 +113,11 @@ export declare class Event<T, R> extends FunctionExt {
|
|
|
67
113
|
* The array of listeners for the event.
|
|
68
114
|
*/
|
|
69
115
|
private listeners;
|
|
116
|
+
/**
|
|
117
|
+
* The array of listeners for the event.
|
|
118
|
+
*/
|
|
119
|
+
private addSpies;
|
|
120
|
+
private removeSpies;
|
|
70
121
|
/**
|
|
71
122
|
* A function that disposes of the event and its listeners.
|
|
72
123
|
*/
|
|
@@ -76,7 +127,7 @@ export declare class Event<T, R> extends FunctionExt {
|
|
|
76
127
|
* @example
|
|
77
128
|
* // Create a click event.
|
|
78
129
|
* const clickEvent = new Event<[x: number, y: number], void>();
|
|
79
|
-
* clickEvent.on((x, y) => console.log(`Clicked at ${x}, ${y}`));
|
|
130
|
+
* clickEvent.on(([x, y]) => console.log(`Clicked at ${x}, ${y}`));
|
|
80
131
|
*
|
|
81
132
|
* @param dispose - A function to call on the event disposal.
|
|
82
133
|
*/
|
|
@@ -86,145 +137,352 @@ export declare class Event<T, R> extends FunctionExt {
|
|
|
86
137
|
*/
|
|
87
138
|
get size(): number;
|
|
88
139
|
/**
|
|
89
|
-
* Checks if a listener is not registered for the event.
|
|
90
|
-
*
|
|
91
|
-
* @
|
|
140
|
+
* Checks if a specific listener is not registered for the event.
|
|
141
|
+
* This method is typically used to verify whether an event listener has not been added to prevent duplicate registrations.
|
|
142
|
+
* @param listener - The listener function to check against the registered listeners.
|
|
143
|
+
* @returns `true` if the listener is not already registered; otherwise, `false`.
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* // Check if a listener is not already added
|
|
147
|
+
* if (event.lacks(myListener)) {
|
|
148
|
+
* event.on(myListener);
|
|
149
|
+
* }
|
|
150
|
+
*
|
|
92
151
|
*/
|
|
93
152
|
lacks(listener: Listener<T, R>): boolean;
|
|
94
153
|
/**
|
|
95
|
-
* Checks if a listener is registered for the event.
|
|
96
|
-
*
|
|
97
|
-
*
|
|
154
|
+
* Checks if a specific listener is registered for the event.
|
|
155
|
+
* This method is used to confirm the presence of a listener in the event's registration list.
|
|
156
|
+
*
|
|
157
|
+
* @param listener - The listener function to verify.
|
|
158
|
+
* @returns `true` if the listener is currently registered; otherwise, `false`.
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* // Verify if a listener is registered
|
|
162
|
+
* if (event.has(myListener)) {
|
|
163
|
+
* console.log('Listener is already registered');
|
|
164
|
+
* }
|
|
98
165
|
*/
|
|
99
166
|
has(listener: Listener<T, R>): boolean;
|
|
100
167
|
/**
|
|
101
|
-
* Removes a listener from the event.
|
|
168
|
+
* Removes a listener from the event's registration list.
|
|
169
|
+
* This method is used when the listener is no longer needed, helping to prevent memory leaks and unnecessary executions.
|
|
170
|
+
*
|
|
102
171
|
* @param listener - The listener to remove.
|
|
172
|
+
* @returns The event instance, allowing for method chaining.
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* // Remove a listener
|
|
176
|
+
* event.off(myListener);
|
|
103
177
|
*/
|
|
104
|
-
off(listener: Listener<T, R>):
|
|
178
|
+
off(listener: Listener<T, R>): this;
|
|
105
179
|
/**
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
180
|
+
* Registers a listener that gets triggered whenever the event is emitted.
|
|
181
|
+
* This is the primary method for adding event handlers that will react to the event being triggered.
|
|
182
|
+
*
|
|
183
|
+
* @param listener - The function to call when the event occurs.
|
|
184
|
+
* @returns An object that can be used to unsubscribe the listener, ensuring easy cleanup.
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* // Add a listener to an event
|
|
188
|
+
* const unsubscribe = event.on((data) => {
|
|
189
|
+
* console.log('Event data:', data);
|
|
190
|
+
* });
|
|
109
191
|
*/
|
|
110
|
-
on(listener: Listener<T, R>):
|
|
192
|
+
on(listener: Listener<T, R>): Unsubscribe;
|
|
111
193
|
/**
|
|
112
|
-
* Adds a listener
|
|
113
|
-
*
|
|
114
|
-
*
|
|
194
|
+
* Adds a listener that will be called only once the next time the event is emitted.
|
|
195
|
+
* This method is useful for one-time notifications or single-trigger scenarios.
|
|
196
|
+
*
|
|
197
|
+
* @param listener - The listener to trigger once.
|
|
198
|
+
* @returns An object that can be used to remove the listener if the event has not yet occurred.
|
|
199
|
+
* @example
|
|
200
|
+
* // Register a one-time listener
|
|
201
|
+
* const onceUnsubscribe = event.once((data) => {
|
|
202
|
+
* console.log('Received data once:', data);
|
|
203
|
+
* });
|
|
115
204
|
*/
|
|
116
|
-
once(listener: Listener<T, R>):
|
|
205
|
+
once(listener: Listener<T, R>): Unsubscribe;
|
|
117
206
|
/**
|
|
118
|
-
* Returns a Promise that resolves with the first
|
|
119
|
-
*
|
|
207
|
+
* Returns a Promise that resolves with the first event argument emitted.
|
|
208
|
+
* This method is useful for scenarios where you need to wait for the first occurrence
|
|
209
|
+
* of an event and then perform actions based on the event data.
|
|
210
|
+
*
|
|
211
|
+
* @returns {Promise<T>} A Promise that resolves with the first event argument emitted.
|
|
212
|
+
* @example
|
|
213
|
+
* const clickEvent = new Event<[number, number]>();
|
|
214
|
+
* clickEvent.onceAsync().then(([x, y]) => {
|
|
215
|
+
* console.log(`First click at (${x}, ${y})`);
|
|
216
|
+
* });
|
|
120
217
|
*/
|
|
121
218
|
onceAsync(): Promise<T>;
|
|
122
219
|
/**
|
|
123
|
-
* Removes all listeners from the event.
|
|
220
|
+
* Removes all listeners from the event, effectively resetting it. This is useful when you need to
|
|
221
|
+
* cleanly dispose of all event handlers to prevent memory leaks or unwanted triggerings after certain conditions.
|
|
222
|
+
*
|
|
223
|
+
* @returns {this} The instance of the event, allowing for method chaining.
|
|
224
|
+
* @example
|
|
225
|
+
* const myEvent = new Event();
|
|
226
|
+
* myEvent.on(data => console.log(data));
|
|
227
|
+
* myEvent.clear(); // Clears all listeners
|
|
124
228
|
*/
|
|
125
|
-
clear():
|
|
229
|
+
clear(): this;
|
|
126
230
|
/**
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
* const spacePressEvent = keyboardEvent.filter((key) => key === 'Space');
|
|
231
|
+
* Filters events, creating a new event that only triggers when the provided filter function returns `true`.
|
|
232
|
+
* This method can be used to selectively process events that meet certain criteria.
|
|
130
233
|
*
|
|
131
|
-
* @param filter The filter function to apply to
|
|
132
|
-
* @returns A new event that only triggers
|
|
234
|
+
* @param {Filter<T, P>} filter The filter function or predicate to apply to each event.
|
|
235
|
+
* @returns {Event<P, R>} A new event that only triggers for filtered events.
|
|
236
|
+
* @example
|
|
237
|
+
* const keyPressedEvent = new Event<string>();
|
|
238
|
+
* const enterPressedEvent = keyPressedEvent.filter(key => key === 'Enter');
|
|
239
|
+
* enterPressedEvent.on(() => console.log('Enter key was pressed.'));
|
|
133
240
|
*/
|
|
134
241
|
filter<P extends T>(predicate: Predicate<T, P>): Event<P, R>;
|
|
135
242
|
filter<P extends T>(filter: FilterFunction<T>): Event<P, R>;
|
|
136
243
|
/**
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
* const escPressEvent = keyboardEvent.first((key) => key === 'Esc');
|
|
140
|
-
* await escPressEvent.toPromise();
|
|
244
|
+
* Creates a new event that will only be triggered once when the provided filter function returns `true`.
|
|
245
|
+
* This method is useful for handling one-time conditions in a stream of events.
|
|
141
246
|
*
|
|
142
|
-
* @param filter - The filter function.
|
|
143
|
-
* @returns A new event that will
|
|
247
|
+
* @param {Filter<T, P>} filter - The filter function or predicate.
|
|
248
|
+
* @returns {Event<P, R>} A new event that will be triggered only once when the filter condition is met.
|
|
249
|
+
* @example
|
|
250
|
+
* const sizeChangeEvent = new Event<number>();
|
|
251
|
+
* const sizeReachedEvent = sizeChangeEvent.first(size => size > 1024);
|
|
252
|
+
* sizeReachedEvent.on(() => console.log('Size threshold exceeded.'));
|
|
144
253
|
*/
|
|
145
254
|
first<P extends T>(predicate: Predicate<T, P>): Event<P, R>;
|
|
146
255
|
first<P extends T>(filter: FilterFunction<T>): Event<P, R>;
|
|
147
256
|
/**
|
|
148
|
-
* Returns a
|
|
149
|
-
*
|
|
150
|
-
* const escPressEvent = await keyboardEvent.firstAsync((key) => key === 'Esc');
|
|
257
|
+
* Returns a Promise that resolves once an event occurs that meets the filter criteria.
|
|
258
|
+
* This method is particularly useful for handling asynchronous flows where you need to wait for a specific condition.
|
|
151
259
|
*
|
|
152
|
-
* @param filter - The filter function.
|
|
153
|
-
* @returns A
|
|
260
|
+
* @param {Filter<T, P>} filter - The filter function or predicate.
|
|
261
|
+
* @returns {Promise<P>} A Promise that resolves once the filter condition is met.
|
|
262
|
+
* @example
|
|
263
|
+
* const mouseEvent = new Event<{x: number, y: number}>();
|
|
264
|
+
* const clickAtPosition = mouseEvent.firstAsync(pos => pos.x > 200 && pos.y > 200);
|
|
265
|
+
* clickAtPosition.then(pos => console.log(`Clicked at (${pos.x}, ${pos.y})`));
|
|
154
266
|
*/
|
|
155
267
|
firstAsync<P extends T>(predicate: Predicate<T, P>): Promise<P>;
|
|
156
268
|
firstAsync<P extends T>(filter: FilterFunction<T>): Promise<P>;
|
|
157
269
|
/**
|
|
158
|
-
*
|
|
270
|
+
* Transforms the data emitted by this event using a mapping function. Each emitted event is processed by the `mapper`
|
|
271
|
+
* function, which returns a new value that is then emitted by the returned `Event` instance. This is useful for data transformation
|
|
272
|
+
* or adapting the event's data structure.
|
|
273
|
+
*
|
|
274
|
+
* @template M The type of data that the mapper function will produce.
|
|
275
|
+
* @template MR The type of data emitted by the mapped event, usually related to or the same as `M`.
|
|
276
|
+
* @param {Mapper<T, M>} mapper A function that takes the original event data and returns the transformed data.
|
|
277
|
+
* @returns {Event<M, MR>} A new `Event` instance that emits the mapped values.
|
|
278
|
+
*
|
|
159
279
|
* @example
|
|
160
|
-
*
|
|
280
|
+
* // Assuming an event that emits numbers, create a new event that emits their squares.
|
|
281
|
+
* const numberEvent = new Event<number>();
|
|
282
|
+
* const squaredEvent = numberEvent.map(num => num * num);
|
|
283
|
+
* squaredEvent.on(squared => console.log('Squared number:', squared));
|
|
284
|
+
* await numberEvent(5); // Logs: "Squared number: 25"
|
|
161
285
|
*
|
|
162
286
|
* @param mapper A function that maps the values of this event to a new value.
|
|
163
287
|
* @returns A new event that emits the mapped values.
|
|
164
288
|
*/
|
|
165
289
|
map<M, MR = R>(mapper: Mapper<T, M>): Event<M, MR>;
|
|
166
290
|
/**
|
|
167
|
-
*
|
|
291
|
+
* Accumulates the values emitted by this event using a reducer function, starting from an initial value. The reducer
|
|
292
|
+
* function takes the accumulated value and the latest emitted event data, then returns a new accumulated value. This
|
|
293
|
+
* new value is then emitted by the returned `Event` instance. This is particularly useful for accumulating state over time.
|
|
294
|
+
*
|
|
168
295
|
* @example
|
|
169
296
|
* const sumEvent = numberEvent.reduce((a, b) => a + b, 0);
|
|
170
297
|
* sumEvent.on((sum) => console.log(sum)); // 1, 3, 6
|
|
171
|
-
* sumEvent(1);
|
|
172
|
-
* sumEvent(2);
|
|
173
|
-
* sumEvent(3);
|
|
174
|
-
*
|
|
175
|
-
* @
|
|
176
|
-
* @
|
|
177
|
-
* @param {Reducer<T, A>} reducer
|
|
178
|
-
* @param {A} init The initial value of the
|
|
179
|
-
* @returns {Event<
|
|
298
|
+
* await sumEvent(1);
|
|
299
|
+
* await sumEvent(2);
|
|
300
|
+
* await sumEvent(3);
|
|
301
|
+
*
|
|
302
|
+
* @template A The type of the accumulator value.
|
|
303
|
+
* @template AR The type of data emitted by the reduced event, usually the same as `A`.
|
|
304
|
+
* @param {Reducer<T, A>} reducer A function that takes the current accumulated value and the new event data, returning the new accumulated value.
|
|
305
|
+
* @param {A} init The initial value of the accumulator.
|
|
306
|
+
* @returns {Event<A, AR>} A new `Event` instance that emits the reduced value.
|
|
307
|
+
*
|
|
180
308
|
*/
|
|
181
309
|
reduce<A, AR = R>(reducer: Reducer<T, A>, init: A): Event<A, AR>;
|
|
182
310
|
/**
|
|
183
|
-
*
|
|
184
|
-
*
|
|
311
|
+
* Transforms each event's data into multiple events using an expander function. The expander function takes
|
|
312
|
+
* the original event's data and returns an array of new data elements, each of which will be emitted individually
|
|
313
|
+
* by the returned `Event` instance. This method is useful for scenarios where an event's data can naturally
|
|
314
|
+
* be expanded into multiple, separate pieces of data which should each trigger further processing independently.
|
|
315
|
+
*
|
|
316
|
+
* @template ET - The type of data elements in the array returned by the expander function.
|
|
317
|
+
* @template ER - The type of responses emitted by the expanded event, usually related to or the same as `ET`.
|
|
318
|
+
* @param {Expander<T, ET[]>} expander - A function that takes the original event data and returns an array of new data elements.
|
|
319
|
+
* @returns {Event<ET, ER>} - A new `Event` instance that emits each value from the array returned by the expander function.
|
|
320
|
+
*
|
|
321
|
+
* @example
|
|
322
|
+
* // Assuming an event that emits a sentence, create a new event that emits each word from the sentence separately.
|
|
323
|
+
* const sentenceEvent = new Event<string>();
|
|
324
|
+
* const wordEvent = sentenceEvent.expand(sentence => sentence.split(' '));
|
|
325
|
+
* wordEvent.on(word => console.log('Word:', word));
|
|
326
|
+
* await sentenceEvent('Hello world'); // Logs: "Word: Hello", "Word: world"
|
|
327
|
+
*/
|
|
328
|
+
expand<ET, ER>(expander: Expander<T, ET[]>): Event<ET, ER>;
|
|
329
|
+
/**
|
|
330
|
+
* Creates a new event that emits values based on a conductor event. The orchestrated event will emit the last value
|
|
331
|
+
* captured from the original event each time the conductor event is triggered. This method is useful for synchronizing
|
|
332
|
+
* events, where the emission of one event controls the timing of another.
|
|
333
|
+
*
|
|
334
|
+
* @example
|
|
335
|
+
* const rightClickPositionEvent = mouseMoveEvent.orchestrate(mouseRightClickEvent);
|
|
336
|
+
*
|
|
337
|
+
* @example
|
|
338
|
+
* // An event that emits whenever a "tick" event occurs.
|
|
339
|
+
* const tickEvent = new Event<void>();
|
|
340
|
+
* const dataEvent = new Event<string>();
|
|
341
|
+
* const synchronizedEvent = dataEvent.orchestrate(tickEvent);
|
|
342
|
+
* synchronizedEvent.on(data => console.log('Data on tick:', data));
|
|
343
|
+
* await dataEvent('Hello');
|
|
344
|
+
* await dataEvent('World!');
|
|
345
|
+
* await tickEvent(); // Logs: "Data on tick: World!"
|
|
346
|
+
*
|
|
347
|
+
* @template T The type of data emitted by the original event.
|
|
348
|
+
* @template R The type of data emitted by the orchestrated event, usually the same as `T`.
|
|
349
|
+
* @param {Event<unknown, unknown>} conductor An event that signals when the orchestrated event should emit.
|
|
350
|
+
* @returns {Event<T, R>} An orchestrated event that emits values based on the conductor's trigger.
|
|
351
|
+
*
|
|
352
|
+
*/
|
|
353
|
+
orchestrate(conductor: Event<any, any>): Event<T, R>;
|
|
354
|
+
/**
|
|
355
|
+
* Creates a debounced event that delays triggering until after a specified interval has elapsed
|
|
356
|
+
* following the last time it was invoked. This method is particularly useful for limiting the rate
|
|
357
|
+
* at which a function is executed. Common use cases include handling rapid user inputs, window resizing,
|
|
358
|
+
* or scroll events.
|
|
359
|
+
*
|
|
185
360
|
* @example
|
|
186
361
|
* const debouncedEvent = textInputEvent.debounce(100);
|
|
187
|
-
* debouncedEvent.on((str) => console.log(str)); // '
|
|
188
|
-
* event('t');
|
|
189
|
-
* event('te');
|
|
190
|
-
* event('
|
|
191
|
-
* event('
|
|
192
|
-
*
|
|
193
|
-
* @param interval - The amount of time to wait before firing the debounced event
|
|
194
|
-
* @returns
|
|
362
|
+
* debouncedEvent.on((str) => console.log(str)); // only 'text' is emitted
|
|
363
|
+
* await event('t');
|
|
364
|
+
* await event('te');
|
|
365
|
+
* await event('tex');
|
|
366
|
+
* await event('text');
|
|
367
|
+
*
|
|
368
|
+
* @param {number} interval - The amount of time to wait (in milliseconds) before firing the debounced event.
|
|
369
|
+
* @returns {ResultEvents<T, R, unknown>} An object containing two events: `value` for the debounced successful
|
|
370
|
+
* outputs and `error` for catching errors during the debounce handling.
|
|
195
371
|
*/
|
|
196
|
-
debounce(interval: number):
|
|
372
|
+
debounce(interval: number): ResultEvents<T, R, unknown>;
|
|
373
|
+
/**
|
|
374
|
+
* Aggregates multiple event emissions into batches and emits the batched events either at specified
|
|
375
|
+
* time intervals or when the batch reaches a predefined size. This method is useful for grouping
|
|
376
|
+
* a high volume of events into manageable chunks, such as logging or processing data in bulk.
|
|
377
|
+
*
|
|
378
|
+
* @example
|
|
379
|
+
* // Batch messages for bulk processing every 1 second or when 10 messages are collected
|
|
380
|
+
* const messageEvent = createEvent<string, void>();
|
|
381
|
+
* const batchedMessageEvent = messageEvent.batch(1000, 10);
|
|
382
|
+
* batchedMessageEvent.value.on((messages) => console.log('Batched Messages:', messages));
|
|
383
|
+
*
|
|
384
|
+
* @param {number} interval - The time in milliseconds between batch emissions.
|
|
385
|
+
* @param {number} [size] - Optional. The maximum size of each batch. If specified, triggers a batch emission
|
|
386
|
+
* once the batch reaches this size, regardless of the interval.
|
|
387
|
+
* @returns {ResultEvents<T[], R, unknown>} An object containing two events: `value` for the batched results
|
|
388
|
+
* and `error` for errors that may occur during batching.
|
|
389
|
+
*/
|
|
390
|
+
batch(interval: number, size?: number): ResultEvents<T[], R, unknown>;
|
|
391
|
+
/**
|
|
392
|
+
* Transforms an event into an AsyncIterable that yields values as they are emitted by the event. This allows for the consumption
|
|
393
|
+
* of event data using async iteration mechanisms. The iterator generated will yield all emitted values until the event
|
|
394
|
+
* signals it should no longer be active.
|
|
395
|
+
*
|
|
396
|
+
* @returns {AsyncIterable<T>} An async iterable that yields values emitted by the event.
|
|
397
|
+
* @example
|
|
398
|
+
* // Assuming an event that emits numbers
|
|
399
|
+
* const numberEvent = new Event<number>();
|
|
400
|
+
* const numberIterable = numberEvent.generator();
|
|
401
|
+
* (async () => {
|
|
402
|
+
* for await (const num of numberIterable) {
|
|
403
|
+
* console.log('Number:', num);
|
|
404
|
+
* }
|
|
405
|
+
* })();
|
|
406
|
+
* await numberEvent(1);
|
|
407
|
+
* await numberEvent(2);
|
|
408
|
+
* await numberEvent(3);
|
|
409
|
+
*/
|
|
410
|
+
generator(): AsyncIterable<T>;
|
|
411
|
+
/**
|
|
412
|
+
* Creates a queue from the event, where each emitted value is sequentially processed. The returned object allows popping elements
|
|
413
|
+
* from the queue, ensuring that elements are handled one at a time. This method is ideal for scenarios where order and sequential processing are critical.
|
|
414
|
+
*
|
|
415
|
+
* @returns {Queue<T>} An object representing the queue. The 'pop' method retrieves the next element from the queue, while 'stop' halts further processing.
|
|
416
|
+
* @example
|
|
417
|
+
* // Queueing tasks for sequential execution
|
|
418
|
+
* const taskEvent = new Event<string>();
|
|
419
|
+
* const taskQueue = taskEvent.queue();
|
|
420
|
+
* await taskEvent('Task 1');
|
|
421
|
+
* await taskEvent('Task 2');
|
|
422
|
+
* (async () => {
|
|
423
|
+
* console.log('Processing:', await taskQueue.pop()); // Processing: Task 1
|
|
424
|
+
* console.log('Processing:', await taskQueue.pop()); // Processing: Task 2
|
|
425
|
+
* })();
|
|
426
|
+
*/
|
|
427
|
+
queue(): Queue<T>;
|
|
197
428
|
}
|
|
429
|
+
export type EventParameters<T> = T extends Event<infer P, any> ? P : never;
|
|
430
|
+
export type EventResult<T> = T extends Event<any, infer R> ? R : never;
|
|
431
|
+
export type AllEventsParameters<T extends Event<any, any>[]> = {
|
|
432
|
+
[K in keyof T]: EventParameters<T[K]>;
|
|
433
|
+
}[number];
|
|
434
|
+
export type AllEventsResults<T extends Event<any, any>[]> = {
|
|
435
|
+
[K in keyof T]: EventResult<T[K]>;
|
|
436
|
+
}[number];
|
|
198
437
|
/**
|
|
199
|
-
* Merges multiple events into a single event.
|
|
200
|
-
*
|
|
201
|
-
*
|
|
438
|
+
* Merges multiple events into a single event. This function takes any number of `Event` instances
|
|
439
|
+
* and returns a new `Event` that triggers whenever any of the input events trigger. The parameters
|
|
440
|
+
* and results of the merged event are derived from the input events, providing a flexible way to
|
|
441
|
+
* handle multiple sources of events in a unified manner.
|
|
202
442
|
*
|
|
203
|
-
* @
|
|
204
|
-
* @
|
|
443
|
+
* @template Events - An array of `Event` instances.
|
|
444
|
+
* @param {...Events} events - A rest parameter that takes multiple events to be merged.
|
|
445
|
+
* @returns {Event<AllEventsParameters<Events>, AllEventsResults<Events>>} - Returns a new `Event` instance
|
|
446
|
+
* that triggers with the parameters and results of any of the merged input events.
|
|
447
|
+
*
|
|
448
|
+
* @example
|
|
449
|
+
* // Merging mouse and keyboard events into a single event
|
|
450
|
+
* const mouseEvent = createEvent<MouseEvent>();
|
|
451
|
+
* const keyboardEvent = createEvent<KeyboardEvent>();
|
|
452
|
+
* const inputEvent = merge(mouseEvent, keyboardEvent);
|
|
453
|
+
* inputEvent.on(event => console.log('Input event:', event));
|
|
205
454
|
*/
|
|
206
455
|
export declare const merge: <Events extends Event<any, any>[]>(...events: Events) => Event<AllEventsParameters<Events>, AllEventsResults<Events>>;
|
|
207
456
|
/**
|
|
208
|
-
* Creates
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
*
|
|
457
|
+
* Creates a periodic event that triggers at a specified interval. The event will automatically emit
|
|
458
|
+
* an incrementing counter value each time it triggers, starting from zero. This function is useful
|
|
459
|
+
* for creating time-based triggers within an application, such as updating UI elements, polling,
|
|
460
|
+
* or any other timed operation.
|
|
212
461
|
*
|
|
213
|
-
* @
|
|
214
|
-
* @
|
|
462
|
+
* @template R - The return type of the event handler function, defaulting to `void`.
|
|
463
|
+
* @param {number} interval - The interval in milliseconds at which the event should trigger.
|
|
464
|
+
* @returns {Event<number, R>} - An `Event` instance that triggers at the specified interval,
|
|
465
|
+
* emitting an incrementing counter value.
|
|
466
|
+
*
|
|
467
|
+
* @example
|
|
468
|
+
* // Creating an interval event that logs a message every second
|
|
469
|
+
* const tickEvent = createInterval(1000);
|
|
470
|
+
* tickEvent.on(tickNumber => console.log('Tick:', tickNumber));
|
|
215
471
|
*/
|
|
216
472
|
export declare const createInterval: <R = void>(interval: number) => Event<number, R>;
|
|
217
473
|
/**
|
|
218
|
-
* Creates a new event
|
|
474
|
+
* Creates a new instance of the `Event` class, which allows for the registration of event handlers that get called when the event is emitted.
|
|
475
|
+
* This factory function simplifies the creation of events by encapsulating the instantiation logic, providing a clean and simple API for event creation.
|
|
219
476
|
*
|
|
220
|
-
* @typeParam T -
|
|
221
|
-
* @typeParam R - The return type of the event handler function.
|
|
222
|
-
* @returns A new instance of the `Event` class.
|
|
477
|
+
* @typeParam T - The tuple of argument types that the event will accept.
|
|
478
|
+
* @typeParam R - The return type of the event handler function, which is emitted after processing the event data.
|
|
479
|
+
* @returns {Event<T, R>} - A new instance of the `Event` class, ready to have listeners added to it.
|
|
223
480
|
*
|
|
224
481
|
* @example
|
|
225
|
-
*
|
|
482
|
+
* // Create a new event that accepts a string and returns the string length
|
|
483
|
+
* const myEvent = createEvent<string, number>();
|
|
226
484
|
* myEvent.on((str: string) => str.length);
|
|
227
|
-
*
|
|
485
|
+
* myEvent('hello').then(results => console.log(results)); // Logs: [5]
|
|
228
486
|
*/
|
|
229
487
|
export declare const createEvent: <T, R = void>() => Event<T, R>;
|
|
230
488
|
export default createEvent;
|