Debian Codegraph @

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

raw
1 /** prettier */
2 import { Observable } from '../Observable';
3 import { concat } from '../observable/concat';
4 import { of } from '../observable/of';
5 import { MonoTypeOperatorFunction, SchedulerLike, OperatorFunction, ValueFromArray } from '../types';
6
7 /** @deprecated The `scheduler` parameter will be removed in v8. Use `scheduled` and `concatAll`. Details: https://rxjs.dev/deprecations/scheduler-argument */
8 export function endWith<T>(scheduler: SchedulerLike): MonoTypeOperatorFunction<T>;
9 /** @deprecated The `scheduler` parameter will be removed in v8. Use `scheduled` and `concatAll`. Details: https://rxjs.dev/deprecations/scheduler-argument */
10 export function endWith<T, A extends unknown[] = T[]>(
11 ...valuesAndScheduler: [...A, SchedulerLike]
12 ): OperatorFunction<T, T | ValueFromArray<A>>;
13
14 export function endWith<T, A extends unknown[] = T[]>(...values: A): OperatorFunction<T, T | ValueFromArray<A>>;
15
16 /**
17 * Returns an observable that will emit all values from the source, then synchronously emit
18 * the provided value(s) immediately after the source completes.
19 *
20 * NOTE: Passing a last argument of a Scheduler is _deprecated_, and may result in incorrect
21 * types in TypeScript.
22 *
23 * This is useful for knowing when an observable ends. Particularly when paired with an
24 * operator like {@link takeUntil}
25 *
26 * ![](endWith.png)
27 *
28 * ## Example
29 *
30 * Emit values to know when an interval starts and stops. The interval will
31 * stop when a user clicks anywhere on the document.
32 *
33 * ```ts
34 * import { interval, map, fromEvent, startWith, takeUntil, endWith } from 'rxjs';
35 *
36 * const ticker$ = interval(5000).pipe(
37 * map(() => 'tick')
38 * );
39 *
40 * const documentClicks$ = fromEvent(document, 'click');
41 *
42 * ticker$.pipe(
43 * startWith('interval started'),
44 * takeUntil(documentClicks$),
45 * endWith('interval ended by click')
46 * )
47 * .subscribe(x => console.log(x));
48 *
49 * // Result (assuming a user clicks after 15 seconds)
50 * // 'interval started'
51 * // 'tick'
52 * // 'tick'
53 * // 'tick'
54 * // 'interval ended by click'
55 * ```
56 *
57 * @see {@link startWith}
58 * @see {@link concat}
59 * @see {@link takeUntil}
60 *
61 * @param values Items you want the modified Observable to emit last.
62 * @return A function that returns an Observable that emits all values from the
63 * source, then synchronously emits the provided value(s) immediately after the
64 * source completes.
65 */
66 export function endWith<T>(...values: Array<T | SchedulerLike>): MonoTypeOperatorFunction<T> {
67 return (source: Observable<T>) => concat(source, of(...values)) as Observable<T>;
68 }