DeferObservable.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /** PURE_IMPORTS_START .._Observable,.._util_subscribeToResult,.._OuterSubscriber PURE_IMPORTS_END */
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b)
  4. if (b.hasOwnProperty(p))
  5. d[p] = b[p];
  6. function __() { this.constructor = d; }
  7. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  8. };
  9. import { Observable } from '../Observable';
  10. import { subscribeToResult } from '../util/subscribeToResult';
  11. import { OuterSubscriber } from '../OuterSubscriber';
  12. /**
  13. * We need this JSDoc comment for affecting ESDoc.
  14. * @extends {Ignored}
  15. * @hide true
  16. */
  17. export var DeferObservable = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  18. __extends(DeferObservable, _super);
  19. function DeferObservable(observableFactory) {
  20. _super.call(this);
  21. this.observableFactory = observableFactory;
  22. }
  23. /**
  24. * Creates an Observable that, on subscribe, calls an Observable factory to
  25. * make an Observable for each new Observer.
  26. *
  27. * <span class="informal">Creates the Observable lazily, that is, only when it
  28. * is subscribed.
  29. * </span>
  30. *
  31. * <img src="./img/defer.png" width="100%">
  32. *
  33. * `defer` allows you to create the Observable only when the Observer
  34. * subscribes, and create a fresh Observable for each Observer. It waits until
  35. * an Observer subscribes to it, and then it generates an Observable,
  36. * typically with an Observable factory function. It does this afresh for each
  37. * subscriber, so although each subscriber may think it is subscribing to the
  38. * same Observable, in fact each subscriber gets its own individual
  39. * Observable.
  40. *
  41. * @example <caption>Subscribe to either an Observable of clicks or an Observable of interval, at random</caption>
  42. * var clicksOrInterval = Rx.Observable.defer(function () {
  43. * if (Math.random() > 0.5) {
  44. * return Rx.Observable.fromEvent(document, 'click');
  45. * } else {
  46. * return Rx.Observable.interval(1000);
  47. * }
  48. * });
  49. * clicksOrInterval.subscribe(x => console.log(x));
  50. *
  51. * // Results in the following behavior:
  52. * // If the result of Math.random() is greater than 0.5 it will listen
  53. * // for clicks anywhere on the "document"; when document is clicked it
  54. * // will log a MouseEvent object to the console. If the result is less
  55. * // than 0.5 it will emit ascending numbers, one every second(1000ms).
  56. *
  57. * @see {@link create}
  58. *
  59. * @param {function(): SubscribableOrPromise} observableFactory The Observable
  60. * factory function to invoke for each Observer that subscribes to the output
  61. * Observable. May also return a Promise, which will be converted on the fly
  62. * to an Observable.
  63. * @return {Observable} An Observable whose Observers' subscriptions trigger
  64. * an invocation of the given Observable factory function.
  65. * @static true
  66. * @name defer
  67. * @owner Observable
  68. */
  69. DeferObservable.create = function (observableFactory) {
  70. return new DeferObservable(observableFactory);
  71. };
  72. /** @deprecated internal use only */ DeferObservable.prototype._subscribe = function (subscriber) {
  73. return new DeferSubscriber(subscriber, this.observableFactory);
  74. };
  75. return DeferObservable;
  76. }(Observable));
  77. var DeferSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  78. __extends(DeferSubscriber, _super);
  79. function DeferSubscriber(destination, factory) {
  80. _super.call(this, destination);
  81. this.factory = factory;
  82. this.tryDefer();
  83. }
  84. DeferSubscriber.prototype.tryDefer = function () {
  85. try {
  86. this._callFactory();
  87. }
  88. catch (err) {
  89. this._error(err);
  90. }
  91. };
  92. DeferSubscriber.prototype._callFactory = function () {
  93. var result = this.factory();
  94. if (result) {
  95. this.add(subscribeToResult(this, result));
  96. }
  97. };
  98. return DeferSubscriber;
  99. }(OuterSubscriber));
  100. //# sourceMappingURL=DeferObservable.js.map