@soffinal/stream 0.1.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/LICENSE +21 -0
- package/README.md +776 -0
- package/dist/benchmark.d.ts +16 -0
- package/dist/benchmark.d.ts.map +1 -0
- package/dist/benchmark.js +151 -0
- package/dist/benchmark.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/list.d.ts +153 -0
- package/dist/list.d.ts.map +1 -0
- package/dist/list.js +200 -0
- package/dist/list.js.map +1 -0
- package/dist/map.d.ts +97 -0
- package/dist/map.d.ts.map +1 -0
- package/dist/map.js +99 -0
- package/dist/map.js.map +1 -0
- package/dist/set.d.ts +92 -0
- package/dist/set.d.ts.map +1 -0
- package/dist/set.js +94 -0
- package/dist/set.js.map +1 -0
- package/dist/state.d.ts +79 -0
- package/dist/state.d.ts.map +1 -0
- package/dist/state.js +90 -0
- package/dist/state.js.map +1 -0
- package/dist/stream.d.ts +480 -0
- package/dist/stream.d.ts.map +1 -0
- package/dist/stream.js +558 -0
- package/dist/stream.js.map +1 -0
- package/package.json +47 -0
package/dist/stream.d.ts
ADDED
|
@@ -0,0 +1,480 @@
|
|
|
1
|
+
export type ValueOf<STREAM> = STREAM extends Stream<infer VALUE> ? VALUE : never;
|
|
2
|
+
/**
|
|
3
|
+
* A reactive streaming library that provides async-first data structures with built-in event streams.
|
|
4
|
+
*
|
|
5
|
+
* @template VALUE - The type of values that flow through the stream
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* // Basic usage
|
|
10
|
+
* const stream = new Stream<number>();
|
|
11
|
+
* stream.listen(value => console.log(value));
|
|
12
|
+
* stream.push(1, 2, 3);
|
|
13
|
+
*
|
|
14
|
+
* // With async generator
|
|
15
|
+
* const timerStream = new Stream(async function* () {
|
|
16
|
+
* let count = 0;
|
|
17
|
+
* while (count < 5) {
|
|
18
|
+
* yield count++;
|
|
19
|
+
* await new Promise(resolve => setTimeout(resolve, 1000));
|
|
20
|
+
* }
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* // Async iteration
|
|
24
|
+
* for await (const value of stream) {
|
|
25
|
+
* console.log(value);
|
|
26
|
+
* if (value === 10) break;
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare class Stream<VALUE = unknown> implements AsyncIterable<VALUE> {
|
|
31
|
+
protected _listeners: Set<(value: VALUE) => void>;
|
|
32
|
+
protected _generatorFn: (() => AsyncGenerator<VALUE, void>) | undefined;
|
|
33
|
+
protected _generator: AsyncGenerator<VALUE, void> | undefined;
|
|
34
|
+
protected _listenerAdded: Stream<void> | undefined;
|
|
35
|
+
protected _listenerRemoved: Stream<void> | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Creates a new Stream instance.
|
|
38
|
+
*
|
|
39
|
+
* @param generatorFn - Optional async generator function to produce values you can use it for creating stream with custom transformation
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* // Empty stream
|
|
44
|
+
* const stream = new Stream<string>();
|
|
45
|
+
*
|
|
46
|
+
* // Stream with generator
|
|
47
|
+
* const countdown = new Stream(async function* () {
|
|
48
|
+
* for (let i = 5; i > 0; i--) {
|
|
49
|
+
* yield i;
|
|
50
|
+
* await new Promise(resolve => setTimeout(resolve, 1000));
|
|
51
|
+
* }
|
|
52
|
+
* });
|
|
53
|
+
*
|
|
54
|
+
* // Stream with custom transformer
|
|
55
|
+
* function filter<VALUE>(source:Stream<VALUE>,predicate:(value:VALUE) => boolean):Stream<VALUE>{
|
|
56
|
+
* return new Stream<VALUE>(async function* () {
|
|
57
|
+
* for await (const value of source) {
|
|
58
|
+
* if (predicate(value)) yield value ;
|
|
59
|
+
* }
|
|
60
|
+
* });
|
|
61
|
+
* }
|
|
62
|
+
* const source = new Stream<number>();
|
|
63
|
+
* const even = filter(source,v=> v % 2 === 0)
|
|
64
|
+
* even.listen(console.log);
|
|
65
|
+
* source.push(1, 2, 3, 4); // 2,4
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
constructor();
|
|
69
|
+
constructor(generatorFn: () => AsyncGenerator<VALUE, void>);
|
|
70
|
+
/**
|
|
71
|
+
* Returns true if the stream has active listeners.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* const stream = new Stream<number>();
|
|
76
|
+
* console.log(stream.hasListeners); // false
|
|
77
|
+
*
|
|
78
|
+
* const cleanup = stream.listen(value => console.log(value));
|
|
79
|
+
* console.log(stream.hasListeners); // true
|
|
80
|
+
*
|
|
81
|
+
* cleanup();
|
|
82
|
+
* console.log(stream.hasListeners); // false
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
get hasListeners(): boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Stream that emits when a listener is added.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* const stream = new Stream<number>();
|
|
92
|
+
* stream.listenerAdded.listen(() => console.log('Listener added'));
|
|
93
|
+
*
|
|
94
|
+
* stream.listen(value => console.log(value)); // Triggers 'Listener added'
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
get listenerAdded(): Stream<void>;
|
|
98
|
+
/**
|
|
99
|
+
* Stream that emits when a listener is removed.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```typescript
|
|
103
|
+
* const stream = new Stream<number>();
|
|
104
|
+
* stream.listenerRemoved.listen(() => console.log('Listener removed'));
|
|
105
|
+
*
|
|
106
|
+
* const cleanup = stream.listen(value => console.log(value));
|
|
107
|
+
* cleanup(); // Triggers 'Listener removed'
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
get listenerRemoved(): Stream<void>;
|
|
111
|
+
[Symbol.asyncIterator](): AsyncGenerator<VALUE, void>;
|
|
112
|
+
/**
|
|
113
|
+
* Pushes one or more values to all listeners.
|
|
114
|
+
*
|
|
115
|
+
* @param value - The first value to push
|
|
116
|
+
* @param values - Additional values to push
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```typescript
|
|
120
|
+
* const stream = new Stream<number>();
|
|
121
|
+
* stream.listen(value => console.log('Received:', value));
|
|
122
|
+
*
|
|
123
|
+
* stream.push(1); // Received: 1
|
|
124
|
+
* stream.push(2, 3, 4); // Received: 2, Received: 3, Received: 4
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
push(value: VALUE, ...values: VALUE[]): void;
|
|
128
|
+
/**
|
|
129
|
+
* Adds a listener to the stream.
|
|
130
|
+
*
|
|
131
|
+
* @param listener - Function to call when values are pushed
|
|
132
|
+
* @param signal - Optional AbortSignal for cleanup
|
|
133
|
+
* @returns Cleanup function to remove the listener
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```typescript
|
|
137
|
+
* const stream = new Stream<string>();
|
|
138
|
+
*
|
|
139
|
+
* // Basic listener
|
|
140
|
+
* const cleanup = stream.listen(value => console.log(value));
|
|
141
|
+
*
|
|
142
|
+
* // With AbortSignal
|
|
143
|
+
* const controller = new AbortController();
|
|
144
|
+
* stream.listen(value => console.log(value), controller.signal);
|
|
145
|
+
* controller.abort(); // Removes listener
|
|
146
|
+
*
|
|
147
|
+
* // Manual cleanup
|
|
148
|
+
* cleanup();
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
listen(listener: (value: VALUE) => void, signal?: AbortSignal): () => void;
|
|
152
|
+
/**
|
|
153
|
+
* Promise-like interface that resolves with the next value.
|
|
154
|
+
*
|
|
155
|
+
* @param onfulfilled - Optional transformation function
|
|
156
|
+
* @returns Promise that resolves with the first value
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```typescript
|
|
160
|
+
* const stream = new Stream<number>();
|
|
161
|
+
*
|
|
162
|
+
* setTimeout(()=>{
|
|
163
|
+
* stream.push(5);
|
|
164
|
+
* })
|
|
165
|
+
* // Wait for first value
|
|
166
|
+
* const firstValue = await stream; // Resolves promises with 5
|
|
167
|
+
*
|
|
168
|
+
*
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
then(onfulfilled?: ((value: VALUE) => VALUE | PromiseLike<VALUE>) | null): Promise<VALUE>;
|
|
172
|
+
/**
|
|
173
|
+
* Filters values based on a predicate function.
|
|
174
|
+
*
|
|
175
|
+
* @param predicate - Function to test each value
|
|
176
|
+
* @returns New stream with filtered values
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```typescript
|
|
180
|
+
* const numbers = new Stream<number>();
|
|
181
|
+
* const evens = numbers.filter(n => n % 2 === 0);
|
|
182
|
+
*
|
|
183
|
+
* evens.listen(n => console.log('Even:', n));
|
|
184
|
+
* numbers.push(1, 2, 3, 4); // Even: 2, Even: 4
|
|
185
|
+
*
|
|
186
|
+
* // With accumulator
|
|
187
|
+
* const increasing = numbers.filter(0, (prev, curr) =>
|
|
188
|
+
* [curr > prev, Math.max(prev, curr)]
|
|
189
|
+
* );
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
192
|
+
filter<FILTERED extends VALUE>(predicate: (value: VALUE) => value is FILTERED): Stream<FILTERED>;
|
|
193
|
+
filter<FILTERED extends VALUE>(predicate: (value: VALUE) => boolean | Promise<boolean>): Stream<FILTERED>;
|
|
194
|
+
filter<FILTERED extends VALUE, ACC>(initialValue: ACC, predicate: (accumulator: ACC, value: VALUE) => [boolean, ACC] | Promise<[boolean, ACC]>): Stream<FILTERED>;
|
|
195
|
+
/**
|
|
196
|
+
* Transforms values using a mapper function.
|
|
197
|
+
*
|
|
198
|
+
* @param mapper - Function to transform each value
|
|
199
|
+
* @returns New stream with transformed values
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* ```typescript
|
|
203
|
+
* const numbers = new Stream<number>();
|
|
204
|
+
* const doubled = numbers.map(n => n * 2);
|
|
205
|
+
*
|
|
206
|
+
* doubled.listen(n => console.log('Doubled:', n));
|
|
207
|
+
* numbers.push(1, 2, 3); // Doubled: 2, Doubled: 4, Doubled: 6
|
|
208
|
+
*
|
|
209
|
+
* // With accumulator (running sum)
|
|
210
|
+
* const sums = numbers.map(0, (sum, n) => [sum + n, sum + n]);
|
|
211
|
+
* ```
|
|
212
|
+
*/
|
|
213
|
+
map<MAPPED>(mapper: (value: VALUE) => MAPPED | Promise<MAPPED>): Stream<MAPPED>;
|
|
214
|
+
map<MAPPED, ACC>(initialValue: ACC, mapper: (accumulator: ACC, value: VALUE) => [MAPPED, ACC] | Promise<[MAPPED, ACC]>): Stream<MAPPED>;
|
|
215
|
+
/**
|
|
216
|
+
* Groups values into batches based on a predicate.
|
|
217
|
+
*
|
|
218
|
+
* @param predicate - Function to determine when to emit a group
|
|
219
|
+
* @returns New stream with grouped values
|
|
220
|
+
*
|
|
221
|
+
* @example
|
|
222
|
+
* ```typescript
|
|
223
|
+
* const numbers = new Stream<number>();
|
|
224
|
+
* const batches = numbers.group(batch => batch.length === 3);
|
|
225
|
+
*
|
|
226
|
+
* batches.listen(batch => console.log('Batch:', batch));
|
|
227
|
+
* numbers.push(1, 2, 3, 4, 5, 6); // Batch: [1,2,3], Batch: [4,5,6]
|
|
228
|
+
*
|
|
229
|
+
* // With accumulator (sum-based grouping)
|
|
230
|
+
* const sumGroups = numbers.group(0, (sum, n) =>
|
|
231
|
+
* sum + n >= 10 ? [true, 0] : [false, sum + n]
|
|
232
|
+
* );
|
|
233
|
+
* ```
|
|
234
|
+
*/
|
|
235
|
+
group(predicate: (accumulator: VALUE[], value: VALUE) => boolean | Promise<boolean>): Stream<VALUE[]>;
|
|
236
|
+
group<ACC>(initialValue: ACC, predicate: (accumulator: ACC, value: VALUE) => [boolean, ACC] | Promise<[boolean, ACC]>): Stream<ACC>;
|
|
237
|
+
/**
|
|
238
|
+
* Merges this stream with other streams.
|
|
239
|
+
*
|
|
240
|
+
* @param streams - Other streams to merge with
|
|
241
|
+
* @returns New stream with values from all streams
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* ```typescript
|
|
245
|
+
* const stream1 = new Stream<string>();
|
|
246
|
+
* const stream2 = new Stream<number>();
|
|
247
|
+
* const merged = stream1.merge(stream2);
|
|
248
|
+
*
|
|
249
|
+
* merged.listen(msg => console.log('Merged:', msg));
|
|
250
|
+
*
|
|
251
|
+
* stream1.push('from stream1');
|
|
252
|
+
* stream2.push(42);
|
|
253
|
+
* // Output: Merged: from stream1, Merged: 42
|
|
254
|
+
* ```
|
|
255
|
+
*/
|
|
256
|
+
merge<STREAMS extends [Stream<any>, ...Stream<any>[]]>(...streams: STREAMS): Stream<VALUE | ValueOf<STREAMS[number]>>;
|
|
257
|
+
/**
|
|
258
|
+
* Flattens array values in the stream.
|
|
259
|
+
*
|
|
260
|
+
* @param depth - Depth to flatten (default: 0)
|
|
261
|
+
* @returns New stream with flattened values
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* ```typescript
|
|
265
|
+
* const arrays = new Stream<number[]>();
|
|
266
|
+
* const flattened = arrays.flat();
|
|
267
|
+
*
|
|
268
|
+
* flattened.listen(n => console.log('Flat:', n));
|
|
269
|
+
* arrays.push([1, 2], [3, 4]); // Flat: 1, Flat: 2, Flat: 3, Flat: 4
|
|
270
|
+
*
|
|
271
|
+
* // Deep flattening
|
|
272
|
+
* const nested = new Stream<number[][][]>();
|
|
273
|
+
* const deepFlat = nested.flat(2);
|
|
274
|
+
* ```
|
|
275
|
+
*/
|
|
276
|
+
flat<DEPTH extends number = 0>(depth?: DEPTH): Stream<FlatArray<VALUE, DEPTH>>;
|
|
277
|
+
/**
|
|
278
|
+
* Applies a transformer function to this stream, enabling functional composition.
|
|
279
|
+
*
|
|
280
|
+
* @param transformer - Function that takes a stream and returns a transformed stream
|
|
281
|
+
* @returns New stream with the transformation applied
|
|
282
|
+
*
|
|
283
|
+
* @example
|
|
284
|
+
* ```typescript
|
|
285
|
+
* const numbers = new Stream<number>();
|
|
286
|
+
*
|
|
287
|
+
* // Using built-in transformers
|
|
288
|
+
* const result = numbers
|
|
289
|
+
* .pipe(filter(n => n > 0))
|
|
290
|
+
* .pipe(map(n => n * 2))
|
|
291
|
+
* .pipe(group(batch => batch.length >= 3));
|
|
292
|
+
*
|
|
293
|
+
* // Custom transformer
|
|
294
|
+
* const throttle = <T>(ms: number) => (stream: Stream<T>) =>
|
|
295
|
+
* new Stream<T>(async function* () {
|
|
296
|
+
* let lastEmit = 0;
|
|
297
|
+
* for await (const value of stream) {
|
|
298
|
+
* const now = Date.now();
|
|
299
|
+
* if (now - lastEmit >= ms) {
|
|
300
|
+
* yield value;
|
|
301
|
+
* lastEmit = now;
|
|
302
|
+
* }
|
|
303
|
+
* }
|
|
304
|
+
* });
|
|
305
|
+
*
|
|
306
|
+
* numbers.pipe(throttle(1000));
|
|
307
|
+
* ```
|
|
308
|
+
*/
|
|
309
|
+
pipe<OUTPUT>(transformer: (stream: Stream<VALUE>) => Stream<OUTPUT>): Stream<OUTPUT>;
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Transformer function interface for mapping stream values.
|
|
313
|
+
* Supports both simple mapping and stateful mapping with accumulators.
|
|
314
|
+
*/
|
|
315
|
+
export interface MapFunction {
|
|
316
|
+
<VALUE, MAPPED>(mapper: (value: VALUE) => MAPPED | Promise<MAPPED>): (stream: Stream<VALUE>) => Stream<MAPPED>;
|
|
317
|
+
<VALUE, MAPPED, ACC>(initialValue: ACC, mapper: (accumulator: ACC, value: VALUE) => [MAPPED, ACC] | Promise<[MAPPED, ACC]>): (stream: Stream<VALUE>) => Stream<MAPPED>;
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Creates a map transformer for use with pipe().
|
|
321
|
+
*
|
|
322
|
+
* @param mapper - Function to transform each value, or initial accumulator value
|
|
323
|
+
* @param mapper - Optional mapper function when using accumulator
|
|
324
|
+
* @returns Transformer function for pipe()
|
|
325
|
+
*
|
|
326
|
+
* @example
|
|
327
|
+
* ```typescript
|
|
328
|
+
* const numbers = new Stream<number>();
|
|
329
|
+
*
|
|
330
|
+
* // Simple mapping
|
|
331
|
+
* const doubled = numbers.pipe(map(n => n * 2));
|
|
332
|
+
*
|
|
333
|
+
* // Stateful mapping (running sum)
|
|
334
|
+
* const sums = numbers.pipe(map(0, (sum, n) => [sum + n, sum + n]));
|
|
335
|
+
*
|
|
336
|
+
* // Async mapping
|
|
337
|
+
* const enriched = numbers.pipe(map(async n => {
|
|
338
|
+
* const data = await fetchData(n);
|
|
339
|
+
* return { value: n, data };
|
|
340
|
+
* }));
|
|
341
|
+
* ```
|
|
342
|
+
*/
|
|
343
|
+
export declare const map: MapFunction;
|
|
344
|
+
/**
|
|
345
|
+
* Transformer function interface for filtering stream values.
|
|
346
|
+
* Supports type guards, boolean predicates, and stateful filtering.
|
|
347
|
+
*/
|
|
348
|
+
export interface FilterFunction {
|
|
349
|
+
<VALUE, FILTERED extends VALUE>(predicate: (value: VALUE) => value is FILTERED): (stream: Stream<VALUE>) => Stream<FILTERED>;
|
|
350
|
+
<VALUE>(predicate: (value: VALUE) => boolean | Promise<boolean>): (stream: Stream<VALUE>) => Stream<VALUE>;
|
|
351
|
+
<VALUE, ACC>(initialValue: ACC, predicate: (accumulator: ACC, value: VALUE) => [boolean, ACC] | Promise<[boolean, ACC]>): (stream: Stream<VALUE>) => Stream<VALUE>;
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Creates a filter transformer for use with pipe().
|
|
355
|
+
*
|
|
356
|
+
* @param predicate - Function to test each value, or initial accumulator value
|
|
357
|
+
* @param predicate - Optional predicate function when using accumulator
|
|
358
|
+
* @returns Transformer function for pipe()
|
|
359
|
+
*
|
|
360
|
+
* @example
|
|
361
|
+
* ```typescript
|
|
362
|
+
* const numbers = new Stream<number>();
|
|
363
|
+
*
|
|
364
|
+
* // Simple filtering
|
|
365
|
+
* const positives = numbers.pipe(filter(n => n > 0));
|
|
366
|
+
*
|
|
367
|
+
* // Type guard filtering
|
|
368
|
+
* const strings = mixed.pipe(filter((x): x is string => typeof x === 'string'));
|
|
369
|
+
*
|
|
370
|
+
* // Stateful filtering (only increasing values)
|
|
371
|
+
* const increasing = numbers.pipe(filter(0, (prev, curr) =>
|
|
372
|
+
* [curr > prev, Math.max(prev, curr)]
|
|
373
|
+
* ));
|
|
374
|
+
* ```
|
|
375
|
+
*/
|
|
376
|
+
export declare const filter: FilterFunction;
|
|
377
|
+
/**
|
|
378
|
+
* Transformer function interface for grouping stream values into batches.
|
|
379
|
+
* Supports both simple batching and stateful grouping with accumulators.
|
|
380
|
+
*/
|
|
381
|
+
export interface GroupFunction {
|
|
382
|
+
<VALUE>(predicate: (accumulator: VALUE[], value: VALUE) => boolean | Promise<boolean>): (stream: Stream<VALUE>) => Stream<VALUE[]>;
|
|
383
|
+
<VALUE, ACC>(initialValue: ACC, predicate: (accumulator: ACC, value: VALUE) => [boolean, ACC] | Promise<[boolean, ACC]>): (stream: Stream<VALUE>) => Stream<ACC>;
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Creates a group transformer for use with pipe().
|
|
387
|
+
*
|
|
388
|
+
* @param predicate - Function to determine when to emit a group, or initial accumulator value
|
|
389
|
+
* @param predicate - Optional predicate function when using accumulator
|
|
390
|
+
* @returns Transformer function for pipe()
|
|
391
|
+
*
|
|
392
|
+
* @example
|
|
393
|
+
* ```typescript
|
|
394
|
+
* const numbers = new Stream<number>();
|
|
395
|
+
*
|
|
396
|
+
* // Group by count
|
|
397
|
+
* const batches = numbers.pipe(group(batch => batch.length >= 5));
|
|
398
|
+
*
|
|
399
|
+
* // Group by sum
|
|
400
|
+
* const sumGroups = numbers.pipe(group(0, (sum, n) =>
|
|
401
|
+
* sum + n >= 100 ? [true, 0] : [false, sum + n]
|
|
402
|
+
* ));
|
|
403
|
+
*
|
|
404
|
+
* // Time-based grouping
|
|
405
|
+
* const timeGroups = events.pipe(group([], (window, event) => {
|
|
406
|
+
* const now = Date.now();
|
|
407
|
+
* const recent = window.filter(e => now - e.timestamp < 5000);
|
|
408
|
+
* return [recent.length >= 10, [...recent, event]];
|
|
409
|
+
* }));
|
|
410
|
+
* ```
|
|
411
|
+
*/
|
|
412
|
+
export declare const group: GroupFunction;
|
|
413
|
+
/**
|
|
414
|
+
* Transformer function interface for merging multiple streams.
|
|
415
|
+
* Combines values from all streams into a single output stream.
|
|
416
|
+
*/
|
|
417
|
+
export interface MergeFunction {
|
|
418
|
+
<STREAMS extends [Stream<any>, ...Stream<any>[]]>(...streams: STREAMS): <VALUE>(stream: Stream<VALUE>) => Stream<VALUE | ValueOf<STREAMS[number]>>;
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Creates a merge transformer for use with pipe().
|
|
422
|
+
*
|
|
423
|
+
* @param streams - Additional streams to merge with the source stream
|
|
424
|
+
* @returns Transformer function for pipe()
|
|
425
|
+
*
|
|
426
|
+
* @example
|
|
427
|
+
* ```typescript
|
|
428
|
+
* const stream1 = new Stream<string>();
|
|
429
|
+
* const stream2 = new Stream<number>();
|
|
430
|
+
* const stream3 = new Stream<boolean>();
|
|
431
|
+
*
|
|
432
|
+
* // Merge multiple streams
|
|
433
|
+
* const merged = stream1.pipe(merge(stream2, stream3));
|
|
434
|
+
* // Type: Stream<string | number | boolean>
|
|
435
|
+
*
|
|
436
|
+
* merged.listen(value => {
|
|
437
|
+
* console.log('Received:', value); // Could be string, number, or boolean
|
|
438
|
+
* });
|
|
439
|
+
*
|
|
440
|
+
* stream1.push('hello');
|
|
441
|
+
* stream2.push(42);
|
|
442
|
+
* stream3.push(true);
|
|
443
|
+
* ```
|
|
444
|
+
*/
|
|
445
|
+
export declare const merge: MergeFunction;
|
|
446
|
+
/**
|
|
447
|
+
* Transformer function interface for flattening array values in streams.
|
|
448
|
+
* Supports configurable depth for nested array flattening.
|
|
449
|
+
*/
|
|
450
|
+
export interface FlatFunction {
|
|
451
|
+
(): <VALUE>(stream: Stream<VALUE>) => Stream<FlatArray<VALUE, 0>>;
|
|
452
|
+
<DEPTH extends number>(depth: DEPTH): <VALUE>(stream: Stream<VALUE>) => Stream<FlatArray<VALUE, DEPTH>>;
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* Creates a flat transformer for use with pipe().
|
|
456
|
+
*
|
|
457
|
+
* @param depth - Optional depth to flatten (default: 0 for single level)
|
|
458
|
+
* @returns Transformer function for pipe()
|
|
459
|
+
*
|
|
460
|
+
* @example
|
|
461
|
+
* ```typescript
|
|
462
|
+
* const arrays = new Stream<number[]>();
|
|
463
|
+
*
|
|
464
|
+
* // Flatten single level
|
|
465
|
+
* const flattened = arrays.pipe(flat());
|
|
466
|
+
* arrays.push([1, 2], [3, 4]); // Emits: 1, 2, 3, 4
|
|
467
|
+
*
|
|
468
|
+
* // Flatten multiple levels
|
|
469
|
+
* const nested = new Stream<number[][][]>();
|
|
470
|
+
* const deepFlat = nested.pipe(flat(2));
|
|
471
|
+
* nested.push([[[1, 2]], [[3, 4]]]); // Emits: 1, 2, 3, 4
|
|
472
|
+
*
|
|
473
|
+
* // Mixed content (non-arrays pass through)
|
|
474
|
+
* const mixed = new Stream<number | number[]>();
|
|
475
|
+
* const result = mixed.pipe(flat());
|
|
476
|
+
* mixed.push(1, [2, 3], 4); // Emits: 1, 2, 3, 4
|
|
477
|
+
* ```
|
|
478
|
+
*/
|
|
479
|
+
export declare const flat: FlatFunction;
|
|
480
|
+
//# sourceMappingURL=stream.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,OAAO,CAAC,MAAM,IAAI,MAAM,SAAS,MAAM,CAAC,MAAM,KAAK,CAAC,GAAG,KAAK,GAAG,KAAK,CAAC;AAEjF;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH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