[6a3a178] | 1 | import { Observable } from '../Observable';
|
---|
| 2 | import { SubscribableOrPromise, ObservedValueOf, ObservableInput } from '../types';
|
---|
| 3 | import { from } from './from'; // lol
|
---|
| 4 | import { empty } from './empty';
|
---|
| 5 |
|
---|
| 6 | /**
|
---|
| 7 | * Creates an Observable that, on subscribe, calls an Observable factory to
|
---|
| 8 | * make an Observable for each new Observer.
|
---|
| 9 | *
|
---|
| 10 | * <span class="informal">Creates the Observable lazily, that is, only when it
|
---|
| 11 | * is subscribed.
|
---|
| 12 | * </span>
|
---|
| 13 | *
|
---|
| 14 | * ![](defer.png)
|
---|
| 15 | *
|
---|
| 16 | * `defer` allows you to create the Observable only when the Observer
|
---|
| 17 | * subscribes, and create a fresh Observable for each Observer. It waits until
|
---|
| 18 | * an Observer subscribes to it, and then it generates an Observable,
|
---|
| 19 | * typically with an Observable factory function. It does this afresh for each
|
---|
| 20 | * subscriber, so although each subscriber may think it is subscribing to the
|
---|
| 21 | * same Observable, in fact each subscriber gets its own individual
|
---|
| 22 | * Observable.
|
---|
| 23 | *
|
---|
| 24 | * ## Example
|
---|
| 25 | * ### Subscribe to either an Observable of clicks or an Observable of interval, at random
|
---|
| 26 | * ```ts
|
---|
| 27 | * import { defer, fromEvent, interval } from 'rxjs';
|
---|
| 28 | *
|
---|
| 29 | * const clicksOrInterval = defer(function () {
|
---|
| 30 | * return Math.random() > 0.5
|
---|
| 31 | * ? fromEvent(document, 'click')
|
---|
| 32 | * : interval(1000);
|
---|
| 33 | * });
|
---|
| 34 | * clicksOrInterval.subscribe(x => console.log(x));
|
---|
| 35 | *
|
---|
| 36 | * // Results in the following behavior:
|
---|
| 37 | * // If the result of Math.random() is greater than 0.5 it will listen
|
---|
| 38 | * // for clicks anywhere on the "document"; when document is clicked it
|
---|
| 39 | * // will log a MouseEvent object to the console. If the result is less
|
---|
| 40 | * // than 0.5 it will emit ascending numbers, one every second(1000ms).
|
---|
| 41 | * ```
|
---|
| 42 | *
|
---|
| 43 | * @see {@link Observable}
|
---|
| 44 | *
|
---|
| 45 | * @param {function(): SubscribableOrPromise} observableFactory The Observable
|
---|
| 46 | * factory function to invoke for each Observer that subscribes to the output
|
---|
| 47 | * Observable. May also return a Promise, which will be converted on the fly
|
---|
| 48 | * to an Observable.
|
---|
| 49 | * @return {Observable} An Observable whose Observers' subscriptions trigger
|
---|
| 50 | * an invocation of the given Observable factory function.
|
---|
| 51 | * @static true
|
---|
| 52 | * @name defer
|
---|
| 53 | * @owner Observable
|
---|
| 54 | */
|
---|
| 55 | export function defer<R extends ObservableInput<any> | void>(observableFactory: () => R): Observable<ObservedValueOf<R>> {
|
---|
| 56 | return new Observable<ObservedValueOf<R>>(subscriber => {
|
---|
| 57 | let input: R | void;
|
---|
| 58 | try {
|
---|
| 59 | input = observableFactory();
|
---|
| 60 | } catch (err) {
|
---|
| 61 | subscriber.error(err);
|
---|
| 62 | return undefined;
|
---|
| 63 | }
|
---|
| 64 | const source = input ? from(input as ObservableInput<ObservedValueOf<R>>) : empty();
|
---|
| 65 | return source.subscribe(subscriber);
|
---|
| 66 | });
|
---|
| 67 | }
|
---|