UNPKG

2.18 kBTypeScriptView Raw
1import { Observable } from '../Observable';
2import { ObservedValueOf, ObservableInput } from '../types';
3/**
4 * Creates an Observable that, on subscribe, calls an Observable factory to
5 * make an Observable for each new Observer.
6 *
7 * <span class="informal">Creates the Observable lazily, that is, only when it
8 * is subscribed.
9 * </span>
10 *
11 * ![](defer.png)
12 *
13 * `defer` allows you to create an Observable only when the Observer
14 * subscribes. It waits until an Observer subscribes to it, calls the given
15 * factory function to get an Observable -- where a factory function typically
16 * generates a new Observable -- and subscribes the Observer to this Observable.
17 * In case the factory function returns a falsy value, then EMPTY is used as
18 * Observable instead. Last but not least, an exception during the factory
19 * function call is transferred to the Observer by calling `error`.
20 *
21 * ## Example
22 *
23 * Subscribe to either an Observable of clicks or an Observable of interval, at random
24 *
25 * ```ts
26 * import { defer, fromEvent, interval } from 'rxjs';
27 *
28 * const clicksOrInterval = defer(() => {
29 * return Math.random() > 0.5
30 * ? fromEvent(document, 'click')
31 * : interval(1000);
32 * });
33 * clicksOrInterval.subscribe(x => console.log(x));
34 *
35 * // Results in the following behavior:
36 * // If the result of Math.random() is greater than 0.5 it will listen
37 * // for clicks anywhere on the "document"; when document is clicked it
38 * // will log a MouseEvent object to the console. If the result is less
39 * // than 0.5 it will emit ascending numbers, one every second(1000ms).
40 * ```
41 *
42 * @see {@link Observable}
43 *
44 * @param {function(): ObservableInput} observableFactory The Observable
45 * factory function to invoke for each Observer that subscribes to the output
46 * Observable. May also return a Promise, which will be converted on the fly
47 * to an Observable.
48 * @return {Observable} An Observable whose Observers' subscriptions trigger
49 * an invocation of the given Observable factory function.
50 */
51export declare function defer<R extends ObservableInput<any>>(observableFactory: () => R): Observable<ObservedValueOf<R>>;
52//# sourceMappingURL=defer.d.ts.map
\No newline at end of file