UI for Zipcoin Blue

DeferObservable.js 3.9KB

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