reactor-core-ts 2.1.6 → 3.2.1
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 +927 -658
- package/dist/_virtual/_@oxc-project_runtime@0.139.0/helpers/esm/defineProperty.js +12 -0
- package/dist/_virtual/_@oxc-project_runtime@0.139.0/helpers/esm/toPrimitive.js +14 -0
- package/dist/_virtual/_@oxc-project_runtime@0.139.0/helpers/esm/toPropertyKey.js +9 -0
- package/dist/_virtual/_@oxc-project_runtime@0.139.0/helpers/esm/typeof.js +11 -0
- package/dist/context/context-view.d.ts +31 -0
- package/dist/context/context-view.d.ts.map +1 -0
- package/dist/context/context-view.js +58 -0
- package/dist/context/context-view.js.map +1 -0
- package/dist/context/context.d.ts +46 -0
- package/dist/context/context.d.ts.map +1 -0
- package/dist/context/context.js +125 -0
- package/dist/context/context.js.map +1 -0
- package/dist/context/types.d.ts +7 -0
- package/dist/context/types.d.ts.map +1 -0
- package/dist/core/boolean-disposable.d.ts +19 -0
- package/dist/core/boolean-disposable.d.ts.map +1 -0
- package/dist/core/boolean-disposable.js +37 -0
- package/dist/core/boolean-disposable.js.map +1 -0
- package/dist/core/composite-disposable.d.ts +21 -0
- package/dist/core/composite-disposable.d.ts.map +1 -0
- package/dist/core/composite-disposable.js +64 -0
- package/dist/core/composite-disposable.js.map +1 -0
- package/dist/core/demand.d.ts +9 -0
- package/dist/core/demand.d.ts.map +1 -0
- package/dist/core/demand.js +21 -0
- package/dist/core/demand.js.map +1 -0
- package/dist/core/disposables.d.ts +12 -0
- package/dist/core/disposables.d.ts.map +1 -0
- package/dist/core/disposables.js +37 -0
- package/dist/core/disposables.js.map +1 -0
- package/dist/core/teardown.d.ts +4 -0
- package/dist/core/teardown.d.ts.map +1 -0
- package/dist/core/types.d.ts +14 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/errors/classes.d.ts +37 -0
- package/dist/errors/classes.d.ts.map +1 -0
- package/dist/errors/classes.js +53 -0
- package/dist/errors/classes.js.map +1 -0
- package/dist/errors/exceptions.d.ts +15 -0
- package/dist/errors/exceptions.d.ts.map +1 -0
- package/dist/errors/helpers.d.ts +9 -0
- package/dist/errors/helpers.d.ts.map +1 -0
- package/dist/errors/helpers.js +18 -0
- package/dist/errors/helpers.js.map +1 -0
- package/dist/index.d.ts +17 -2556
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -2
- package/dist/internal/abort.d.ts +5 -0
- package/dist/internal/abort.d.ts.map +1 -0
- package/dist/internal/abort.js +36 -0
- package/dist/internal/abort.js.map +1 -0
- package/dist/internal/async-queue.d.ts +48 -0
- package/dist/internal/async-queue.d.ts.map +1 -0
- package/dist/internal/async-queue.js +187 -0
- package/dist/internal/async-queue.js.map +1 -0
- package/dist/internal/iterable-terminal.d.ts +12 -0
- package/dist/internal/iterable-terminal.d.ts.map +1 -0
- package/dist/internal/iterable-terminal.js +49 -0
- package/dist/internal/iterable-terminal.js.map +1 -0
- package/dist/internal/iterable-transform.d.ts +24 -0
- package/dist/internal/iterable-transform.d.ts.map +1 -0
- package/dist/internal/iterable-transform.js +159 -0
- package/dist/internal/iterable-transform.js.map +1 -0
- package/dist/internal/iterable.d.ts +23 -0
- package/dist/internal/iterable.d.ts.map +1 -0
- package/dist/internal/iterable.js +81 -0
- package/dist/internal/iterable.js.map +1 -0
- package/dist/internal/microtask.d.ts +7 -0
- package/dist/internal/microtask.d.ts.map +1 -0
- package/dist/internal/microtask.js +19 -0
- package/dist/internal/microtask.js.map +1 -0
- package/dist/publisher/browser-sources.d.ts +15 -0
- package/dist/publisher/browser-sources.d.ts.map +1 -0
- package/dist/publisher/browser-sources.js +92 -0
- package/dist/publisher/browser-sources.js.map +1 -0
- package/dist/publisher/flux-entrypoint.d.ts +7 -0
- package/dist/publisher/flux-entrypoint.d.ts.map +1 -0
- package/dist/publisher/flux-entrypoint.js +11 -0
- package/dist/publisher/flux-entrypoint.js.map +1 -0
- package/dist/publisher/flux.d.ts +121 -0
- package/dist/publisher/flux.d.ts.map +1 -0
- package/dist/publisher/flux.js +697 -0
- package/dist/publisher/flux.js.map +1 -0
- package/dist/publisher/helpers.d.ts +19 -0
- package/dist/publisher/helpers.d.ts.map +1 -0
- package/dist/publisher/helpers.js +84 -0
- package/dist/publisher/helpers.js.map +1 -0
- package/dist/publisher/mono-entrypoint.d.ts +7 -0
- package/dist/publisher/mono-entrypoint.d.ts.map +1 -0
- package/dist/publisher/mono-entrypoint.js +11 -0
- package/dist/publisher/mono-entrypoint.js.map +1 -0
- package/dist/publisher/mono.d.ts +72 -0
- package/dist/publisher/mono.d.ts.map +1 -0
- package/dist/publisher/mono.js +373 -0
- package/dist/publisher/mono.js.map +1 -0
- package/dist/publisher/operators/aggregate.d.ts +35 -0
- package/dist/publisher/operators/aggregate.d.ts.map +1 -0
- package/dist/publisher/operators/aggregate.js +217 -0
- package/dist/publisher/operators/aggregate.js.map +1 -0
- package/dist/publisher/operators/compat/flux.d.ts +56 -0
- package/dist/publisher/operators/compat/flux.d.ts.map +1 -0
- package/dist/publisher/operators/compat/flux.js +156 -0
- package/dist/publisher/operators/compat/flux.js.map +1 -0
- package/dist/publisher/operators/compat/mono.d.ts +82 -0
- package/dist/publisher/operators/compat/mono.d.ts.map +1 -0
- package/dist/publisher/operators/compat/mono.js +125 -0
- package/dist/publisher/operators/compat/mono.js.map +1 -0
- package/dist/publisher/operators/coordination.d.ts +82 -0
- package/dist/publisher/operators/coordination.d.ts.map +1 -0
- package/dist/publisher/operators/coordination.js +345 -0
- package/dist/publisher/operators/coordination.js.map +1 -0
- package/dist/publisher/operators/error.d.ts +17 -0
- package/dist/publisher/operators/error.d.ts.map +1 -0
- package/dist/publisher/operators/error.js +48 -0
- package/dist/publisher/operators/error.js.map +1 -0
- package/dist/publisher/operators/flatten.d.ts +19 -0
- package/dist/publisher/operators/flatten.d.ts.map +1 -0
- package/dist/publisher/operators/flatten.js +127 -0
- package/dist/publisher/operators/flatten.js.map +1 -0
- package/dist/publisher/operators/lifecycle.d.ts +103 -0
- package/dist/publisher/operators/lifecycle.d.ts.map +1 -0
- package/dist/publisher/operators/lifecycle.js +237 -0
- package/dist/publisher/operators/lifecycle.js.map +1 -0
- package/dist/publisher/operators/mono-parity.d.ts +109 -0
- package/dist/publisher/operators/mono-parity.d.ts.map +1 -0
- package/dist/publisher/operators/mono-parity.js +144 -0
- package/dist/publisher/operators/mono-parity.js.map +1 -0
- package/dist/publisher/operators/mono.d.ts +30 -0
- package/dist/publisher/operators/mono.d.ts.map +1 -0
- package/dist/publisher/operators/mono.js +37 -0
- package/dist/publisher/operators/mono.js.map +1 -0
- package/dist/publisher/operators/register.d.ts +20 -0
- package/dist/publisher/operators/register.d.ts.map +1 -0
- package/dist/publisher/operators/register.js +23 -0
- package/dist/publisher/operators/register.js.map +1 -0
- package/dist/publisher/operators/selection.d.ts +25 -0
- package/dist/publisher/operators/selection.d.ts.map +1 -0
- package/dist/publisher/operators/selection.js +108 -0
- package/dist/publisher/operators/selection.js.map +1 -0
- package/dist/publisher/operators/side-effect.d.ts +15 -0
- package/dist/publisher/operators/side-effect.d.ts.map +1 -0
- package/dist/publisher/operators/side-effect.js +56 -0
- package/dist/publisher/operators/side-effect.js.map +1 -0
- package/dist/publisher/operators/terminal.d.ts +16 -0
- package/dist/publisher/operators/terminal.d.ts.map +1 -0
- package/dist/publisher/operators/terminal.js +47 -0
- package/dist/publisher/operators/terminal.js.map +1 -0
- package/dist/publisher/operators/time.d.ts +20 -0
- package/dist/publisher/operators/time.d.ts.map +1 -0
- package/dist/publisher/operators/time.js +92 -0
- package/dist/publisher/operators/time.js.map +1 -0
- package/dist/publisher/operators/transform.d.ts +31 -0
- package/dist/publisher/operators/transform.d.ts.map +1 -0
- package/dist/publisher/operators/transform.js +74 -0
- package/dist/publisher/operators/transform.js.map +1 -0
- package/dist/publisher/operators/windowing.d.ts +28 -0
- package/dist/publisher/operators/windowing.d.ts.map +1 -0
- package/dist/publisher/operators/windowing.js +148 -0
- package/dist/publisher/operators/windowing.js.map +1 -0
- package/dist/publisher/publisher.d.ts +11 -0
- package/dist/publisher/publisher.d.ts.map +1 -0
- package/dist/publisher/types.d.ts +51 -0
- package/dist/publisher/types.d.ts.map +1 -0
- package/dist/schedulers/animation-frame-scheduler.d.ts +11 -0
- package/dist/schedulers/animation-frame-scheduler.d.ts.map +1 -0
- package/dist/schedulers/animation-frame-scheduler.js +30 -0
- package/dist/schedulers/animation-frame-scheduler.js.map +1 -0
- package/dist/schedulers/base-scheduler.d.ts +28 -0
- package/dist/schedulers/base-scheduler.d.ts.map +1 -0
- package/dist/schedulers/base-scheduler.js +77 -0
- package/dist/schedulers/base-scheduler.js.map +1 -0
- package/dist/schedulers/basic-worker.d.ts +22 -0
- package/dist/schedulers/basic-worker.d.ts.map +1 -0
- package/dist/schedulers/basic-worker.js +71 -0
- package/dist/schedulers/basic-worker.js.map +1 -0
- package/dist/schedulers/delay-scheduler.d.ts +12 -0
- package/dist/schedulers/delay-scheduler.d.ts.map +1 -0
- package/dist/schedulers/delay-scheduler.js +17 -0
- package/dist/schedulers/delay-scheduler.js.map +1 -0
- package/dist/schedulers/disposed.d.ts +4 -0
- package/dist/schedulers/disposed.d.ts.map +1 -0
- package/dist/schedulers/disposed.js +14 -0
- package/dist/schedulers/disposed.js.map +1 -0
- package/dist/schedulers/duration.d.ts +8 -0
- package/dist/schedulers/duration.d.ts.map +1 -0
- package/dist/schedulers/duration.js +11 -0
- package/dist/schedulers/duration.js.map +1 -0
- package/dist/schedulers/executor-scheduler.d.ts +13 -0
- package/dist/schedulers/executor-scheduler.d.ts.map +1 -0
- package/dist/schedulers/executor-scheduler.js +43 -0
- package/dist/schedulers/executor-scheduler.js.map +1 -0
- package/dist/schedulers/immediate-scheduler.d.ts +15 -0
- package/dist/schedulers/immediate-scheduler.d.ts.map +1 -0
- package/dist/schedulers/immediate-scheduler.js +24 -0
- package/dist/schedulers/immediate-scheduler.js.map +1 -0
- package/dist/schedulers/index.d.ts +7 -0
- package/dist/schedulers/index.d.ts.map +1 -0
- package/dist/schedulers/index.js +2 -0
- package/dist/schedulers/interval-scheduler.d.ts +12 -0
- package/dist/schedulers/interval-scheduler.d.ts.map +1 -0
- package/dist/schedulers/interval-scheduler.js +17 -0
- package/dist/schedulers/interval-scheduler.js.map +1 -0
- package/dist/schedulers/macro-scheduler.d.ts +19 -0
- package/dist/schedulers/macro-scheduler.d.ts.map +1 -0
- package/dist/schedulers/macro-scheduler.js +30 -0
- package/dist/schedulers/macro-scheduler.js.map +1 -0
- package/dist/schedulers/microtask-scheduler.d.ts +11 -0
- package/dist/schedulers/microtask-scheduler.d.ts.map +1 -0
- package/dist/schedulers/microtask-scheduler.js +34 -0
- package/dist/schedulers/microtask-scheduler.js.map +1 -0
- package/dist/schedulers/schedulers.d.ts +39 -0
- package/dist/schedulers/schedulers.d.ts.map +1 -0
- package/dist/schedulers/schedulers.js +83 -0
- package/dist/schedulers/schedulers.js.map +1 -0
- package/dist/schedulers/timeout-scheduler.d.ts +11 -0
- package/dist/schedulers/timeout-scheduler.d.ts.map +1 -0
- package/dist/schedulers/types.d.ts +35 -0
- package/dist/schedulers/types.d.ts.map +1 -0
- package/dist/signal/signal-type.d.ts +26 -0
- package/dist/signal/signal-type.d.ts.map +1 -0
- package/dist/signal/signal-type.js +31 -0
- package/dist/signal/signal-type.js.map +1 -0
- package/dist/signal/signal.d.ts +33 -0
- package/dist/signal/signal.d.ts.map +1 -0
- package/dist/signal/signal.js +70 -0
- package/dist/signal/signal.js.map +1 -0
- package/dist/sinks/index.d.ts +7 -0
- package/dist/sinks/index.d.ts.map +1 -0
- package/dist/sinks/index.js +2 -0
- package/dist/sinks/many.d.ts +66 -0
- package/dist/sinks/many.d.ts.map +1 -0
- package/dist/sinks/many.js +193 -0
- package/dist/sinks/many.js.map +1 -0
- package/dist/sinks/one.d.ts +41 -0
- package/dist/sinks/one.d.ts.map +1 -0
- package/dist/sinks/one.js +144 -0
- package/dist/sinks/one.js.map +1 -0
- package/dist/sinks/specs.d.ts +25 -0
- package/dist/sinks/specs.d.ts.map +1 -0
- package/dist/sinks/specs.js +126 -0
- package/dist/sinks/specs.js.map +1 -0
- package/dist/sinks/subscribers.d.ts +16 -0
- package/dist/sinks/subscribers.d.ts.map +1 -0
- package/dist/sinks/subscribers.js +32 -0
- package/dist/sinks/subscribers.js.map +1 -0
- package/dist/sinks/types.d.ts +137 -0
- package/dist/sinks/types.d.ts.map +1 -0
- package/dist/sinks/types.js +58 -0
- package/dist/sinks/types.js.map +1 -0
- package/dist/subscription/core-subscriber.d.ts +12 -0
- package/dist/subscription/core-subscriber.d.ts.map +1 -0
- package/dist/subscription/iterable-subscription.d.ts +64 -0
- package/dist/subscription/iterable-subscription.d.ts.map +1 -0
- package/dist/subscription/iterable-subscription.js +281 -0
- package/dist/subscription/iterable-subscription.js.map +1 -0
- package/dist/subscription/reactive-streams.d.ts +8 -0
- package/dist/subscription/reactive-streams.d.ts.map +1 -0
- package/dist/subscription/reactive-streams.js +0 -0
- package/dist/subscription/subscriber.d.ts +17 -0
- package/dist/subscription/subscriber.d.ts.map +1 -0
- package/dist/subscription/subscription.d.ts +12 -0
- package/dist/subscription/subscription.d.ts.map +1 -0
- package/package.json +99 -33
- package/LICENSE.md +0 -56
- package/dist/index.d.mts +0 -2557
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -2
- package/dist/index.mjs.map +0 -1
|
@@ -0,0 +1,697 @@
|
|
|
1
|
+
import _defineProperty from "../_virtual/_@oxc-project_runtime@0.139.0/helpers/esm/defineProperty.js";
|
|
2
|
+
import { Context } from "../context/context.js";
|
|
3
|
+
import { EMPTY_ITERABLE, closeAsyncIterator, isAsyncIterable, isIterable, neverIterable, toAsyncIterator } from "../internal/iterable.js";
|
|
4
|
+
import { mapIterable } from "../internal/iterable-transform.js";
|
|
5
|
+
import { BooleanDisposable } from "../core/boolean-disposable.js";
|
|
6
|
+
import { CompositeError } from "../errors/classes.js";
|
|
7
|
+
import { asError, throwIfNullish } from "../errors/helpers.js";
|
|
8
|
+
import { firstValueFrom, raceWithAbort } from "../internal/abort.js";
|
|
9
|
+
import { AsyncQueue } from "../internal/async-queue.js";
|
|
10
|
+
import { collectIterable, lastIterableValue } from "../internal/iterable-terminal.js";
|
|
11
|
+
import { eventSource, webSocketSource } from "./browser-sources.js";
|
|
12
|
+
import { Schedulers } from "../schedulers/schedulers.js";
|
|
13
|
+
import { addCancelCallback, isSubscriber, runCancelCallbacks, scheduleDelay } from "./helpers.js";
|
|
14
|
+
import { IterableSubscription } from "../subscription/iterable-subscription.js";
|
|
15
|
+
//#region src/publisher/flux.ts
|
|
16
|
+
/**
|
|
17
|
+
* @packageDocumentation
|
|
18
|
+
* Flux, Mono and operator implementation modules.
|
|
19
|
+
*/
|
|
20
|
+
var _Symbol$asyncIterator = Symbol.asyncIterator;
|
|
21
|
+
/** Multi-value Reactive Streams publisher with Reactor-style operators. */
|
|
22
|
+
var Flux = class Flux {
|
|
23
|
+
/** Creates a Flux from a lazy source factory. */
|
|
24
|
+
constructor(sourceFactory) {
|
|
25
|
+
_defineProperty(
|
|
26
|
+
this,
|
|
27
|
+
/** Lazy source factory invoked for each subscription or async iteration. */
|
|
28
|
+
"sourceFactory",
|
|
29
|
+
void 0
|
|
30
|
+
);
|
|
31
|
+
this.sourceFactory = sourceFactory;
|
|
32
|
+
}
|
|
33
|
+
/** Creates a Flux that emits the provided values and then completes. */
|
|
34
|
+
static just(...values) {
|
|
35
|
+
if (values.length === 0) return Flux.empty();
|
|
36
|
+
for (const value of values) throwIfNullish(value, "value");
|
|
37
|
+
return Flux.fromIterable(values);
|
|
38
|
+
}
|
|
39
|
+
/** Creates a Flux from an array-like readonly value list. */
|
|
40
|
+
static fromArray(values) {
|
|
41
|
+
throwIfNullish(values, "values");
|
|
42
|
+
if (values.length === 0) return Flux.empty();
|
|
43
|
+
return Flux.fromIterable(values);
|
|
44
|
+
}
|
|
45
|
+
/** Creates a Flux from a stream-like iterable or a lazy stream supplier. */
|
|
46
|
+
static fromStream(stream) {
|
|
47
|
+
throwIfNullish(stream, "stream");
|
|
48
|
+
return typeof stream === "function" ? Flux.defer(() => Flux.fromIterable(stream())) : Flux.fromIterable(stream);
|
|
49
|
+
}
|
|
50
|
+
/** Creates a Flux from a JavaScript iterable. */
|
|
51
|
+
static fromIterable(values) {
|
|
52
|
+
throwIfNullish(values, "values");
|
|
53
|
+
if (Array.isArray(values) && values.length === 0) return Flux.empty();
|
|
54
|
+
return new Flux(() => values);
|
|
55
|
+
}
|
|
56
|
+
/** Creates a Flux from browser events and removes the listener on cancellation. */
|
|
57
|
+
static fromEvent(target, type, options) {
|
|
58
|
+
return new Flux(eventSource(target, type, options));
|
|
59
|
+
}
|
|
60
|
+
/** Creates a Flux from browser WebSocket message events and cleans listeners on cancellation. */
|
|
61
|
+
static fromWebSocket(input, options) {
|
|
62
|
+
return new Flux(webSocketSource(input, options));
|
|
63
|
+
}
|
|
64
|
+
/** Adapts a publisher, iterable, async iterable or promise-like value to Flux. */
|
|
65
|
+
static from(input) {
|
|
66
|
+
throwIfNullish(input, "input");
|
|
67
|
+
if (input instanceof Flux) return input;
|
|
68
|
+
if (isAsyncIterable(input)) return new Flux(() => input);
|
|
69
|
+
if (isIterable(input)) return Flux.fromIterable(input);
|
|
70
|
+
if (typeof input.subscribe === "function") return fromPublisher(input);
|
|
71
|
+
if (typeof input.then === "function") return new Flux(async function* (signal) {
|
|
72
|
+
const value = await raceWithAbort(Promise.resolve(input), signal);
|
|
73
|
+
if (value !== void 0 && value !== null) yield value;
|
|
74
|
+
});
|
|
75
|
+
throw new TypeError("Unsupported publisher input");
|
|
76
|
+
}
|
|
77
|
+
/** Creates a Flux that completes without emitting values. */
|
|
78
|
+
static empty() {
|
|
79
|
+
return EMPTY_FLUX;
|
|
80
|
+
}
|
|
81
|
+
/** Creates a Flux that never emits or completes until cancelled. */
|
|
82
|
+
static never() {
|
|
83
|
+
return NEVER_FLUX;
|
|
84
|
+
}
|
|
85
|
+
/** Creates a Flux that terminates with the provided error. */
|
|
86
|
+
static error(error) {
|
|
87
|
+
return new Flux(async function* () {
|
|
88
|
+
throw typeof error === "function" ? error() : error;
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
/** Defers source creation until subscription time. */
|
|
92
|
+
static defer(supplier) {
|
|
93
|
+
return new Flux((signal, context) => Flux.from(supplier()).iterate(signal, context));
|
|
94
|
+
}
|
|
95
|
+
/** Defers source creation and exposes the subscriber context. */
|
|
96
|
+
static deferContextual(supplier) {
|
|
97
|
+
return new Flux((signal, context) => Flux.from(supplier(context.readOnly())).iterate(signal, context));
|
|
98
|
+
}
|
|
99
|
+
/** Creates a Flux by bridging callback-style producers through a sink. */
|
|
100
|
+
static create(callback) {
|
|
101
|
+
throwIfNullish(callback, "callback");
|
|
102
|
+
return new Flux((signal, context) => {
|
|
103
|
+
const queue = new AsyncQueue(signal);
|
|
104
|
+
let cancelled = false;
|
|
105
|
+
let cancelCallbacks;
|
|
106
|
+
const sink = {
|
|
107
|
+
/** Emits one value into the created Flux. */
|
|
108
|
+
next(value) {
|
|
109
|
+
throwIfNullish(value, "value");
|
|
110
|
+
queue.push(value);
|
|
111
|
+
},
|
|
112
|
+
/** Terminates the created Flux with an error. */
|
|
113
|
+
error(error) {
|
|
114
|
+
queue.error(error);
|
|
115
|
+
},
|
|
116
|
+
/** Completes the created Flux. */
|
|
117
|
+
complete() {
|
|
118
|
+
queue.complete();
|
|
119
|
+
},
|
|
120
|
+
/** Registers a callback invoked when the subscription is cancelled. */
|
|
121
|
+
onCancel(cancelCallback) {
|
|
122
|
+
cancelCallbacks = addCancelCallback(cancelCallbacks, cancelCallback);
|
|
123
|
+
return sink;
|
|
124
|
+
},
|
|
125
|
+
/** Returns true after cancellation has been observed. */
|
|
126
|
+
isCancelled() {
|
|
127
|
+
return cancelled || signal.aborted;
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
signal.addEventListener("abort", () => {
|
|
131
|
+
cancelled = true;
|
|
132
|
+
runCancelCallbacks(cancelCallbacks);
|
|
133
|
+
cancelCallbacks = void 0;
|
|
134
|
+
queue.complete();
|
|
135
|
+
}, { once: true });
|
|
136
|
+
try {
|
|
137
|
+
callback(sink);
|
|
138
|
+
} catch (error) {
|
|
139
|
+
queue.error(error);
|
|
140
|
+
}
|
|
141
|
+
return queue;
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
/** Creates a Flux by bridging a single-threaded push-style producer through a sink. */
|
|
145
|
+
static push(callback) {
|
|
146
|
+
return Flux.create(callback);
|
|
147
|
+
}
|
|
148
|
+
/** Creates a Flux by repeatedly invoking a synchronous generator callback. */
|
|
149
|
+
static generate(generator) {
|
|
150
|
+
throwIfNullish(generator, "generator");
|
|
151
|
+
return new Flux(async function* (signal) {
|
|
152
|
+
let done = false;
|
|
153
|
+
let hasValue = false;
|
|
154
|
+
let value;
|
|
155
|
+
const sink = {
|
|
156
|
+
/** Emits one generated value for this turn. */
|
|
157
|
+
next(nextValue) {
|
|
158
|
+
if (hasValue) throw new Error("Synchronous generator can emit at most one value per turn");
|
|
159
|
+
hasValue = true;
|
|
160
|
+
value = nextValue;
|
|
161
|
+
},
|
|
162
|
+
/** Completes the generated sequence. */
|
|
163
|
+
complete() {
|
|
164
|
+
done = true;
|
|
165
|
+
},
|
|
166
|
+
/** Fails the generated sequence. */
|
|
167
|
+
error(error) {
|
|
168
|
+
throw error;
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
while (!done && !signal.aborted) {
|
|
172
|
+
hasValue = false;
|
|
173
|
+
value = void 0;
|
|
174
|
+
generator(sink);
|
|
175
|
+
if (hasValue) yield value;
|
|
176
|
+
else if (!done) throw new Error("Synchronous generator must emit, complete or fail");
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
/** Creates a finite integer range. */
|
|
181
|
+
static range(start, count) {
|
|
182
|
+
if (!Number.isInteger(start) || !Number.isInteger(count) || count < 0) throw new RangeError("Flux.range expects an integer start and a non-negative integer count");
|
|
183
|
+
if (count === 0) return Flux.empty();
|
|
184
|
+
return new Flux(function* () {
|
|
185
|
+
for (let i = 0; i < count; i += 1) yield start + i;
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
/** Creates an interval Flux that emits incrementing numbers. */
|
|
189
|
+
static interval(period, scheduler = Schedulers.timeout()) {
|
|
190
|
+
return new Flux(async function* (signal) {
|
|
191
|
+
let index = 0;
|
|
192
|
+
while (!signal.aborted) {
|
|
193
|
+
await scheduleDelay(scheduler, period, signal);
|
|
194
|
+
if (!signal.aborted) {
|
|
195
|
+
yield index;
|
|
196
|
+
index += 1;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
/** Merges several publishers concurrently. */
|
|
202
|
+
static merge(...sources) {
|
|
203
|
+
if (sources.length === 0) return Flux.empty();
|
|
204
|
+
if (sources.length === 1) return Flux.from(sources[0]);
|
|
205
|
+
return mergeSources(toFluxArray(sources));
|
|
206
|
+
}
|
|
207
|
+
/** Merges several publishers concurrently and delays failures until every source terminates. */
|
|
208
|
+
static mergeDelayError(...sources) {
|
|
209
|
+
if (sources.length === 0) return Flux.empty();
|
|
210
|
+
if (sources.length === 1) return Flux.from(sources[0]);
|
|
211
|
+
return mergeSourcesDelayError(toFluxArray(sources));
|
|
212
|
+
}
|
|
213
|
+
/** Merges sources sequentially while subscribing to one source at a time. */
|
|
214
|
+
static mergeSequential(...sources) {
|
|
215
|
+
return Flux.concat(...sources);
|
|
216
|
+
}
|
|
217
|
+
/** Merges sources sequentially and delays failures until every source has been consumed. */
|
|
218
|
+
static mergeSequentialDelayError(...sources) {
|
|
219
|
+
return concatDelayErrorSources(sources);
|
|
220
|
+
}
|
|
221
|
+
/** Merges finite sources and emits their values sorted by a comparator. */
|
|
222
|
+
static mergeComparing(comparatorOrSource, ...sources) {
|
|
223
|
+
const { comparator, inputs } = orderedMergeInputs(comparatorOrSource, sources);
|
|
224
|
+
return sortMergedSources(inputs, comparator);
|
|
225
|
+
}
|
|
226
|
+
/** Merges finite sources, delays failures and emits their values sorted by a comparator. */
|
|
227
|
+
static mergeComparingDelayError(comparatorOrSource, ...sources) {
|
|
228
|
+
const { comparator, inputs } = orderedMergeInputs(comparatorOrSource, sources);
|
|
229
|
+
return sortMergedSourcesDelayError(inputs, comparator);
|
|
230
|
+
}
|
|
231
|
+
/** Merges finite sources and emits their values in comparator order. */
|
|
232
|
+
static mergeOrdered(comparatorOrSource, ...sources) {
|
|
233
|
+
return Flux.mergeComparing(comparatorOrSource, ...sources);
|
|
234
|
+
}
|
|
235
|
+
/** Merges finite sources with priority ordering. */
|
|
236
|
+
static mergePriority(comparatorOrSource, ...sources) {
|
|
237
|
+
return Flux.mergeComparing(comparatorOrSource, ...sources);
|
|
238
|
+
}
|
|
239
|
+
/** Merges finite sources with priority ordering and delayed failures. */
|
|
240
|
+
static mergePriorityDelayError(comparatorOrSource, ...sources) {
|
|
241
|
+
return Flux.mergeComparingDelayError(comparatorOrSource, ...sources);
|
|
242
|
+
}
|
|
243
|
+
/** Concatenates several publishers sequentially. */
|
|
244
|
+
static concat(...sources) {
|
|
245
|
+
if (sources.length === 0) return Flux.empty();
|
|
246
|
+
if (sources.length === 1) return Flux.from(sources[0]);
|
|
247
|
+
return new Flux(async function* (signal, context) {
|
|
248
|
+
for (const source of sources) for await (const value of Flux.from(source).iterate(signal, context)) {
|
|
249
|
+
if (signal.aborted) return;
|
|
250
|
+
yield value;
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
/** Concatenates several publishers sequentially and delays failures until every source has been consumed. */
|
|
255
|
+
static concatDelayError(...sources) {
|
|
256
|
+
if (sources.length === 0) return Flux.empty();
|
|
257
|
+
if (sources.length === 1) return Flux.from(sources[0]);
|
|
258
|
+
return concatDelayErrorSources(sources);
|
|
259
|
+
}
|
|
260
|
+
/** Relays the first source that emits any signal. */
|
|
261
|
+
static first(...sources) {
|
|
262
|
+
return Flux.firstWithSignal(...sources);
|
|
263
|
+
}
|
|
264
|
+
/** Relays the first source that emits any signal. */
|
|
265
|
+
static firstWithSignal(...sources) {
|
|
266
|
+
if (sources.length === 0) return Flux.empty();
|
|
267
|
+
if (sources.length === 1) return Flux.from(sources[0]);
|
|
268
|
+
return new Flux((signal, context) => {
|
|
269
|
+
const queue = new AsyncQueue(signal);
|
|
270
|
+
let settled = false;
|
|
271
|
+
for (const source of sources) (async () => {
|
|
272
|
+
try {
|
|
273
|
+
const iterator = toAsyncIterator(Flux.from(source).iterate(signal, context));
|
|
274
|
+
try {
|
|
275
|
+
const first = await raceWithAbort(iterator.next(), signal);
|
|
276
|
+
if (settled) {
|
|
277
|
+
await iterator.return?.();
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
settled = true;
|
|
281
|
+
if (first.done) {
|
|
282
|
+
queue.complete();
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
queue.push(first.value);
|
|
286
|
+
for (;;) {
|
|
287
|
+
const next = await raceWithAbort(iterator.next(), signal);
|
|
288
|
+
if (next.done) {
|
|
289
|
+
queue.complete();
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
queue.push(next.value);
|
|
293
|
+
}
|
|
294
|
+
} finally {
|
|
295
|
+
await iterator.return?.();
|
|
296
|
+
}
|
|
297
|
+
} catch (error) {
|
|
298
|
+
if (!settled) {
|
|
299
|
+
settled = true;
|
|
300
|
+
queue.error(error);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
})();
|
|
304
|
+
return queue;
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
/** Relays the first source that produces a value, ignoring empty completions. */
|
|
308
|
+
static firstWithValue(...sources) {
|
|
309
|
+
if (sources.length === 0) return Flux.empty();
|
|
310
|
+
return new Flux((signal, context) => {
|
|
311
|
+
const queue = new AsyncQueue(signal);
|
|
312
|
+
let remaining = sources.length;
|
|
313
|
+
let settled = false;
|
|
314
|
+
const errors = [];
|
|
315
|
+
for (const source of sources) (async () => {
|
|
316
|
+
try {
|
|
317
|
+
const value = await firstValueFrom(Flux.from(source), signal);
|
|
318
|
+
if (settled) return;
|
|
319
|
+
if (value !== void 0) {
|
|
320
|
+
settled = true;
|
|
321
|
+
queue.push(value);
|
|
322
|
+
queue.complete();
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
} catch (error) {
|
|
326
|
+
errors.push(error);
|
|
327
|
+
}
|
|
328
|
+
remaining -= 1;
|
|
329
|
+
if (remaining === 0 && !settled) errors.length > 0 ? queue.error(new CompositeError(errors)) : queue.complete();
|
|
330
|
+
})();
|
|
331
|
+
return queue;
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
/** Zips publishers by combining one value from each source. */
|
|
335
|
+
static zip(sources, combinator) {
|
|
336
|
+
const sourceCount = sources.length;
|
|
337
|
+
if (sourceCount === 0) return Flux.empty();
|
|
338
|
+
if (sourceCount === 1) return singleTupleSource(sources[0], combinator);
|
|
339
|
+
return new Flux(async function* (signal, context) {
|
|
340
|
+
const iterators = new Array(sourceCount);
|
|
341
|
+
const nexts = new Array(sourceCount);
|
|
342
|
+
for (let index = 0; index < sourceCount; index += 1) iterators[index] = toAsyncIterator(Flux.from(sources[index]).iterate(signal, context));
|
|
343
|
+
try {
|
|
344
|
+
while (!signal.aborted) {
|
|
345
|
+
for (let index = 0; index < sourceCount; index += 1) nexts[index] = raceWithAbort(iterators[index].next(), signal);
|
|
346
|
+
const results = await Promise.all(nexts);
|
|
347
|
+
const values = new Array(sourceCount);
|
|
348
|
+
for (let index = 0; index < sourceCount; index += 1) {
|
|
349
|
+
const result = results[index];
|
|
350
|
+
if (result.done) return;
|
|
351
|
+
values[index] = result.value;
|
|
352
|
+
}
|
|
353
|
+
const tuple = values;
|
|
354
|
+
yield combinator ? combinator(tuple) : tuple;
|
|
355
|
+
}
|
|
356
|
+
} finally {
|
|
357
|
+
for (const iterator of iterators) await closeAsyncIterator(iterator);
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
/** Combines the latest value from each source after all sources emitted once. */
|
|
362
|
+
static combineLatest(sources, combinator) {
|
|
363
|
+
if (sources.length === 0) return Flux.empty();
|
|
364
|
+
if (sources.length === 1) return singleTupleSource(sources[0], combinator);
|
|
365
|
+
return new Flux((signal, context) => {
|
|
366
|
+
const queue = new AsyncQueue(signal);
|
|
367
|
+
const sourceCount = sources.length;
|
|
368
|
+
const latest = new Array(sourceCount);
|
|
369
|
+
const hasValue = new Array(sourceCount);
|
|
370
|
+
let readyCount = 0;
|
|
371
|
+
let completed = 0;
|
|
372
|
+
let failed = false;
|
|
373
|
+
for (let index = 0; index < sourceCount; index += 1) {
|
|
374
|
+
const source = sources[index];
|
|
375
|
+
(async () => {
|
|
376
|
+
try {
|
|
377
|
+
for await (const value of Flux.from(source).iterate(signal, context)) {
|
|
378
|
+
latest[index] = value;
|
|
379
|
+
if (hasValue[index] !== true) {
|
|
380
|
+
hasValue[index] = true;
|
|
381
|
+
readyCount += 1;
|
|
382
|
+
}
|
|
383
|
+
if (readyCount === sourceCount) {
|
|
384
|
+
const values = latest.slice();
|
|
385
|
+
queue.push(combinator ? combinator(values) : values);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
completed += 1;
|
|
389
|
+
if (completed === sourceCount && !failed) queue.complete();
|
|
390
|
+
} catch (error) {
|
|
391
|
+
failed = true;
|
|
392
|
+
queue.error(error);
|
|
393
|
+
}
|
|
394
|
+
})();
|
|
395
|
+
}
|
|
396
|
+
return queue;
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
/** Switches to the most recent publisher emitted by an outer publisher. */
|
|
400
|
+
static switchOnNext(sources) {
|
|
401
|
+
return new Flux((signal, context) => {
|
|
402
|
+
const queue = new AsyncQueue(signal);
|
|
403
|
+
let active = 0;
|
|
404
|
+
let outerDone = false;
|
|
405
|
+
let failed = false;
|
|
406
|
+
const completeIfDone = () => {
|
|
407
|
+
if (outerDone && active === 0 && !failed) queue.complete();
|
|
408
|
+
};
|
|
409
|
+
(async () => {
|
|
410
|
+
const iterator = toAsyncIterator(Flux.from(sources).iterate(signal, context));
|
|
411
|
+
try {
|
|
412
|
+
let version = 0;
|
|
413
|
+
while (!signal.aborted) {
|
|
414
|
+
const next = await raceWithAbort(iterator.next(), signal);
|
|
415
|
+
if (next.done) break;
|
|
416
|
+
version += 1;
|
|
417
|
+
const current = version;
|
|
418
|
+
active += 1;
|
|
419
|
+
(async () => {
|
|
420
|
+
try {
|
|
421
|
+
for await (const value of Flux.from(next.value).iterate(signal, context)) if (current === version) queue.push(value);
|
|
422
|
+
} catch (error) {
|
|
423
|
+
failed = true;
|
|
424
|
+
queue.error(error);
|
|
425
|
+
} finally {
|
|
426
|
+
active -= 1;
|
|
427
|
+
completeIfDone();
|
|
428
|
+
}
|
|
429
|
+
})();
|
|
430
|
+
}
|
|
431
|
+
outerDone = true;
|
|
432
|
+
completeIfDone();
|
|
433
|
+
} catch (error) {
|
|
434
|
+
failed = true;
|
|
435
|
+
queue.error(error);
|
|
436
|
+
} finally {
|
|
437
|
+
iterator.return?.();
|
|
438
|
+
}
|
|
439
|
+
})();
|
|
440
|
+
return queue;
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
/** Uses a resource for the duration of a derived publisher and cleans it up afterwards. */
|
|
444
|
+
static using(resourceSupplier, sourceFactory, resourceCleanup = closeResource) {
|
|
445
|
+
return new Flux(async function* (signal, context) {
|
|
446
|
+
const resource = resourceSupplier();
|
|
447
|
+
try {
|
|
448
|
+
for await (const value of Flux.from(sourceFactory(resource)).iterate(signal, context)) yield value;
|
|
449
|
+
} finally {
|
|
450
|
+
resourceCleanup(resource);
|
|
451
|
+
}
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
/** Uses an asynchronously supplied resource and runs asynchronous cleanup publishers. */
|
|
455
|
+
static usingWhen(resourceSupplier, sourceFactory, asyncComplete, asyncError, asyncCancel) {
|
|
456
|
+
return new Flux(async function* (signal, context) {
|
|
457
|
+
const resource = await firstValueFrom(Flux.from(resourceSupplier), signal);
|
|
458
|
+
if (resource === void 0) return;
|
|
459
|
+
let failed;
|
|
460
|
+
try {
|
|
461
|
+
for await (const value of Flux.from(sourceFactory(resource)).iterate(signal, context)) yield value;
|
|
462
|
+
} catch (error) {
|
|
463
|
+
failed = error;
|
|
464
|
+
if (asyncError) await drainPublisher(asyncError(resource, error), new AbortController().signal, context);
|
|
465
|
+
throw error;
|
|
466
|
+
} finally {
|
|
467
|
+
if (signal.aborted && asyncCancel) await drainPublisher(asyncCancel(resource), new AbortController().signal, context);
|
|
468
|
+
else if (failed === void 0 && asyncComplete) await drainPublisher(asyncComplete(resource), new AbortController().signal, context);
|
|
469
|
+
}
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
/** Subscribes either a full subscriber or callback handlers. */
|
|
473
|
+
subscribe(subscriberOrNext, onError = throwUnhandledSubscriberError, onComplete = noopSubscriberCallback) {
|
|
474
|
+
if (isSubscriber(subscriberOrNext)) {
|
|
475
|
+
this.subscribeActual(subscriberOrNext);
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
let subscription;
|
|
479
|
+
const disposable = new BooleanDisposable(() => subscription?.cancel());
|
|
480
|
+
const subscriber = {
|
|
481
|
+
/** Stores the subscription and requests unbounded demand. */
|
|
482
|
+
onSubscribe(nextSubscription) {
|
|
483
|
+
subscription = nextSubscription;
|
|
484
|
+
nextSubscription.request(Number.POSITIVE_INFINITY);
|
|
485
|
+
},
|
|
486
|
+
onNext: subscriberOrNext ?? noopSubscriberCallback,
|
|
487
|
+
onError,
|
|
488
|
+
onComplete
|
|
489
|
+
};
|
|
490
|
+
this.subscribeActual(subscriber);
|
|
491
|
+
return disposable;
|
|
492
|
+
}
|
|
493
|
+
/** Subscribes a normalized Reactive Streams subscriber to this Flux. */
|
|
494
|
+
subscribeActual(subscriber) {
|
|
495
|
+
const context = subscriber.currentContext?.() ?? Context.empty();
|
|
496
|
+
const subscription = new IterableSubscription(this, subscriber, context);
|
|
497
|
+
subscription.start();
|
|
498
|
+
subscriber.onSubscribe(subscription);
|
|
499
|
+
subscription.deliverStartFailure();
|
|
500
|
+
}
|
|
501
|
+
/** Returns the lazy iterable backing this Flux for internal operator composition. */
|
|
502
|
+
iterate(signal, context = Context.empty()) {
|
|
503
|
+
return this.sourceFactory(signal, context);
|
|
504
|
+
}
|
|
505
|
+
/** Returns an async iterator over this Flux. */
|
|
506
|
+
[_Symbol$asyncIterator]() {
|
|
507
|
+
const controller = new AbortController();
|
|
508
|
+
const iterator = toAsyncIterator(this.iterate(controller.signal, Context.empty()));
|
|
509
|
+
return {
|
|
510
|
+
next: () => iterator.next(),
|
|
511
|
+
return: async (value) => {
|
|
512
|
+
controller.abort();
|
|
513
|
+
if (iterator.return) await iterator.return(value);
|
|
514
|
+
return {
|
|
515
|
+
done: true,
|
|
516
|
+
value
|
|
517
|
+
};
|
|
518
|
+
},
|
|
519
|
+
throw: async (error) => {
|
|
520
|
+
controller.abort();
|
|
521
|
+
if (iterator.throw) return iterator.throw(error);
|
|
522
|
+
throw error;
|
|
523
|
+
}
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
/** Returns this Flux as an async iterable. */
|
|
527
|
+
toIterable() {
|
|
528
|
+
return this;
|
|
529
|
+
}
|
|
530
|
+
/** Collects all values into an array. */
|
|
531
|
+
async toArray() {
|
|
532
|
+
return collectIterable(this.iterate(new AbortController().signal, Context.empty()));
|
|
533
|
+
}
|
|
534
|
+
/** Resolves to the last emitted value, or undefined for an empty source. */
|
|
535
|
+
async toPromise() {
|
|
536
|
+
return lastIterableValue(this.iterate(new AbortController().signal, Context.empty()));
|
|
537
|
+
}
|
|
538
|
+
};
|
|
539
|
+
/** Shared stateless empty Flux instance. */
|
|
540
|
+
var EMPTY_FLUX = new Flux(() => EMPTY_ITERABLE);
|
|
541
|
+
/** Shared stateless never Flux instance. */
|
|
542
|
+
var NEVER_FLUX = new Flux((signal) => neverIterable(signal));
|
|
543
|
+
/** Default cleanup for resources that expose a close method. */
|
|
544
|
+
function closeResource(resource) {
|
|
545
|
+
resource.close?.();
|
|
546
|
+
}
|
|
547
|
+
/** Shared no-op subscriber callback. */
|
|
548
|
+
function noopSubscriberCallback() {}
|
|
549
|
+
/** Throws unhandled subscriber errors like the default callback contract. */
|
|
550
|
+
function throwUnhandledSubscriberError(error) {
|
|
551
|
+
throw asError(error);
|
|
552
|
+
}
|
|
553
|
+
/** Adapts a Reactive Streams publisher to the Flux async-iteration model. */
|
|
554
|
+
function fromPublisher(publisher) {
|
|
555
|
+
return new Flux((signal, context) => {
|
|
556
|
+
const queue = new AsyncQueue(signal);
|
|
557
|
+
let subscription;
|
|
558
|
+
publisher.subscribe({
|
|
559
|
+
/** Captures upstream subscription and requests unbounded demand. */
|
|
560
|
+
onSubscribe(nextSubscription) {
|
|
561
|
+
subscription = nextSubscription;
|
|
562
|
+
nextSubscription.request(Number.POSITIVE_INFINITY);
|
|
563
|
+
},
|
|
564
|
+
/** Pushes upstream values into the bridge queue. */
|
|
565
|
+
onNext(value) {
|
|
566
|
+
queue.push(value);
|
|
567
|
+
},
|
|
568
|
+
/** Fails the bridge queue with the upstream error. */
|
|
569
|
+
onError(error) {
|
|
570
|
+
queue.error(error);
|
|
571
|
+
},
|
|
572
|
+
/** Completes the bridge queue when upstream completes. */
|
|
573
|
+
onComplete() {
|
|
574
|
+
queue.complete();
|
|
575
|
+
}
|
|
576
|
+
});
|
|
577
|
+
signal.addEventListener("abort", () => subscription?.cancel(), { once: true });
|
|
578
|
+
return queue;
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
/** Adapts publisher inputs to Flux instances without callback allocation. */
|
|
582
|
+
function toFluxArray(sources) {
|
|
583
|
+
const fluxes = new Array(sources.length);
|
|
584
|
+
for (let index = 0; index < sources.length; index += 1) fluxes[index] = Flux.from(sources[index]);
|
|
585
|
+
return fluxes;
|
|
586
|
+
}
|
|
587
|
+
/** Emits one-element tuple values without multi-source coordination overhead. */
|
|
588
|
+
function singleTupleSource(source, combinator) {
|
|
589
|
+
return new Flux((signal, context) => mapIterable(Flux.from(source).iterate(signal, context), (value) => {
|
|
590
|
+
const tuple = [value];
|
|
591
|
+
return combinator ? combinator(tuple) : tuple;
|
|
592
|
+
}));
|
|
593
|
+
}
|
|
594
|
+
/** Merges sources concurrently without bounded-concurrency bookkeeping. */
|
|
595
|
+
function mergeSources(sources) {
|
|
596
|
+
return new Flux((signal, context) => {
|
|
597
|
+
const queue = new AsyncQueue(signal);
|
|
598
|
+
const tasks = new Array(sources.length);
|
|
599
|
+
let failed = false;
|
|
600
|
+
(async () => {
|
|
601
|
+
try {
|
|
602
|
+
for (let index = 0; index < sources.length; index += 1) tasks[index] = drainMergedSource(sources[index], signal, context, queue, () => {
|
|
603
|
+
failed = true;
|
|
604
|
+
});
|
|
605
|
+
await Promise.all(tasks);
|
|
606
|
+
if (!failed) queue.complete();
|
|
607
|
+
} catch (error) {
|
|
608
|
+
queue.error(error);
|
|
609
|
+
}
|
|
610
|
+
})();
|
|
611
|
+
return queue;
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
/** Merges sources concurrently and collects failures until all active sources terminate. */
|
|
615
|
+
function mergeSourcesDelayError(sources) {
|
|
616
|
+
return new Flux((signal, context) => {
|
|
617
|
+
const queue = new AsyncQueue(signal);
|
|
618
|
+
const tasks = new Array(sources.length);
|
|
619
|
+
const errors = [];
|
|
620
|
+
(async () => {
|
|
621
|
+
try {
|
|
622
|
+
for (let index = 0; index < sources.length; index += 1) tasks[index] = drainMergedSourceDelayError(sources[index], signal, context, queue, errors);
|
|
623
|
+
await Promise.all(tasks);
|
|
624
|
+
errors.length > 0 ? queue.error(new CompositeError(errors)) : queue.complete();
|
|
625
|
+
} catch (error) {
|
|
626
|
+
queue.error(error);
|
|
627
|
+
}
|
|
628
|
+
})();
|
|
629
|
+
return queue;
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
/** Drains one merged source and reports its first failure through the output queue. */
|
|
633
|
+
async function drainMergedSource(source, signal, context, queue, markFailed) {
|
|
634
|
+
try {
|
|
635
|
+
for await (const value of source.iterate(signal, context)) queue.push(value);
|
|
636
|
+
} catch (error) {
|
|
637
|
+
markFailed();
|
|
638
|
+
queue.error(error);
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
/** Drains one merged source while recording failures for delayed-error merging. */
|
|
642
|
+
async function drainMergedSourceDelayError(source, signal, context, queue, errors) {
|
|
643
|
+
try {
|
|
644
|
+
for await (const value of source.iterate(signal, context)) queue.push(value);
|
|
645
|
+
} catch (error) {
|
|
646
|
+
errors.push(error);
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
/** Concatenates sources while collecting failures until all sources have run. */
|
|
650
|
+
function concatDelayErrorSources(sources) {
|
|
651
|
+
return new Flux(async function* (signal, context) {
|
|
652
|
+
const errors = [];
|
|
653
|
+
for (const source of sources) try {
|
|
654
|
+
for await (const value of Flux.from(source).iterate(signal, context)) yield value;
|
|
655
|
+
} catch (error) {
|
|
656
|
+
errors.push(error);
|
|
657
|
+
}
|
|
658
|
+
if (errors.length > 0) throw new CompositeError(errors);
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
/** Normalizes ordered merge arguments into a comparator and source list. */
|
|
662
|
+
function orderedMergeInputs(comparatorOrSource, sources) {
|
|
663
|
+
if (typeof comparatorOrSource === "function") return {
|
|
664
|
+
comparator: comparatorOrSource,
|
|
665
|
+
inputs: sources
|
|
666
|
+
};
|
|
667
|
+
return {
|
|
668
|
+
comparator: void 0,
|
|
669
|
+
inputs: comparatorOrSource === void 0 ? sources : [comparatorOrSource, ...sources]
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
/** Collects finite merged values and emits them sorted by the optional comparator. */
|
|
673
|
+
function sortMergedSources(sources, comparator) {
|
|
674
|
+
return new Flux(async function* (signal, context) {
|
|
675
|
+
const values = [];
|
|
676
|
+
for await (const value of Flux.merge(...sources).iterate(signal, context)) values.push(value);
|
|
677
|
+
values.sort(comparator);
|
|
678
|
+
yield* values;
|
|
679
|
+
});
|
|
680
|
+
}
|
|
681
|
+
/** Collects finite merged values with delayed errors and emits them sorted by the optional comparator. */
|
|
682
|
+
function sortMergedSourcesDelayError(sources, comparator) {
|
|
683
|
+
return new Flux(async function* (signal, context) {
|
|
684
|
+
const values = [];
|
|
685
|
+
for await (const value of Flux.mergeDelayError(...sources).iterate(signal, context)) values.push(value);
|
|
686
|
+
values.sort(comparator);
|
|
687
|
+
yield* values;
|
|
688
|
+
});
|
|
689
|
+
}
|
|
690
|
+
/** Consumes a cleanup publisher and ignores all emitted values. */
|
|
691
|
+
async function drainPublisher(source, signal, context) {
|
|
692
|
+
for await (const _ of Flux.from(source).iterate(signal, context));
|
|
693
|
+
}
|
|
694
|
+
//#endregion
|
|
695
|
+
export { Flux };
|
|
696
|
+
|
|
697
|
+
//# sourceMappingURL=flux.js.map
|