import { Observable } from '../Observable'; import { SubscribableOrPromise, ObservedValueOf, ObservableInput } from '../types'; import { from } from './from'; // lol import { empty } from './empty'; /** * Creates an Observable that, on subscribe, calls an Observable factory to * make an Observable for each new Observer. * * <span class="informal">Creates the Observable lazily, that is, only when it * is subscribed. * </span> * *  * * `defer` allows you to create the Observable only when the Observer * subscribes, and create a fresh Observable for each Observer. It waits until * an Observer subscribes to it, and then it generates an Observable, * typically with an Observable factory function. It does this afresh for each * subscriber, so although each subscriber may think it is subscribing to the * same Observable, in fact each subscriber gets its own individual * Observable. * * ## Example * ### Subscribe to either an Observable of clicks or an Observable of interval, at random * ```ts * import { defer, fromEvent, interval } from 'rxjs'; * * const clicksOrInterval = defer(function () { * return Math.random() > 0.5 * ? fromEvent(document, 'click') * : interval(1000); * }); * clicksOrInterval.subscribe(x => console.log(x)); * * // Results in the following behavior: * // If the result of Math.random() is greater than 0.5 it will listen * // for clicks anywhere on the "document"; when document is clicked it * // will log a MouseEvent object to the console. If the result is less * // than 0.5 it will emit ascending numbers, one every second(1000ms). * ``` * * @see {@link Observable} * * @param {function(): SubscribableOrPromise} observableFactory The Observable * factory function to invoke for each Observer that subscribes to the output * Observable. May also return a Promise, which will be converted on the fly * to an Observable. * @return {Observable} An Observable whose Observers' subscriptions trigger * an invocation of the given Observable factory function. * @static true * @name defer * @owner Observable */ export function defer<R extends ObservableInput<any> | void>(observableFactory: () => R): Observable<ObservedValueOf<R>> { return new Observable<ObservedValueOf<R>>(subscriber => { let input: R | void; try { input = observableFactory(); } catch (err) { subscriber.error(err); return undefined; } const source = input ? from(input as ObservableInput<ObservedValueOf<R>>) : empty(); return source.subscribe(subscriber); }); }
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
dom | Folder | 0755 |
|
|
ConnectableObservable.ts | File | 5.42 KB | 0644 |
|
SubscribeOnObservable.ts | File | 1.59 KB | 0644 |
|
bindCallback.ts | File | 15.9 KB | 0644 |
|
bindNodeCallback.ts | File | 15.41 KB | 0644 |
|
combineLatest.ts | File | 21.95 KB | 0644 |
|
concat.ts | File | 9.43 KB | 0644 |
|
defer.ts | File | 2.53 KB | 0644 |
|
empty.ts | File | 2.21 KB | 0644 |
|
forkJoin.ts | File | 9.49 KB | 0644 |
|
from.ts | File | 3.06 KB | 0644 |
|
fromArray.ts | File | 423 B | 0644 |
|
fromEvent.ts | File | 10.38 KB | 0644 |
|
fromEventPattern.ts | File | 7.14 KB | 0644 |
|
fromIterable.ts | File | 511 B | 0644 |
|
fromObservable.ts | File | 485 B | 0644 |
|
fromPromise.ts | File | 439 B | 0644 |
|
generate.ts | File | 12.55 KB | 0644 |
|
iif.ts | File | 3.22 KB | 0644 |
|
interval.ts | File | 2.53 KB | 0644 |
|
merge.ts | File | 9.94 KB | 0644 |
|
never.ts | File | 1.16 KB | 0644 |
|
of.ts | File | 4.77 KB | 0644 |
|
onErrorResumeNext.ts | File | 3.83 KB | 0644 |
|
pairs.ts | File | 3.06 KB | 0644 |
|
partition.ts | File | 2.61 KB | 0644 |
|
race.ts | File | 5.03 KB | 0644 |
|
range.ts | File | 2.28 KB | 0644 |
|
throwError.ts | File | 2.2 KB | 0644 |
|
timer.ts | File | 3.32 KB | 0644 |
|
using.ts | File | 3.04 KB | 0644 |
|
zip.ts | File | 12.06 KB | 0644 |
|