8c2ca3248ab8af4f5089eaf7011d8af851d64bf34dc665ce9c89fd28e71a855369061c1ddaa3bac3db73f6a59925d9e96d56d9b5274c310214718d9d742ff0 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { Observable } from '../Observable';
  2. import { 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 observableFactory The Observable factory function to invoke for each
  45. * Observer that subscribes to the output Observable. May also return any
  46. * `ObservableInput`, which will be converted on the fly to an Observable.
  47. * @return An Observable whose Observers' subscriptions trigger an invocation of the
  48. * given Observable factory function.
  49. */
  50. export declare function defer<R extends ObservableInput<any>>(observableFactory: () => R): Observable<ObservedValueOf<R>>;
  51. //# sourceMappingURL=defer.d.ts.map