a zip code crypto-currency system good for red ONLY

scan.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 Subscriber_1 = require('../Subscriber');
  8. /* tslint:enable:max-line-length */
  9. /**
  10. * Applies an accumulator function over the source Observable, and returns each
  11. * intermediate result, with an optional seed value.
  12. *
  13. * <span class="informal">It's like {@link reduce}, but emits the current
  14. * accumulation whenever the source emits a value.</span>
  15. *
  16. * <img src="./img/scan.png" width="100%">
  17. *
  18. * Combines together all values emitted on the source, using an accumulator
  19. * function that knows how to join a new source value into the accumulation from
  20. * the past. Is similar to {@link reduce}, but emits the intermediate
  21. * accumulations.
  22. *
  23. * Returns an Observable that applies a specified `accumulator` function to each
  24. * item emitted by the source Observable. If a `seed` value is specified, then
  25. * that value will be used as the initial value for the accumulator. If no seed
  26. * value is specified, the first item of the source is used as the seed.
  27. *
  28. * @example <caption>Count the number of click events</caption>
  29. * var clicks = Rx.Observable.fromEvent(document, 'click');
  30. * var ones = clicks.mapTo(1);
  31. * var seed = 0;
  32. * var count = ones.scan((acc, one) => acc + one, seed);
  33. * count.subscribe(x => console.log(x));
  34. *
  35. * @see {@link expand}
  36. * @see {@link mergeScan}
  37. * @see {@link reduce}
  38. *
  39. * @param {function(acc: R, value: T, index: number): R} accumulator
  40. * The accumulator function called on each source value.
  41. * @param {T|R} [seed] The initial accumulation value.
  42. * @return {Observable<R>} An observable of the accumulated values.
  43. * @method scan
  44. * @owner Observable
  45. */
  46. function scan(accumulator, seed) {
  47. var hasSeed = false;
  48. // providing a seed of `undefined` *should* be valid and trigger
  49. // hasSeed! so don't use `seed !== undefined` checks!
  50. // For this reason, we have to check it here at the original call site
  51. // otherwise inside Operator/Subscriber we won't know if `undefined`
  52. // means they didn't provide anything or if they literally provided `undefined`
  53. if (arguments.length >= 2) {
  54. hasSeed = true;
  55. }
  56. return function scanOperatorFunction(source) {
  57. return source.lift(new ScanOperator(accumulator, seed, hasSeed));
  58. };
  59. }
  60. exports.scan = scan;
  61. var ScanOperator = (function () {
  62. function ScanOperator(accumulator, seed, hasSeed) {
  63. if (hasSeed === void 0) { hasSeed = false; }
  64. this.accumulator = accumulator;
  65. this.seed = seed;
  66. this.hasSeed = hasSeed;
  67. }
  68. ScanOperator.prototype.call = function (subscriber, source) {
  69. return source.subscribe(new ScanSubscriber(subscriber, this.accumulator, this.seed, this.hasSeed));
  70. };
  71. return ScanOperator;
  72. }());
  73. /**
  74. * We need this JSDoc comment for affecting ESDoc.
  75. * @ignore
  76. * @extends {Ignored}
  77. */
  78. var ScanSubscriber = (function (_super) {
  79. __extends(ScanSubscriber, _super);
  80. function ScanSubscriber(destination, accumulator, _seed, hasSeed) {
  81. _super.call(this, destination);
  82. this.accumulator = accumulator;
  83. this._seed = _seed;
  84. this.hasSeed = hasSeed;
  85. this.index = 0;
  86. }
  87. Object.defineProperty(ScanSubscriber.prototype, "seed", {
  88. get: function () {
  89. return this._seed;
  90. },
  91. set: function (value) {
  92. this.hasSeed = true;
  93. this._seed = value;
  94. },
  95. enumerable: true,
  96. configurable: true
  97. });
  98. ScanSubscriber.prototype._next = function (value) {
  99. if (!this.hasSeed) {
  100. this.seed = value;
  101. this.destination.next(value);
  102. }
  103. else {
  104. return this._tryNext(value);
  105. }
  106. };
  107. ScanSubscriber.prototype._tryNext = function (value) {
  108. var index = this.index++;
  109. var result;
  110. try {
  111. result = this.accumulator(this.seed, value, index);
  112. }
  113. catch (err) {
  114. this.destination.error(err);
  115. }
  116. this.seed = result;
  117. this.destination.next(result);
  118. };
  119. return ScanSubscriber;
  120. }(Subscriber_1.Subscriber));
  121. //# sourceMappingURL=scan.js.map