a zip code crypto-currency system good for red ONLY

withLatestFrom.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /** PURE_IMPORTS_START .._OuterSubscriber,.._util_subscribeToResult 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 { OuterSubscriber } from '../OuterSubscriber';
  10. import { subscribeToResult } from '../util/subscribeToResult';
  11. /* tslint:enable:max-line-length */
  12. /**
  13. * Combines the source Observable with other Observables to create an Observable
  14. * whose values are calculated from the latest values of each, only when the
  15. * source emits.
  16. *
  17. * <span class="informal">Whenever the source Observable emits a value, it
  18. * computes a formula using that value plus the latest values from other input
  19. * Observables, then emits the output of that formula.</span>
  20. *
  21. * <img src="./img/withLatestFrom.png" width="100%">
  22. *
  23. * `withLatestFrom` combines each value from the source Observable (the
  24. * instance) with the latest values from the other input Observables only when
  25. * the source emits a value, optionally using a `project` function to determine
  26. * the value to be emitted on the output Observable. All input Observables must
  27. * emit at least one value before the output Observable will emit a value.
  28. *
  29. * @example <caption>On every click event, emit an array with the latest timer event plus the click event</caption>
  30. * var clicks = Rx.Observable.fromEvent(document, 'click');
  31. * var timer = Rx.Observable.interval(1000);
  32. * var result = clicks.withLatestFrom(timer);
  33. * result.subscribe(x => console.log(x));
  34. *
  35. * @see {@link combineLatest}
  36. *
  37. * @param {ObservableInput} other An input Observable to combine with the source
  38. * Observable. More than one input Observables may be given as argument.
  39. * @param {Function} [project] Projection function for combining values
  40. * together. Receives all values in order of the Observables passed, where the
  41. * first parameter is a value from the source Observable. (e.g.
  42. * `a.withLatestFrom(b, c, (a1, b1, c1) => a1 + b1 + c1)`). If this is not
  43. * passed, arrays will be emitted on the output Observable.
  44. * @return {Observable} An Observable of projected values from the most recent
  45. * values from each input Observable, or an array of the most recent values from
  46. * each input Observable.
  47. * @method withLatestFrom
  48. * @owner Observable
  49. */
  50. export function withLatestFrom() {
  51. var args = [];
  52. for (var _i = 0; _i < arguments.length; _i++) {
  53. args[_i - 0] = arguments[_i];
  54. }
  55. return function (source) {
  56. var project;
  57. if (typeof args[args.length - 1] === 'function') {
  58. project = args.pop();
  59. }
  60. var observables = args;
  61. return source.lift(new WithLatestFromOperator(observables, project));
  62. };
  63. }
  64. var WithLatestFromOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  65. function WithLatestFromOperator(observables, project) {
  66. this.observables = observables;
  67. this.project = project;
  68. }
  69. WithLatestFromOperator.prototype.call = function (subscriber, source) {
  70. return source.subscribe(new WithLatestFromSubscriber(subscriber, this.observables, this.project));
  71. };
  72. return WithLatestFromOperator;
  73. }());
  74. /**
  75. * We need this JSDoc comment for affecting ESDoc.
  76. * @ignore
  77. * @extends {Ignored}
  78. */
  79. var WithLatestFromSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  80. __extends(WithLatestFromSubscriber, _super);
  81. function WithLatestFromSubscriber(destination, observables, project) {
  82. _super.call(this, destination);
  83. this.observables = observables;
  84. this.project = project;
  85. this.toRespond = [];
  86. var len = observables.length;
  87. this.values = new Array(len);
  88. for (var i = 0; i < len; i++) {
  89. this.toRespond.push(i);
  90. }
  91. for (var i = 0; i < len; i++) {
  92. var observable = observables[i];
  93. this.add(subscribeToResult(this, observable, observable, i));
  94. }
  95. }
  96. WithLatestFromSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  97. this.values[outerIndex] = innerValue;
  98. var toRespond = this.toRespond;
  99. if (toRespond.length > 0) {
  100. var found = toRespond.indexOf(outerIndex);
  101. if (found !== -1) {
  102. toRespond.splice(found, 1);
  103. }
  104. }
  105. };
  106. WithLatestFromSubscriber.prototype.notifyComplete = function () {
  107. // noop
  108. };
  109. WithLatestFromSubscriber.prototype._next = function (value) {
  110. if (this.toRespond.length === 0) {
  111. var args = [value].concat(this.values);
  112. if (this.project) {
  113. this._tryProject(args);
  114. }
  115. else {
  116. this.destination.next(args);
  117. }
  118. }
  119. };
  120. WithLatestFromSubscriber.prototype._tryProject = function (args) {
  121. var result;
  122. try {
  123. result = this.project.apply(this, args);
  124. }
  125. catch (err) {
  126. this.destination.error(err);
  127. return;
  128. }
  129. this.destination.next(result);
  130. };
  131. return WithLatestFromSubscriber;
  132. }(OuterSubscriber));
  133. //# sourceMappingURL=withLatestFrom.js.map