Debian Codegraph @

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

raw
1 import { Observable } from '../Observable';
2 import { ObservableInputTuple, SchedulerLike } from '../types';
3 import { concatAll } from '../operators/concatAll';
4 import { popScheduler } from '../util/args';
5 import { from } from './from';
6
7 export function concat<T extends readonly unknown[]>(...inputs: [...ObservableInputTuple<T>]): Observable<T[number]>;
8 export function concat<T extends readonly unknown[]>(
9 ...inputsAndScheduler: [...ObservableInputTuple<T>, SchedulerLike]
10 ): Observable<T[number]>;
11
12 /**
13 * Creates an output Observable which sequentially emits all values from the first given
14 * Observable and then moves on to the next.
15 *
16 * <span class="informal">Concatenates multiple Observables together by
17 * sequentially emitting their values, one Observable after the other.</span>
18 *
19 * ![](concat.png)
20 *
21 * `concat` joins multiple Observables together, by subscribing to them one at a time and
22 * merging their results into the output Observable. You can pass either an array of
23 * Observables, or put them directly as arguments. Passing an empty array will result
24 * in Observable that completes immediately.
25 *
26 * `concat` will subscribe to first input Observable and emit all its values, without
27 * changing or affecting them in any way. When that Observable completes, it will
28 * subscribe to then next Observable passed and, again, emit its values. This will be
29 * repeated, until the operator runs out of Observables. When last input Observable completes,
30 * `concat` will complete as well. At any given moment only one Observable passed to operator
31 * emits values. If you would like to emit values from passed Observables concurrently, check out
32 * {@link merge} instead, especially with optional `concurrent` parameter. As a matter of fact,
33 * `concat` is an equivalent of `merge` operator with `concurrent` parameter set to `1`.
34 *
35 * Note that if some input Observable never completes, `concat` will also never complete
36 * and Observables following the one that did not complete will never be subscribed. On the other
37 * hand, if some Observable simply completes immediately after it is subscribed, it will be
38 * invisible for `concat`, which will just move on to the next Observable.
39 *
40 * If any Observable in chain errors, instead of passing control to the next Observable,
41 * `concat` will error immediately as well. Observables that would be subscribed after
42 * the one that emitted error, never will.
43 *
44 * If you pass to `concat` the same Observable many times, its stream of values
45 * will be "replayed" on every subscription, which means you can repeat given Observable
46 * as many times as you like. If passing the same Observable to `concat` 1000 times becomes tedious,
47 * you can always use {@link repeat}.
48 *
49 * ## Examples
50 *
51 * Concatenate a timer counting from 0 to 3 with a synchronous sequence from 1 to 10
52 *
53 * ```ts
54 * import { interval, take, range, concat } from 'rxjs';
55 *
56 * const timer = interval(1000).pipe(take(4));
57 * const sequence = range(1, 10);
58 * const result = concat(timer, sequence);
59 * result.subscribe(x => console.log(x));
60 *
61 * // results in:
62 * // 0 -1000ms-> 1 -1000ms-> 2 -1000ms-> 3 -immediate-> 1 ... 10
63 * ```
64 *
65 * Concatenate 3 Observables
66 *
67 * ```ts
68 * import { interval, take, concat } from 'rxjs';
69 *
70 * const timer1 = interval(1000).pipe(take(10));
71 * const timer2 = interval(2000).pipe(take(6));
72 * const timer3 = interval(500).pipe(take(10));
73 *
74 * const result = concat(timer1, timer2, timer3);
75 * result.subscribe(x => console.log(x));
76 *
77 * // results in the following:
78 * // (Prints to console sequentially)
79 * // -1000ms-> 0 -1000ms-> 1 -1000ms-> ... 9
80 * // -2000ms-> 0 -2000ms-> 1 -2000ms-> ... 5
81 * // -500ms-> 0 -500ms-> 1 -500ms-> ... 9
82 * ```
83 *
84 * Concatenate the same Observable to repeat it
85 *
86 * ```ts
87 * import { interval, take, concat } from 'rxjs';
88 *
89 * const timer = interval(1000).pipe(take(2));
90 *
91 * concat(timer, timer) // concatenating the same Observable!
92 * .subscribe({
93 * next: value => console.log(value),
94 * complete: () => console.log('...and it is done!')
95 * });
96 *
97 * // Logs:
98 * // 0 after 1s
99 * // 1 after 2s
100 * // 0 after 3s
101 * // 1 after 4s
102 * // '...and it is done!' also after 4s
103 * ```
104 *
105 * @see {@link concatAll}
106 * @see {@link concatMap}
107 * @see {@link concatMapTo}
108 * @see {@link startWith}
109 * @see {@link endWith}
110 *
111 * @param args `ObservableInput`s to concatenate.
112 */
113 export function concat(...args: any[]): Observable<unknown> {
114 return concatAll()(from(args, popScheduler(args)));
115 }