DeferObservable.d.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { Observable, SubscribableOrPromise } from '../Observable';
  2. import { Subscriber } from '../Subscriber';
  3. import { Subscription } from '../Subscription';
  4. /**
  5. * We need this JSDoc comment for affecting ESDoc.
  6. * @extends {Ignored}
  7. * @hide true
  8. */
  9. export declare class DeferObservable<T> extends Observable<T> {
  10. private observableFactory;
  11. /**
  12. * Creates an Observable that, on subscribe, calls an Observable factory to
  13. * make an Observable for each new Observer.
  14. *
  15. * <span class="informal">Creates the Observable lazily, that is, only when it
  16. * is subscribed.
  17. * </span>
  18. *
  19. * <img src="./img/defer.png" width="100%">
  20. *
  21. * `defer` allows you to create the Observable only when the Observer
  22. * subscribes, and create a fresh Observable for each Observer. It waits until
  23. * an Observer subscribes to it, and then it generates an Observable,
  24. * typically with an Observable factory function. It does this afresh for each
  25. * subscriber, so although each subscriber may think it is subscribing to the
  26. * same Observable, in fact each subscriber gets its own individual
  27. * Observable.
  28. *
  29. * @example <caption>Subscribe to either an Observable of clicks or an Observable of interval, at random</caption>
  30. * var clicksOrInterval = Rx.Observable.defer(function () {
  31. * if (Math.random() > 0.5) {
  32. * return Rx.Observable.fromEvent(document, 'click');
  33. * } else {
  34. * return Rx.Observable.interval(1000);
  35. * }
  36. * });
  37. * clicksOrInterval.subscribe(x => console.log(x));
  38. *
  39. * // Results in the following behavior:
  40. * // If the result of Math.random() is greater than 0.5 it will listen
  41. * // for clicks anywhere on the "document"; when document is clicked it
  42. * // will log a MouseEvent object to the console. If the result is less
  43. * // than 0.5 it will emit ascending numbers, one every second(1000ms).
  44. *
  45. * @see {@link create}
  46. *
  47. * @param {function(): SubscribableOrPromise} observableFactory The Observable
  48. * factory function to invoke for each Observer that subscribes to the output
  49. * Observable. May also return a Promise, which will be converted on the fly
  50. * to an Observable.
  51. * @return {Observable} An Observable whose Observers' subscriptions trigger
  52. * an invocation of the given Observable factory function.
  53. * @static true
  54. * @name defer
  55. * @owner Observable
  56. */
  57. static create<T>(observableFactory: () => SubscribableOrPromise<T> | void): Observable<T>;
  58. constructor(observableFactory: () => SubscribableOrPromise<T> | void);
  59. /** @deprecated internal use only */ _subscribe(subscriber: Subscriber<T>): Subscription;
  60. }