[6a3a178] | 1 | import { Observable } from '../Observable';
|
---|
| 2 | import { SchedulerLike } from '../types';
|
---|
| 3 |
|
---|
| 4 | /**
|
---|
| 5 | * The same Observable instance returned by any call to {@link empty} without a
|
---|
| 6 | * `scheduler`. It is preferrable to use this over `empty()`.
|
---|
| 7 | */
|
---|
| 8 | export const EMPTY = new Observable<never>(subscriber => subscriber.complete());
|
---|
| 9 |
|
---|
| 10 | /**
|
---|
| 11 | * Creates an Observable that emits no items to the Observer and immediately
|
---|
| 12 | * emits a complete notification.
|
---|
| 13 | *
|
---|
| 14 | * <span class="informal">Just emits 'complete', and nothing else.
|
---|
| 15 | * </span>
|
---|
| 16 | *
|
---|
| 17 | * ![](empty.png)
|
---|
| 18 | *
|
---|
| 19 | * This static operator is useful for creating a simple Observable that only
|
---|
| 20 | * emits the complete notification. It can be used for composing with other
|
---|
| 21 | * Observables, such as in a {@link mergeMap}.
|
---|
| 22 | *
|
---|
| 23 | * ## Examples
|
---|
| 24 | * ### Emit the number 7, then complete
|
---|
| 25 | * ```ts
|
---|
| 26 | * import { empty } from 'rxjs';
|
---|
| 27 | * import { startWith } from 'rxjs/operators';
|
---|
| 28 | *
|
---|
| 29 | * const result = empty().pipe(startWith(7));
|
---|
| 30 | * result.subscribe(x => console.log(x));
|
---|
| 31 | * ```
|
---|
| 32 | *
|
---|
| 33 | * ### Map and flatten only odd numbers to the sequence 'a', 'b', 'c'
|
---|
| 34 | * ```ts
|
---|
| 35 | * import { empty, interval, of } from 'rxjs';
|
---|
| 36 | * import { mergeMap } from 'rxjs/operators';
|
---|
| 37 | *
|
---|
| 38 | * const interval$ = interval(1000);
|
---|
| 39 | * const result = interval$.pipe(
|
---|
| 40 | * mergeMap(x => x % 2 === 1 ? of('a', 'b', 'c') : empty()),
|
---|
| 41 | * );
|
---|
| 42 | * result.subscribe(x => console.log(x));
|
---|
| 43 | *
|
---|
| 44 | * // Results in the following to the console:
|
---|
| 45 | * // x is equal to the count on the interval eg(0,1,2,3,...)
|
---|
| 46 | * // x will occur every 1000ms
|
---|
| 47 | * // if x % 2 is equal to 1 print abc
|
---|
| 48 | * // if x % 2 is not equal to 1 nothing will be output
|
---|
| 49 | * ```
|
---|
| 50 | *
|
---|
| 51 | * @see {@link Observable}
|
---|
| 52 | * @see {@link never}
|
---|
| 53 | * @see {@link of}
|
---|
| 54 | * @see {@link throwError}
|
---|
| 55 | *
|
---|
| 56 | * @param scheduler A {@link SchedulerLike} to use for scheduling
|
---|
| 57 | * the emission of the complete notification.
|
---|
| 58 | * @return An "empty" Observable: emits only the complete
|
---|
| 59 | * notification.
|
---|
| 60 | * @deprecated Deprecated in favor of using {@link EMPTY} constant, or {@link scheduled} (e.g. `scheduled([], scheduler)`)
|
---|
| 61 | */
|
---|
| 62 | export function empty(scheduler?: SchedulerLike) {
|
---|
| 63 | return scheduler ? emptyScheduled(scheduler) : EMPTY;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | function emptyScheduled(scheduler: SchedulerLike) {
|
---|
| 67 | return new Observable<never>(subscriber => scheduler.schedule(() => subscriber.complete()));
|
---|
| 68 | }
|
---|