Debian Codegraph @

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

raw
1 import { Observable } from '../Observable';
2 import { SchedulerLike } from '../types';
3
4 /**
5 * A simple Observable that emits no items to the Observer and immediately
6 * emits a complete notification.
7 *
8 * <span class="informal">Just emits 'complete', and nothing else.</span>
9 *
10 * ![](empty.png)
11 *
12 * A simple Observable that only emits the complete notification. It can be used
13 * for composing with other Observables, such as in a {@link mergeMap}.
14 *
15 * ## Examples
16 *
17 * Log complete notification
18 *
19 * ```ts
20 * import { EMPTY } from 'rxjs';
21 *
22 * EMPTY.subscribe({
23 * next: () => console.log('Next'),
24 * complete: () => console.log('Complete!')
25 * });
26 *
27 * // Outputs
28 * // Complete!
29 * ```
30 *
31 * Emit the number 7, then complete
32 *
33 * ```ts
34 * import { EMPTY, startWith } from 'rxjs';
35 *
36 * const result = EMPTY.pipe(startWith(7));
37 * result.subscribe(x => console.log(x));
38 *
39 * // Outputs
40 * // 7
41 * ```
42 *
43 * Map and flatten only odd numbers to the sequence `'a'`, `'b'`, `'c'`
44 *
45 * ```ts
46 * import { interval, mergeMap, of, EMPTY } from 'rxjs';
47 *
48 * const interval$ = interval(1000);
49 * const result = interval$.pipe(
50 * mergeMap(x => x % 2 === 1 ? of('a', 'b', 'c') : EMPTY),
51 * );
52 * result.subscribe(x => console.log(x));
53 *
54 * // Results in the following to the console:
55 * // x is equal to the count on the interval, e.g. (0, 1, 2, 3, ...)
56 * // x will occur every 1000ms
57 * // if x % 2 is equal to 1, print a, b, c (each on its own)
58 * // if x % 2 is not equal to 1, nothing will be output
59 * ```
60 *
61 * @see {@link Observable}
62 * @see {@link NEVER}
63 * @see {@link of}
64 * @see {@link throwError}
65 */
66 export const EMPTY = new Observable<never>((subscriber) => subscriber.complete());
67
68 /**
69 * @param scheduler A {@link SchedulerLike} to use for scheduling
70 * the emission of the complete notification.
71 * @deprecated Replaced with the {@link EMPTY} constant or {@link scheduled} (e.g. `scheduled([], scheduler)`). Will be removed in v8.
72 */
73 export function empty(scheduler?: SchedulerLike) {
74 return scheduler ? emptyScheduled(scheduler) : EMPTY;
75 }
76
77 function emptyScheduled(scheduler: SchedulerLike) {
78 return new Observable<never>((subscriber) => scheduler.schedule(() => subscriber.complete()));
79 }