Debian Codegraph @

node/rxjs/src/internal/operators/raceWith.ts

raw
1 import { OperatorFunction, ObservableInputTuple } from '../types';
2 import { raceInit } from '../observable/race';
3 import { operate } from '../util/lift';
4 import { identity } from '../util/identity';
5
6 /**
7 * Creates an Observable that mirrors the first source Observable to emit a next,
8 * error or complete notification from the combination of the Observable to which
9 * the operator is applied and supplied Observables.
10 *
11 * ## Example
12 *
13 * ```ts
14 * import { interval, map, raceWith } from 'rxjs';
15 *
16 * const obs1 = interval(7000).pipe(map(() => 'slow one'));
17 * const obs2 = interval(3000).pipe(map(() => 'fast one'));
18 * const obs3 = interval(5000).pipe(map(() => 'medium one'));
19 *
20 * obs1
21 * .pipe(raceWith(obs2, obs3))
22 * .subscribe(winner => console.log(winner));
23 *
24 * // Outputs
25 * // a series of 'fast one'
26 * ```
27 *
28 * @param otherSources Sources used to race for which Observable emits first.
29 * @return A function that returns an Observable that mirrors the output of the
30 * first Observable to emit an item.
31 */
32 export function raceWith<T, A extends readonly unknown[]>(
33 ...otherSources: [...ObservableInputTuple<A>]
34 ): OperatorFunction<T, T | A[number]> {
35 return !otherSources.length
36 ? identity
37 : operate((source, subscriber) => {
38 raceInit<T | A[number]>([source, ...otherSources])(subscriber);
39 });
40 }