Debian Codegraph @

node/rxjs/src/internal/observable/race.ts

raw
1 import { Observable } from '../Observable';
2 import { innerFrom } from './innerFrom';
3 import { Subscription } from '../Subscription';
4 import { ObservableInput, ObservableInputTuple } from '../types';
5 import { argsOrArgArray } from '../util/argsOrArgArray';
6 import { createOperatorSubscriber } from '../operators/OperatorSubscriber';
7 import { Subscriber } from '../Subscriber';
8
9 export function race<T extends readonly unknown[]>(inputs: [...ObservableInputTuple<T>]): Observable<T[number]>;
10 export function race<T extends readonly unknown[]>(...inputs: [...ObservableInputTuple<T>]): Observable<T[number]>;
11
12 /**
13 * Returns an observable that mirrors the first source observable to emit an item.
14 *
15 * ![](race.png)
16 *
17 * `race` returns an observable, that when subscribed to, subscribes to all source observables immediately.
18 * As soon as one of the source observables emits a value, the result unsubscribes from the other sources.
19 * The resulting observable will forward all notifications, including error and completion, from the "winning"
20 * source observable.
21 *
22 * If one of the used source observable throws an errors before a first notification
23 * the race operator will also throw an error, no matter if another source observable
24 * could potentially win the race.
25 *
26 * `race` can be useful for selecting the response from the fastest network connection for
27 * HTTP or WebSockets. `race` can also be useful for switching observable context based on user
28 * input.
29 *
30 * ## Example
31 *
32 * Subscribes to the observable that was the first to start emitting.
33 *
34 * ```ts
35 * import { interval, map, race } from 'rxjs';
36 *
37 * const obs1 = interval(7000).pipe(map(() => 'slow one'));
38 * const obs2 = interval(3000).pipe(map(() => 'fast one'));
39 * const obs3 = interval(5000).pipe(map(() => 'medium one'));
40 *
41 * race(obs1, obs2, obs3)
42 * .subscribe(winner => console.log(winner));
43 *
44 * // Outputs
45 * // a series of 'fast one'
46 * ```
47 *
48 * @param sources Used to race for which `ObservableInput` emits first.
49 * @return An Observable that mirrors the output of the first Observable to emit an item.
50 */
51 export function race<T>(...sources: (ObservableInput<T> | ObservableInput<T>[])[]): Observable<any> {
52 sources = argsOrArgArray(sources);
53 // If only one source was passed, just return it. Otherwise return the race.
54 return sources.length === 1 ? innerFrom(sources[0] as ObservableInput<T>) : new Observable<T>(raceInit(sources as ObservableInput<T>[]));
55 }
56
57 /**
58 * An observable initializer function for both the static version and the
59 * operator version of race.
60 * @param sources The sources to race
61 */
62 export function raceInit<T>(sources: ObservableInput<T>[]) {
63 return (subscriber: Subscriber<T>) => {
64 let subscriptions: Subscription[] = [];
65
66 // Subscribe to all of the sources. Note that we are checking `subscriptions` here
67 // Is is an array of all actively "racing" subscriptions, and it is `null` after the
68 // race has been won. So, if we have racer that synchronously "wins", this loop will
69 // stop before it subscribes to any more.
70 for (let i = 0; subscriptions && !subscriber.closed && i < sources.length; i++) {
71 subscriptions.push(
72 innerFrom(sources[i] as ObservableInput<T>).subscribe(
73 createOperatorSubscriber(subscriber, (value) => {
74 if (subscriptions) {
75 // We're still racing, but we won! So unsubscribe
76 // all other subscriptions that we have, except this one.
77 for (let s = 0; s < subscriptions.length; s++) {
78 s !== i && subscriptions[s].unsubscribe();
79 }
80 subscriptions = null!;
81 }
82 subscriber.next(value);
83 })
84 )
85 );
86 }
87 };
88 }