a zip code crypto-currency system good for red ONLY

DeferObservable.js 3.2KB

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