@okikio/observables 1.0.2 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +117 -6
- package/esm/helpers/_types.d.ts +62 -0
- package/esm/helpers/_types.d.ts.map +1 -1
- package/esm/helpers/operations/combination.d.ts +112 -3
- package/esm/helpers/operations/combination.d.ts.map +1 -1
- package/esm/helpers/operations/combination.js +682 -115
- package/esm/helpers/operations/conditional.d.ts +77 -1
- package/esm/helpers/operations/conditional.d.ts.map +1 -1
- package/esm/helpers/operations/conditional.js +139 -0
- package/esm/helpers/operators.d.ts +57 -0
- package/esm/helpers/operators.d.ts.map +1 -1
- package/esm/helpers/operators.js +139 -4
- package/esm/helpers/utils.d.ts +73 -1
- package/esm/helpers/utils.d.ts.map +1 -1
- package/esm/helpers/utils.js +168 -0
- package/package.json +1 -1
- package/script/helpers/_types.d.ts +62 -0
- package/script/helpers/_types.d.ts.map +1 -1
- package/script/helpers/operations/combination.d.ts +112 -3
- package/script/helpers/operations/combination.d.ts.map +1 -1
- package/script/helpers/operations/combination.js +686 -115
- package/script/helpers/operations/conditional.d.ts +77 -1
- package/script/helpers/operations/conditional.d.ts.map +1 -1
- package/script/helpers/operations/conditional.js +143 -0
- package/script/helpers/operators.d.ts +57 -0
- package/script/helpers/operators.d.ts.map +1 -1
- package/script/helpers/operators.js +141 -4
- package/script/helpers/utils.d.ts +73 -1
- package/script/helpers/utils.d.ts.map +1 -1
- package/script/helpers/utils.js +172 -0
package/esm/helpers/utils.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ObservableError } from "../error.js";
|
|
2
|
+
import { Observable } from "../observable.js";
|
|
2
3
|
/**
|
|
3
4
|
* Type guard to check if options is a TransformStreamOptions
|
|
4
5
|
*
|
|
@@ -118,6 +119,173 @@ export function toStream(iterable) {
|
|
|
118
119
|
},
|
|
119
120
|
});
|
|
120
121
|
}
|
|
122
|
+
/**
|
|
123
|
+
* Adapts a readable/writable stream pair into an Observable operator.
|
|
124
|
+
*
|
|
125
|
+
* Some platform transforms, such as `CompressionStream`, expose a writable side
|
|
126
|
+
* and a readable side without being created through this library's operator
|
|
127
|
+
* builders. This helper turns that pair into a normal `Operator` so it can be
|
|
128
|
+
* used inside `pipe()` like any built-in operator.
|
|
129
|
+
*
|
|
130
|
+
* A fresh pair is created for each operator application. That preserves the
|
|
131
|
+
* cold semantics of the surrounding Observable pipeline and avoids reusing a
|
|
132
|
+
* consumed stream pair across subscriptions.
|
|
133
|
+
*
|
|
134
|
+
* @typeParam TIn - Chunk type written into the pair
|
|
135
|
+
* @typeParam TOut - Chunk type read from the pair
|
|
136
|
+
* @param createPair - Factory that returns a fresh readable/writable pair
|
|
137
|
+
* @returns An operator that pipes input through the created pair
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```ts
|
|
141
|
+
* const gzip = fromStreamPair<Uint8Array, Uint8Array>(
|
|
142
|
+
* () => new CompressionStream('gzip')
|
|
143
|
+
* );
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
export function fromStreamPair(createPair) {
|
|
147
|
+
return (source) => source.pipeThrough(createPair());
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Wraps a `ReadableStream` as an Observable so foreign Observable-style
|
|
151
|
+
* operators can subscribe to it directly.
|
|
152
|
+
*
|
|
153
|
+
* This avoids first converting the stream into an async-iterable Observable via
|
|
154
|
+
* `Observable.from()`, which would add an extra adaptation layer before the
|
|
155
|
+
* foreign operator even starts running.
|
|
156
|
+
*/
|
|
157
|
+
export function streamAsObservable(stream) {
|
|
158
|
+
return new Observable((observer) => {
|
|
159
|
+
const reader = stream.getReader();
|
|
160
|
+
let cancelled = false;
|
|
161
|
+
void (async () => {
|
|
162
|
+
try {
|
|
163
|
+
while (!cancelled && !observer.closed) {
|
|
164
|
+
const { done, value } = await reader.read();
|
|
165
|
+
if (done) {
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
observer.next(value);
|
|
169
|
+
}
|
|
170
|
+
if (!cancelled && !observer.closed) {
|
|
171
|
+
observer.complete();
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
catch (err) {
|
|
175
|
+
if (!cancelled && !observer.closed) {
|
|
176
|
+
observer.error(err);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
finally {
|
|
180
|
+
try {
|
|
181
|
+
reader.releaseLock();
|
|
182
|
+
}
|
|
183
|
+
catch {
|
|
184
|
+
// Releasing the reader is best-effort during teardown.
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
})();
|
|
188
|
+
return () => {
|
|
189
|
+
cancelled = true;
|
|
190
|
+
void reader.cancel();
|
|
191
|
+
};
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Returns true when a value exposes a direct `subscribe()` method.
|
|
196
|
+
*
|
|
197
|
+
* Many Observable libraries return subscribable objects that are usable without
|
|
198
|
+
* first going through this library's `Observable.from()`. Detecting that shape
|
|
199
|
+
* lets interop stay direct for outputs such as RxJS Observables.
|
|
200
|
+
*/
|
|
201
|
+
function hasSubscribe(input) {
|
|
202
|
+
return typeof input === "object" && input !== null &&
|
|
203
|
+
typeof input.subscribe === "function";
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Subscribes to an Observable-like output and exposes it as a ReadableStream.
|
|
207
|
+
*
|
|
208
|
+
* Foreign operators are allowed to return either a normal `Observable.from()`
|
|
209
|
+
* input or a direct subscribable from another Observable implementation.
|
|
210
|
+
* Converting the result with a direct subscription avoids the extra
|
|
211
|
+
* async-generator and stream layers that `pull(...)+toStream()` would add.
|
|
212
|
+
*/
|
|
213
|
+
export function observableInputToStream(input, errorContext) {
|
|
214
|
+
let subscription;
|
|
215
|
+
return new ReadableStream({
|
|
216
|
+
start(controller) {
|
|
217
|
+
const source = hasSubscribe(input) ? input : Observable.from(input);
|
|
218
|
+
subscription = source.subscribe({
|
|
219
|
+
next(value) {
|
|
220
|
+
try {
|
|
221
|
+
controller.enqueue(value);
|
|
222
|
+
}
|
|
223
|
+
catch {
|
|
224
|
+
// Downstream cancellation already decided the stream outcome.
|
|
225
|
+
}
|
|
226
|
+
},
|
|
227
|
+
error(error) {
|
|
228
|
+
try {
|
|
229
|
+
controller.enqueue(ObservableError.from(error, errorContext));
|
|
230
|
+
controller.close();
|
|
231
|
+
}
|
|
232
|
+
catch {
|
|
233
|
+
// Downstream cancellation already decided the stream outcome.
|
|
234
|
+
}
|
|
235
|
+
},
|
|
236
|
+
complete() {
|
|
237
|
+
try {
|
|
238
|
+
controller.close();
|
|
239
|
+
}
|
|
240
|
+
catch {
|
|
241
|
+
// Downstream cancellation already decided the stream outcome.
|
|
242
|
+
}
|
|
243
|
+
},
|
|
244
|
+
});
|
|
245
|
+
},
|
|
246
|
+
cancel() {
|
|
247
|
+
subscription?.unsubscribe?.();
|
|
248
|
+
subscription = undefined;
|
|
249
|
+
},
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Adapts a foreign Observable-style operator into a stream operator.
|
|
254
|
+
*
|
|
255
|
+
* Libraries such as RxJS model operators as functions from one Observable-like
|
|
256
|
+
* source to another Observable-like result. This helper bridges that shape into
|
|
257
|
+
* this library's `Operator` contract by:
|
|
258
|
+
*
|
|
259
|
+
* 1. wrapping the input stream as an Observable-like source
|
|
260
|
+
* 2. calling the foreign operator
|
|
261
|
+
* 3. converting the resulting Observable-like output back into a stream
|
|
262
|
+
*
|
|
263
|
+
* The result keeps this library's buffered error behavior by converting the
|
|
264
|
+
* foreign output into a `ReadableStream` that enqueues wrapped
|
|
265
|
+
* `ObservableError` values instead of failing the readable side outright.
|
|
266
|
+
*
|
|
267
|
+
* @typeParam TIn - Value type accepted by the foreign operator
|
|
268
|
+
* @typeParam TOut - Value type produced by the foreign operator
|
|
269
|
+
* @param operator - Foreign operator function to adapt
|
|
270
|
+
* @returns An operator compatible with this library's `pipe()`
|
|
271
|
+
*
|
|
272
|
+
* @example
|
|
273
|
+
* ```ts
|
|
274
|
+
* const foreignTakeOne = fromObservableOperator<number, number>((source) =>
|
|
275
|
+
* rxTake(1)(source)
|
|
276
|
+
* , { sourceAdapter: (source) => rxFrom(source) });
|
|
277
|
+
* ```
|
|
278
|
+
*/
|
|
279
|
+
export function fromObservableOperator(operator, options) {
|
|
280
|
+
return (source) => {
|
|
281
|
+
const observableSource = streamAsObservable(source);
|
|
282
|
+
const foreignSource = options?.sourceAdapter
|
|
283
|
+
? options.sourceAdapter(observableSource)
|
|
284
|
+
: observableSource;
|
|
285
|
+
const output = operator(foreignSource);
|
|
286
|
+
return observableInputToStream(output, options?.errorContext ?? "operator:fromObservableOperator:output");
|
|
287
|
+
};
|
|
288
|
+
}
|
|
121
289
|
/**
|
|
122
290
|
* Creates a TransformStream that injects an error at the start and passes through all original data.
|
|
123
291
|
*
|
package/package.json
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ObservableError } from "../error.js";
|
|
2
2
|
import type { Observable } from "../observable.js";
|
|
3
|
+
import type { SpecObservable } from "../_spec.js";
|
|
3
4
|
/**
|
|
4
5
|
* Type representing a stream operator function
|
|
5
6
|
* Transforms a ReadableStream of type In to a ReadableStream of type Out
|
|
@@ -67,6 +68,67 @@ export interface BaseTransformOptions {
|
|
|
67
68
|
*/
|
|
68
69
|
name?: string;
|
|
69
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* Structural readable/writable pair used by platform transforms such as
|
|
73
|
+
* `CompressionStream`.
|
|
74
|
+
*/
|
|
75
|
+
export interface StreamPair<TIn, TOut> {
|
|
76
|
+
/** Readable side exposed by the transform-like object. */
|
|
77
|
+
readable: ReadableStream<TOut>;
|
|
78
|
+
/** Writable side exposed by the transform-like object. */
|
|
79
|
+
writable: WritableStream<TIn>;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Minimal subscribable shape used by foreign Observable implementations.
|
|
83
|
+
*/
|
|
84
|
+
export interface SubscribableLike<T> {
|
|
85
|
+
/** Subscribe to values from the foreign Observable-like object. */
|
|
86
|
+
subscribe(observer: {
|
|
87
|
+
next?(value: T): void;
|
|
88
|
+
error?(error: unknown): void;
|
|
89
|
+
complete?(): void;
|
|
90
|
+
}): {
|
|
91
|
+
unsubscribe?(): void;
|
|
92
|
+
} | void;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Observable-like values that can be converted through `Observable.from()`.
|
|
96
|
+
*/
|
|
97
|
+
export type ObservableInputLike<T> = SpecObservable<T> | AsyncIterable<T> | Iterable<T> | PromiseLike<T>;
|
|
98
|
+
/**
|
|
99
|
+
* Wider Observable-like values accepted by interop helpers that adapt foreign
|
|
100
|
+
* operator ecosystems.
|
|
101
|
+
*
|
|
102
|
+
* This is intentionally wider than `ObservableInputLike<T>`. `Observable.from()`
|
|
103
|
+
* stays aligned with the library's spec-facing conversion contract, while
|
|
104
|
+
* interop helpers may also need to consume direct subscribables returned by
|
|
105
|
+
* third-party libraries such as RxJS.
|
|
106
|
+
*/
|
|
107
|
+
export type ObservableInteropInputLike<T> = ObservableInputLike<T> | SubscribableLike<T>;
|
|
108
|
+
/**
|
|
109
|
+
* Foreign operator shape used by libraries that transform one Observable-like
|
|
110
|
+
* source into another Observable-like result.
|
|
111
|
+
*/
|
|
112
|
+
export type ObservableOperatorInterop<TIn, TOut, TSource = Observable<TIn>> = (source: TSource) => ObservableInteropInputLike<TOut>;
|
|
113
|
+
/**
|
|
114
|
+
* Configuration for adapting foreign Observable-style operators.
|
|
115
|
+
*
|
|
116
|
+
* Some libraries, such as RxJS, require their own Observable class on the
|
|
117
|
+
* input side even when the returned operator shape is still `(source) =>
|
|
118
|
+
* output`. `sourceAdapter` lets callers convert this library's Observable into
|
|
119
|
+
* that foreign source type before the operator runs.
|
|
120
|
+
*/
|
|
121
|
+
export interface ObservableOperatorInteropOptions<TIn, TSource = Observable<TIn>> {
|
|
122
|
+
/**
|
|
123
|
+
* Converts this library's Observable into the source type expected by the
|
|
124
|
+
* foreign operator.
|
|
125
|
+
*/
|
|
126
|
+
sourceAdapter?: (source: Observable<TIn>) => TSource;
|
|
127
|
+
/**
|
|
128
|
+
* Context label used when the foreign output reports an error.
|
|
129
|
+
*/
|
|
130
|
+
errorContext?: string;
|
|
131
|
+
}
|
|
70
132
|
/**
|
|
71
133
|
* Options for using an existing TransformStream
|
|
72
134
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_types.d.ts","sourceRoot":"","sources":["../../src/helpers/_types.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"_types.d.ts","sourceRoot":"","sources":["../../src/helpers/_types.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAElD;;;GAGG;AACH,MAAM,MAAM,QAAQ,CAAC,EAAE,EAAE,GAAG,IAAI,CAC9B,MAAM,EAAE,cAAc,CAAC,EAAE,CAAC,KACvB,cAAc,CAAC,GAAG,CAAC,CAAC;AAEzB;;;GAGG;AACH,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,EAAE,eAAe,CAAC,CAAC;AAE1D;;;GAGG;AACH,MAAM,MAAM,YAAY,CAAC,CAAC,EAAE,CAAC,IAAI,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC;AAE/E;;;GAGG;AACH,MAAM,MAAM,eAAe,CAAC,OAAO,SAAS,OAAO,IAAI,OAAO,SAC5D,UAAU,CAAC,O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t.polyfills.js";
|
|
|
18
18
|
import type { SpecObservable } from "../../_spec.js";
|
|
19
19
|
import type { ExcludeError, Operator } from "../_types.js";
|
|
20
20
|
import { ObservableError } from "../../error.js";
|
|
21
|
+
/**
|
|
22
|
+
* Creates a follow-up Observable for each source value.
|
|
23
|
+
*
|
|
24
|
+
* These higher-order operators all work in two stages:
|
|
25
|
+
* they read an outer source value, then use this callback to start an inner
|
|
26
|
+
* Observable whose values are forwarded according to the operator's policy.
|
|
27
|
+
*
|
|
28
|
+
* ```text
|
|
29
|
+
* outer value ---> createFollowUp(value, index) ---> inner Observable
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* The operators in this module differ in what they do after that point:
|
|
33
|
+
*
|
|
34
|
+
* ```text
|
|
35
|
+
* mergeMap -> keep many inners running at once
|
|
36
|
+
* concatMap -> queue inners and run one at a time
|
|
37
|
+
* switchMap -> cancel the old inner and keep only the latest one
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @typeParam T - Value type from the outer source
|
|
41
|
+
* @typeParam R - Value type emitted by each follow-up Observable
|
|
42
|
+
*/
|
|
43
|
+
export type FollowUpProject<T, R> = (value: ExcludeError<T>, index: number) => SpecObservable<R>;
|
|
44
|
+
/**
|
|
45
|
+
* Tuple of values emitted by a source and one or more companion Observables.
|
|
46
|
+
*
|
|
47
|
+
* Operators such as `withLatestFrom`, `combineLatestWith`, and `zipWith`
|
|
48
|
+
* return tuples so callers can decide how to shape the combined values in a
|
|
49
|
+
* later `map()` step without losing type information.
|
|
50
|
+
*
|
|
51
|
+
* @typeParam T - Value type from the primary source
|
|
52
|
+
* @typeParam TOthers - Value types from the companion Observables
|
|
53
|
+
*/
|
|
54
|
+
export type CombinationTuple<T, TOthers extends readonly unknown[]> = [
|
|
55
|
+
T,
|
|
56
|
+
...TOthers
|
|
57
|
+
];
|
|
58
|
+
/**
|
|
59
|
+
* Tuple-shaped list of companion Observables used by the combination helpers.
|
|
60
|
+
*
|
|
61
|
+
* @typeParam TValues - Value types emitted by each companion Observable
|
|
62
|
+
*/
|
|
63
|
+
export type CombinationSources<TValues extends readonly unknown[]> = {
|
|
64
|
+
[K in keyof TValues]: SpecObservable<TValues[K]>;
|
|
65
|
+
};
|
|
21
66
|
/**
|
|
22
67
|
* Transforms each item into a new stream and merges their outputs, running
|
|
23
68
|
* them in parallel.
|
|
@@ -63,7 +108,7 @@ import { ObservableError } from "../../error.js";
|
|
|
63
108
|
* to concurrently. Default is Infinity.
|
|
64
109
|
* @returns An operator function that maps and flattens values
|
|
65
110
|
*/
|
|
66
|
-
export declare function mergeMap<T, R>(project:
|
|
111
|
+
export declare function mergeMap<T, R>(project: FollowUpProject<T, R>, concurrent?: number): Operator<T | ObservableError, R | ObservableError>;
|
|
67
112
|
/**
|
|
68
113
|
* Transforms each item into a new stream and runs them one after another, in
|
|
69
114
|
* strict order.
|
|
@@ -113,7 +158,7 @@ export declare function mergeMap<T, R>(project: (value: ExcludeError<T>, index:
|
|
|
113
158
|
* @param project - Function that maps a source value to an Observable
|
|
114
159
|
* @returns An operator function that maps and concatenates values
|
|
115
160
|
*/
|
|
116
|
-
export declare function concatMap<T, R>(project:
|
|
161
|
+
export declare function concatMap<T, R>(project: FollowUpProject<T, R>): Operator<T | ObservableError, R | ObservableError>;
|
|
117
162
|
/**
|
|
118
163
|
* Transforms items into new streams, but cancels the previous stream when a new
|
|
119
164
|
* item arrives.
|
|
@@ -158,5 +203,69 @@ export declare function concatMap<T, R>(project: (value: ExcludeError<T>, index:
|
|
|
158
203
|
* @param project - Function that maps a source value to an Observable
|
|
159
204
|
* @returns An operator function that maps and switches between values
|
|
160
205
|
*/
|
|
161
|
-
export declare function switchMap<T, R>(project:
|
|
206
|
+
export declare function switchMap<T, R>(project: FollowUpProject<T, R>): Operator<T | ObservableError, R | ObservableError>;
|
|
207
|
+
/**
|
|
208
|
+
* Emits each source value together with the latest value from every companion
|
|
209
|
+
* Observable.
|
|
210
|
+
*
|
|
211
|
+
* This operator waits until every companion Observable has produced at least
|
|
212
|
+
* one value. After that gate opens, each new source value emits a tuple that
|
|
213
|
+
* includes the source value followed by the most recent companion values.
|
|
214
|
+
*
|
|
215
|
+
* ```text
|
|
216
|
+
* companion A: ---a----b-------c--
|
|
217
|
+
* companion B: -----x-------y-----
|
|
218
|
+
* source: ------1--2------3--
|
|
219
|
+
* output: ------[1,a,x][2,b,x][3,c,y]
|
|
220
|
+
* ```
|
|
221
|
+
*
|
|
222
|
+
* The source still decides when outputs happen. Companion Observables only
|
|
223
|
+
* update the remembered latest values.
|
|
224
|
+
*
|
|
225
|
+
* @typeParam T - Value type from the primary source
|
|
226
|
+
* @typeParam TOthers - Value types from the companion Observables
|
|
227
|
+
* @param others - Companion Observables whose latest values should be attached
|
|
228
|
+
* @returns An operator that emits tuples of the source value and latest others
|
|
229
|
+
*/
|
|
230
|
+
export declare function withLatestFrom<T, TOthers extends readonly unknown[]>(...others: CombinationSources<TOthers>): Operator<T | ObservableError, CombinationTuple<ExcludeError<T>, TOthers> | ObservableError>;
|
|
231
|
+
/**
|
|
232
|
+
* Combines the source with companion Observables using combine-latest timing.
|
|
233
|
+
*
|
|
234
|
+
* Once every source has emitted at least one value, any new value from the
|
|
235
|
+
* primary source or a companion Observable emits a new tuple containing the
|
|
236
|
+
* latest value from each source.
|
|
237
|
+
*
|
|
238
|
+
* @typeParam T - Value type from the primary source
|
|
239
|
+
* @typeParam TOthers - Value types from the companion Observables
|
|
240
|
+
* @param others - Companion Observables that participate in the latest-value set
|
|
241
|
+
* @returns An operator that emits tuples of the latest value from every source
|
|
242
|
+
*/
|
|
243
|
+
export declare function combineLatestWith<T, TOthers extends readonly unknown[]>(...others: CombinationSources<TOthers>): Operator<T | ObservableError, CombinationTuple<ExcludeError<T>, TOthers> | ObservableError>;
|
|
244
|
+
/**
|
|
245
|
+
* Pairs source values with companion values in strict arrival order.
|
|
246
|
+
*
|
|
247
|
+
* `zipWith` waits until it has one value from the source and one value from
|
|
248
|
+
* each companion. It then emits a tuple and removes those values from the
|
|
249
|
+
* queues before waiting for the next full set.
|
|
250
|
+
*
|
|
251
|
+
* @typeParam T - Value type from the primary source
|
|
252
|
+
* @typeParam TOthers - Value types from the companion Observables
|
|
253
|
+
* @param others - Companion Observables to align by arrival order
|
|
254
|
+
* @returns An operator that emits tuples aligned by position instead of time
|
|
255
|
+
*/
|
|
256
|
+
export declare function zipWith<T, TOthers extends readonly unknown[]>(...others: CombinationSources<TOthers>): Operator<T | ObservableError, CombinationTuple<ExcludeError<T>, TOthers> | ObservableError>;
|
|
257
|
+
/**
|
|
258
|
+
* Mirrors whichever Observable emits first: the source or one of the
|
|
259
|
+
* companions.
|
|
260
|
+
*
|
|
261
|
+
* If the primary source wins, future source values keep flowing and companion
|
|
262
|
+
* Observables are cancelled. If a companion wins, the source is ignored from
|
|
263
|
+
* that point on and the winning companion continues to drive the output.
|
|
264
|
+
*
|
|
265
|
+
* @typeParam T - Value type from the primary source
|
|
266
|
+
* @typeParam TOthers - Value types from the companion Observables
|
|
267
|
+
* @param others - Companion Observables racing against the source
|
|
268
|
+
* @returns An operator that mirrors the winning Observable
|
|
269
|
+
*/
|
|
270
|
+
export declare function raceWith<T, TOthers extends readonly unknown[]>(...others: CombinationSources<TOthers>): Operator<T | ObservableError, ExcludeError<T> | TOthers[number] | ObservableError>;
|
|
162
271
|
//# sourceMappingURL=combination.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"combination.d.ts","sourceRoot":"","sources":["../../../src/helpers/operations/combination.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,yBAAyB,CAAC;AAGjC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,KAAK,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"combination.d.ts","sourceRoot":"","sources":["../../../src/helpers/operations/combination.ts"],"names":[],"mappings":"AAAA;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