-
Notifications
You must be signed in to change notification settings - Fork 6
/
stream.ts
1322 lines (1222 loc) · 36.7 KB
/
stream.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* The stream module includes construction and combinator functions for a push
* stream datatype. Streams in fun are very close to the Streams of mostjs as
* well as the Observables of rxjs. There are few differences that come from
* a nuanced selection of invariants. Those invariants are:
*
* 1. Streams are lazy by default, and will not start collecting or emitting
* events until they are run.
* 2. A stream must be connected to a sink for events to be consumed. A sink is
* an object with a notion of accepting an event message and an end message.
* 3. When a stream is run by linking it with a sink it will return a
* Disposable, which can be used to cancel the operation of the stream early.
* 4. Once a stream is started it must call end when it completes. If the stream
* is disposed it will not call end.
*
* @module Stream
* @experimental
* @since 2.2.0
*/
import type { In, Kind, Out } from "./kind.ts";
import type { Wrappable } from "./wrappable.ts";
import type { BindTo, Mappable } from "./mappable.ts";
import type { Applicable } from "./applicable.ts";
import type { Bind, Flatmappable, Tap } from "./flatmappable.ts";
import type { Predicate } from "./predicate.ts";
import type { Refinement } from "./refinement.ts";
import type { Option } from "./option.ts";
import type { Either } from "./either.ts";
import type { Pair } from "./pair.ts";
import * as O from "./option.ts";
import * as E from "./either.ts";
import * as A from "./array.ts";
import { createBind, createTap } from "./flatmappable.ts";
import { createBindTo } from "./mappable.ts";
import { pair } from "./pair.ts";
import { flow, pipe } from "./fn.ts";
/**
* Represents a sink for receiving values emitted by a stream.
*
* @since 2.2.0
*/
export type Sink<A> = {
readonly event: (value: A) => void;
readonly end: (reason?: unknown) => void;
};
/**
* Represents a stream that emits values of type `A`.
*
* @since 2.2.0
*/
export type Stream<A, R = unknown> = (sink: Sink<A>, env: R) => Disposable;
/**
* Specifies Stream as a Higher Kinded Type, with covariant
* parameter A corresponding to the 0th index of any substitutions.
*
* @since 2.2.0
*/
export interface KindStream extends Kind {
readonly kind: Stream<Out<this, 0>, In<this, 0>>;
}
/**
* Represents a stream with unknown value type and any environment.
*
* @since 2.2.0
*/
// deno-lint-ignore no-explicit-any
export type AnyStream = Stream<any, any>;
/**
* Extracts the value type from a stream type.
*
* @since 2.2.0
*/
export type TypeOf<U> = U extends Stream<infer A, infer _> ? A : never;
/**
* Extracts the environment type from a stream type.
*
* @since 2.2.0
*/
export type EnvOf<U> = U extends Stream<infer _, infer R> ? R : never;
/**
* Represents a timeout object with `setTimeout` and `clearTimeout` methods.
*
* @since 2.2.0
*/
export type Timeout = {
readonly setTimeout: typeof setTimeout;
readonly clearTimeout: typeof clearTimeout;
};
/**
* Represents an interval object with `setInterval` and `clearInterval` methods.
*/
export type Interval = {
readonly setInterval: typeof setInterval;
readonly clearInterval: typeof clearInterval;
};
/**
* @since 2.2.0
*/
export type DefaultEnv = Timeout & Interval;
/**
* Creates a disposable resource with a dispose function. The resource can be disposed of
* by calling the `dispose` method. If `dispose` is called more than once, an error will be thrown.
*
* @param dispose A function that disposes of the resource.
* @returns A Disposable object with a dispose method.
*
* @since 2.2.0
*/
export function disposable(dispose: () => void): Disposable {
return { [Symbol.dispose]: dispose };
}
/**
* Disposes of a disposable resource by calling its dispose method.
*
* @since 2.2.0
*/
export function dispose(disposable: Disposable): void {
return disposable[Symbol.dispose]();
}
/**
* Creates a `Disposable` that does nothing when disposed.
*
* @since 2.0.0
*/
export function disposeNone(): Disposable {
return disposable(() => {});
}
/**
* @since 2.2.0
*/
export function sink<A>(
event: (value: A) => void,
end: (reason?: unknown) => void,
): Sink<A> {
return { event, end };
}
/**
* Creates a sink that does nothing when receiving events or ends
*
* @since 2.2.0
*/
export function emptySink(): Sink<unknown> {
return sink(NOOP, NOOP);
}
/**
* Creates a stream with a run function. The run function is responsible for
* managing the interaction with the provided sink and environment. It returns
* a Disposable object that can be used to clean up any resources associated
* with the stream.
*
* @since 2.2.0
*/
export function stream<A, R = unknown>(
run: (sink: Sink<A>, env: R) => Disposable,
): Stream<A, R> {
return run;
}
/**
* Runs a stream until completion, returning a disposable to stop the stream
* early.
*
* @param stream The stream to run.
* @param sink The sink to send event and end messages to.
* @param env The environment to run the stream in.
* @returns A disposable that can cancel the stream.
*
* @since 2.2.0
*/
export function run(): <A>(stream: Stream<A, typeof DefaultEnv>) => Disposable;
export function run<R>(env: R): <A>(stream: Stream<A, R>) => Disposable;
export function run<A, R>(
env: R,
sink: Sink<A>,
): (stream: Stream<A, R>) => Disposable;
export function run<A, R>(
env: R = DefaultEnv as R,
sink: Sink<A> = emptySink(),
): (stream: Stream<A, R>) => Disposable {
return (stream) => stream(sink, env);
}
/**
* Runs a stream until completion, returning a promise that resolves when the stream ends.
*
* @param stream The stream to run.
* @param env The environment to run the stream in.
* @returns A promise that resolves when the stream ends.
*
* @since 2.2.0
*/
export function runPromise(): <A>(
stream: Stream<A, typeof DefaultEnv>,
) => Promise<unknown>;
export function runPromise<R>(
env: R,
): <A>(stream: Stream<A, R>) => Promise<unknown>;
export function runPromise<R>(
env: R = DefaultEnv as R,
): <A>(stream: Stream<A, R>) => Promise<unknown> {
return (stream) =>
new Promise<unknown>((resolve) =>
pipe(stream, run(env, sink(NOOP, resolve)))
);
}
/**
* Runs a stream until completion, calling the onEvent when events arrive and
* onEnd when the stream ends.
*
* @param onEvent The function to run on each stream event.
* @param onEnd The function to run on stream end.
* @returns A function that takes a stream and returns a disposable
*
* @since 2.2.0
*/
export function forEach<A>(
onEvent: (value: A) => void,
onEnd?: (reason?: unknown) => void,
): (ua: Stream<A, typeof DefaultEnv>) => Disposable;
export function forEach<A, R>(
onEvent: (value: A) => void,
onEnd: (reason?: unknown) => void,
env: R,
): (ua: Stream<A, R>) => Disposable;
export function forEach<A, R = unknown>(
onEvent: (value: A) => void = NOOP,
onEnd: (reason?: unknown) => void = NOOP,
env: R = DefaultEnv as R,
): (ua: Stream<A, R>) => Disposable {
return run(env, sink(onEvent, onEnd));
}
/**
* Runs a stream, collecting eny events into an array, then returning the array
* once the stream ends.
*
* @since 2.2.0
*/
export function collect(): <A>(
stream: Stream<A, typeof DefaultEnv>,
) => Promise<ReadonlyArray<A>>;
export function collect<R>(
env: R,
): <A>(stream: Stream<A, R>) => Promise<ReadonlyArray<A>>;
export function collect<R>(
env: R = DefaultEnv as R,
): <A>(stream: Stream<A, R>) => Promise<ReadonlyArray<A>> {
return <A = never>(stream: Stream<A, R>): Promise<ReadonlyArray<A>> =>
new Promise((resolve) => {
const result: A[] = [];
pipe(
stream,
run(
env,
sink((value) => result.push(value), () => resolve(result)),
),
);
});
}
const NOOP: () => void = () => {};
/**
* A Stream instance that emits no values and immediately ends.
*/
const EMPTY: Stream<never, unknown> = stream(
(snk) => {
let open = true;
const close = () => open = false;
queueMicrotask(() => open && snk.end());
return disposable(close);
},
);
/**
* Creates an empty `Stream`, which emits no events and ends immediately.
*
* @since 2.2.0
*/
export function empty<A = never, R = unknown>(): Stream<A, R> {
return EMPTY;
}
/**
* Creates a `Stream` that never emits any events and never ends.
*
* @since 2.2.0
*/
export function never<A = never, R = unknown>(): Stream<A, R> {
return stream(() => disposable(NOOP));
}
/**
* Creates a `Stream` that emits a single event when the provided promise resolves, and then ends.
*
* @param ua The promise to convert into a stream.
* @returns A `Stream` that emits the resolved value of the provided promise and then ends.
*
* @since 2.0.0
*/
export function fromPromise<A>(ua: Promise<A>): Stream<A> {
return stream((snk) => {
let open = true;
const close = () => open = false;
ua.then((value) => {
if (open) {
close();
snk.event(value);
snk.end();
}
});
return disposable(close);
});
}
/**
* Creates a `Stream` that emits events from the provided iterable and then ends.
*
* @param values The iterable whose values will be emitted by the stream.
* @returns A `Stream` that emits each value from the provided iterable and then ends.
*
* @since 2.0.0
*/
export function fromIterable<A>(values: Iterable<A>): Stream<A> {
return stream((snk) => {
let open = true;
const close = () => open = false;
queueMicrotask(() => {
if (open) {
for (const value of values) {
snk.event(value);
}
snk.end();
}
});
return disposable(close);
});
}
/**
* Creates a stream that emits a single value after a specified delay.
*
* @param time The time in milliseconds after which the value should be emitted.
* @returns A stream that emits a single value after the specified delay.
*
* @since 2.2.0
*/
export function at(time: number): Stream<number, Timeout> {
return stream(
(snk: Sink<number>, { setTimeout, clearTimeout }: Timeout) => {
const handle = setTimeout(
() => {
snk.event(time);
snk.end();
},
time,
);
return disposable(() => {
clearTimeout(handle);
});
},
);
}
/**
* Creates a stream that emits incremental values at regular intervals.
*
* @param period The time interval in milliseconds between each emitted value.
* @returns A stream that emits incremental values at regular intervals.
*
* @since 2.2.0
*/
export function periodic(period: number): Stream<number, Interval> {
return stream((snk: Sink<number>, env: Interval) => {
let start = 0;
const { setInterval, clearInterval } = env;
const handle = setInterval(
() => {
snk.event(start += period);
},
period,
);
return disposable(() => {
clearInterval(handle);
});
});
}
/**
* Combines two streams, emitting events from the first stream until it
* ends, and then continuing with events from the second stream.
*
* @param second A function that returns the second stream to be concatenated.
* @returns A function that takes the first stream and returns a new stream concatenating events from both streams.
*
* @since 2.0.0
*/
export function combine<A2, R2>(
second: Stream<A2, R2>,
): <A1, R1>(first: Stream<A1, R1>) => Stream<A1 | A2, R1 & R2> {
return <A1, R1>(first: Stream<A1, R1>): Stream<A1 | A2, R1 & R2> =>
pipe(
fromIterable<Stream<A1 | A2, R1 & R2>>([first, second]),
join(1),
);
}
/**
* Maps the values of a stream from one type to another using a provided function.
*
* @param fai A function that maps values from type `A` to type `I`.
* @returns A higher-order function that takes a stream of type `A` and returns a new stream of type `I`.
*
* @since 2.2.0
*/
export function map<A, I>(
fai: (a: A) => I,
): <R>(ua: Stream<A, R>) => Stream<I, R> {
return (ua) =>
stream((snk, env) => ua(sink((a) => snk.event(fai(a)), snk.end), env));
}
/**
* Wraps a single value into a stream.
*
* @param value The value to be wrapped into the stream.
* @returns A stream containing the provided value.
*
* @since 2.2.0
*/
export function wrap<A, R = unknown>(value: A): Stream<A, R> {
return stream((snk) => {
let open = true;
const close = () => open = false;
queueMicrotask(() => {
if (open) {
snk.event(value);
snk.end();
}
});
return disposable(close);
});
}
/**
* Creates a new stream that only emits values from the original stream that satisfy the provided predicate function.
*
* @param predicate A function that determines whether a value should be emitted (`true`) or filtered out (`false`).
* @returns A higher-order function that takes a stream and returns a new stream containing only the values that satisfy the predicate.
*
* @since 2.2.0
*/
export function filter<A, B extends A>(
refinement: (a: A) => a is B,
): <R>(s: Stream<A, R>) => Stream<B, R>;
export function filter<A>(
predicate: (a: A) => boolean,
): <R>(s: Stream<A, R>) => Stream<A, R>;
export function filter<A>(
predicate: (a: A) => boolean,
): <R>(ua: Stream<A, R>) => Stream<A, R> {
return (ua) =>
stream((snk, env) =>
ua(
sink((value) => {
if (predicate(value)) {
snk.event(value);
}
}, snk.end),
env,
)
);
}
/**
* Apply a filter and mapping operation at the same time against a Stream.
*
* @since 2.2.0
*/
export function filterMap<A, I>(
fai: (a: A) => Option<I>,
): <R = unknown>(ua: Stream<A, R>) => Stream<I, R> {
return (ua) =>
stream((snk, env) =>
pipe(
ua,
run(
env,
sink((value) => {
const oi = fai(value);
if (O.isSome(oi)) {
snk.event(oi.value);
}
}, snk.end),
),
)
);
}
/**
* Given a refinement or predicate, return a function that splits an Stream into
* a Pair<Stream<A>, Stream<B>>.
*
* @since 2.2.0
*/
export function partition<A, B extends A>(
refinement: Refinement<A, B>,
): <R = unknown>(ua: Stream<A, R>) => Pair<Stream<B, R>, Stream<A, R>>;
export function partition<A>(
predicate: Predicate<A>,
): <R = unknown>(ua: Stream<A, R>) => Pair<Stream<A, R>, Stream<A, R>>;
export function partition<A>(
predicate: Predicate<A>,
): <R = unknown>(ua: Stream<A, R>) => Pair<Stream<A, R>, Stream<A, R>> {
return (ua) =>
pair(pipe(ua, filter(predicate)), pipe(ua, filter((a) => !predicate(a))));
}
/**
* Map and partition over the inner value of an Stream<A> at the same time.
*
* @since 2.0.0
*/
export function partitionMap<A, I, J>(
fai: (a: A) => Either<J, I>,
): <R = unknown>(ua: Stream<A, R>) => Pair<Stream<I, R>, Stream<J, R>> {
return (ua) =>
pair(
pipe(ua, filterMap(flow(fai, E.getRight))),
pipe(ua, filterMap(flow(fai, E.getLeft))),
);
}
/**
* Creates a new stream by continuously applying a function to a seed value and the values of the original stream.
*
* @param stepper A function that takes the current state and a value from the original stream, and returns an array containing the new state and the value to be emitted by the new stream.
* @param seed The initial state value.
* @returns A higher-order function that takes a stream and returns a new stream resulting from applying the stepper function to each value of the original stream.
*
* @since 2.2.0
*/
export function loop<A, B, S>(
stepper: (state: S, value: A) => [S, B],
seed: S,
): <R = unknown>(ua: Stream<A, R>) => Stream<B, R> {
return (ua) =>
stream((snk, env) => {
let hold: S = seed;
return pipe(
ua,
run(
env,
sink((a) => {
const [seed, value] = stepper(hold, a);
hold = seed;
snk.event(value);
}, snk.end),
),
);
});
}
/**
* Creates a new stream by accumulating values from the original stream using a provided scanning function.
*
* @param scanner A function that takes the current accumulator value and a value from the original stream, and returns the new accumulator value.
* @param seed The initial accumulator value.
* @returns A higher-order function that takes a stream and returns a new stream with accumulated values.
*
* @since 2.2.0
*/
export function scan<A, O>(
scanner: (accumulator: O, value: A) => O,
seed: O,
): <R>(ua: Stream<A, R>) => Stream<O, R> {
return (ua) =>
stream((snk, env) => {
let state = seed;
return ua(
sink(
(value) => {
state = scanner(state, value);
snk.event(state);
},
snk.end,
),
env,
);
});
}
/**
* Creates a new stream by concurrently merging multiple streams into one stream.
* The concurrency of the join can be set and defaults to positive infinity,
* indicating unbounded join. A strategy can also be supplied, which controls
* how inner streams are handled when maximum concurrency is met.
*
* *Hold Strategy*: This strategy keeps an ordered queue of streams to pull from
* when running inner streams end. This strategy has no loss of data.
*
* *Swap Strategy*: This strategy will dispose the oldest running inner stream
* when a new stream arrives to make space for the newest stream. In this
* strategy the oldest streams are where we lose data.
*
* *Drop Strategy": This strategy will ignore any new streams once concurrency
* is maxed. In this strategy newest streams are where we lose data.
*
* There is room for a combination of strategies in the future, but the vast
* majority of behaviors are representable with these three.
*
* @param concurrency The maximum number of inner streams to be running concurrently.
* @param strategy The strategy to use once concurrency is saturated.
* @returns A higher-order function that takes a stream of streams and returns a new stream containing values from all inner streams.
*
* @since 2.2.0
*/
export function join(
concurrency = Number.POSITIVE_INFINITY,
strategy: "hold" | "swap" | "drop" = "hold",
): <A, R1, R2>(
ua: Stream<Stream<A, R1>, R2>,
) => Stream<A, R1 & R2> {
return <A, R1, R2>(
ua: Stream<Stream<A, R1>, R2>,
): Stream<A, R1 & R2> =>
stream((outerSnk, env) => {
let outerClosed = false;
const queue: Stream<A, R1>[] = [];
const running = new Map<Sink<A>, Disposable>(); // WeakMap?
/**
* Start inner should only be called when there is enough concurrency to
* support starting a new stream.
*/
function startInner(strm: Stream<A, R1>) {
const innerSnk = sink<A>(outerSnk.event, () => {
/**
* We know that this inner stream has ended so we no longer need to
* dispose of it. Thus we start by removing the sink/disposable pair
* from our running map.
*/
if (running.has(innerSnk)) {
running.delete(innerSnk);
}
/**
* Next we decide if we should start a new stream by checking the
* concurrency and the queue. There might be a bug here but I haven't
* desk checked it yet. If the call to startInner here leads to a
* sync stream and that stream calls end it might lead to a double
* end call.
*
* I don't think it will because the inner stream started here should
* see running.size === 1, but I'm not entirely sure.
*/
if (running.size < concurrency && queue.length > 0) {
startInner(queue.shift() as Stream<A, R1>);
}
/**
* Lastly, we check to see if we are the last end call by looking at
* whether the outer stream is closed and whether we are running any
* inner streams still.
*/
if (outerClosed && running.size === 0) {
outerSnk.end();
}
});
/**
* Lastly, we start the innerStream and store it's disposable in
* running, indexed by the innerSnk created above.
*/
running.set(innerSnk, run(env, innerSnk)(strm));
}
const dsp = ua(
sink((strm) => {
/**
* If there is enough concurrency to start an inner stream then we
* start it. Otherwise we move on to queueing using the provided
* strategy.
*/
if (running.size < concurrency) {
return startInner(strm);
}
/**
* For the hold strategy we queue up any inner streams above
* concurrency. As running inner streams end they will be pulled from
* the queue.
*/
if (strategy === "hold") {
return queue.push(strm);
}
/**
* For the swap strategy we use the unique property of Map that when
* converted to an array it will be put into insertion order. This may
* have some performance impacts, in which case switching to another
* data structure may benefit. For now, the simplicity of this
* implementation wins.
*
* We start by finding the oldest running inner stream, disposing it,
* and starting the new stream. In other libraries this behavior is
* called left switch.
*/
if (strategy === "swap") {
pipe(
Array.from(running),
A.lookup(0),
O.match(
// If there is no running stream then something is likely wrong?
NOOP,
([oldestSink, oldestDsp]) => {
running.delete(oldestSink);
dispose(oldestDsp);
startInner(strm);
},
),
);
}
/**
* The drop strategy would go here, however its behavior is
* to ignore new streams so it is a noop.
*/
}, () => {
/**
* There are two cases for end coming from the outer stream. There are
* either inner streams left to process or not. When there are inner
* streams then the startInner function will handle end. If there
* aren't then end must be called here.
*/
outerClosed = true;
if (running.size === 0 && queue.length == 0) {
outerSnk.end();
}
}),
env,
);
/**
* In join both the outer and inner streams must be disposed. By design,
* disposed streams call end after their cleanup is complete. For join
* the process of dispose is to first dispose of the outer stream if it
* isn't closed, then to clear the queue, and last dispose of the inner
* streams. The last inner stream to be disposed is expected to call end,
* so the machinery of streams should lead to a single call to end.
*/
return disposable(() => {
queue.length = 0;
running.forEach(dispose);
if (!outerClosed) {
dispose(dsp);
}
});
});
}
/**
* Maps each value of the input stream to a new stream using a provided function,
* then flattens the resulting streams into a single stream.
*
* @param faui A function that maps each value of the input stream to a new stream.
* @param count The maximum number of inner streams to be running concurrently.
* @returns A higher-order function that takes a stream and returns a new stream
* containing values from all mapped streams
*
* @since 2.2.0
*/
export function flatmap<A, I, R2 = unknown>(
faui: (a: A) => Stream<I, R2>,
concurrency = Number.POSITIVE_INFINITY,
): <R1>(ua: Stream<A, R1>) => Stream<I, R1 & R2> {
return (ua) => pipe(ua, map(faui), join(concurrency));
}
export function switchmap<A, I, R2>(
faui: (a: A) => Stream<I, R2>,
concurrency = 1,
): <R1>(ua: Stream<A, R1>) => Stream<I, R1 & R2> {
return <R1>(ua: Stream<A, R1>): Stream<I, R1 & R2> =>
pipe(ua, map(faui), join(concurrency, "swap"));
}
export function exhaustmap<A, I, R2>(
faui: (a: A) => Stream<I, R2>,
concurrency = 1,
): <R1>(ua: Stream<A, R1>) => Stream<I, R1 & R2> {
return <R1>(ua: Stream<A, R1>): Stream<I, R1 & R2> =>
pipe(ua, map(faui), join(concurrency, "drop"));
}
/**
* Applies each value of the input stream to a stream of functions,
* producing a stream of results. Apply may lose data if the underlying streams
* push events in a tight loop. This is because in a tight loop the runtime does
* not allow pausing between events from one of the two streams.
*
* @param ua The input stream.
* @returns A higher-order function that takes a stream of functions and returns
* a new stream containing the results of applying each value of the input stream
* to the corresponding function.
*
* @since 2.2.0
*/
export function apply<A, R2 = never>(
ua: Stream<A, R2>,
): <I, R1>(ufai: Stream<(a: A) => I, R1>) => Stream<I, R2 & R1> {
return <I, R1>(ufai: Stream<(a: A) => I, R1>): Stream<I, R1 & R2> =>
stream((snk, env) => {
let valueDone = false;
let fnDone = false;
let fai: Option<(a: A) => I> = O.none;
let a: Option<A> = O.none;
function send() {
pipe(fai, O.apply(a), O.tap((i) => snk.event(i)));
}
const dsp_ufai = ufai(
sink((fn) => {
fai = O.some(fn);
send();
}, () => {
fnDone = true;
if (valueDone) {
snk.end();
}
}),
env,
);
const dsp_ua = ua(
sink((value) => {
a = O.some(value);
send();
}, () => {
valueDone = true;
if (fnDone) {
snk.end();
}
}),
env,
);
return disposable(() => {
if (!valueDone) {
dispose(dsp_ua);
}
if (!fnDone) {
dispose(dsp_ufai);
}
});
});
}
/**
* Creates a new stream by combining each value of the input stream with an index value generated by a provided indexing function.
*
* @param indexer A function that takes the current state and returns an array containing the index value and the new state.
* @param seed The initial state value.
* @returns A higher-order function that takes a stream and returns a new stream containing tuples of index-value pairs.
*
* @since 2.2.0
*/
export function indexed<S, I>(
indexer: (seed: S) => [I, S],
seed: S,
): <A, R = unknown>(ua: Stream<A, R>) => Stream<[I, A], R> {
return loop((previous, value) => {
const [index, next] = indexer(previous);
return [next, [index, value]];
}, seed);
}
/**
* Creates a new stream by combining each value of the input stream with an index value.
*
* @param start The starting index value.
* @param step The increment step for generating index values.
* @returns A higher-order function that takes a stream and returns a new stream containing tuples of index-value pairs.
*
* @since 2.2.0
*/
export function withIndex(
start: number = 0,
step: number = 1,
): <A, R = unknown>(ua: Stream<A, R>) => Stream<[number, A], R> {
return indexed((i) => [i, i + step], start);
}
/**
* Creates a new stream by combining each value of the input stream with a count index starting from 1.
*
* @param ua The input stream.
* @returns A stream containing tuples of count-value pairs.
*
* @since 2.2.0
*/
export function withCount<A, R = unknown>(
ua: Stream<A, R>,
): Stream<[number, A], R> {
return withIndex(1)(ua);
}
/**
* Creates a new stream by counting the number of values emitted by the input stream.
*
* @param ua The input stream.
* @returns A stream containing the count of values emitted by the input stream.
*
* @since 2.2.0
*/
export function count<A, R = unknown>(ua: Stream<A, R>): Stream<number, R> {
return pipe(ua, withCount, map(([i]) => i));
}
/**
* Creates a new stream that emits values from the input stream until a condition specified by the predicate function is met.
*
* @param predicate A function that determines whether to continue emitting values (`true`) or stop emitting values (`false`).
* @returns A higher-order function that takes a stream and returns a new stream containing values until the condition specified by the predicate function is met.
*
* @since 2.2.0
*/
export function takeUntil<A>(
predicate: (a: A) => boolean,
): <R = unknown>(ua: Stream<A, R>) => Stream<A, R> {
return (ua) =>
stream((snk, env) => {
const dsp = ua(
sink((a) => {
if (predicate(a)) {
snk.event(a);
dispose(dsp);
} else {
snk.event(a);
}
}, snk.end),
env,
);
return dsp;
});
}
/**
* Creates a new stream that emits a specified number of values from the input stream.
*
* @param count The number of values to emit.
* @returns A higher-order function that takes a stream and returns a new stream containing the specified number of values.
*
* @since 2.2.0
*/
export function take(count: number): <A, R>(ua: Stream<A, R>) => Stream<A, R> {
return (ua) => {
if (count <= 0) {
return empty();
}
return stream((snk, env) => {
let index = Math.max(0, count);
const dsp = ua(
sink(
(value) => {
snk.event(value);
if (--index <= 0) {
dispose(dsp);
snk.end();
}
},
snk.end,
),
env,
);
return dsp;
});
};
}