a zip code crypto-currency system good for red ONLY

expand.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 tryCatch_1 = require('../util/tryCatch');
  8. var errorObject_1 = require('../util/errorObject');
  9. var OuterSubscriber_1 = require('../OuterSubscriber');
  10. var subscribeToResult_1 = require('../util/subscribeToResult');
  11. /* tslint:enable:max-line-length */
  12. /**
  13. * Recursively projects each source value to an Observable which is merged in
  14. * the output Observable.
  15. *
  16. * <span class="informal">It's similar to {@link mergeMap}, but applies the
  17. * projection function to every source value as well as every output value.
  18. * It's recursive.</span>
  19. *
  20. * <img src="./img/expand.png" width="100%">
  21. *
  22. * Returns an Observable that emits items based on applying a function that you
  23. * supply to each item emitted by the source Observable, where that function
  24. * returns an Observable, and then merging those resulting Observables and
  25. * emitting the results of this merger. *Expand* will re-emit on the output
  26. * Observable every source value. Then, each output value is given to the
  27. * `project` function which returns an inner Observable to be merged on the
  28. * output Observable. Those output values resulting from the projection are also
  29. * given to the `project` function to produce new output values. This is how
  30. * *expand* behaves recursively.
  31. *
  32. * @example <caption>Start emitting the powers of two on every click, at most 10 of them</caption>
  33. * var clicks = Rx.Observable.fromEvent(document, 'click');
  34. * var powersOfTwo = clicks
  35. * .mapTo(1)
  36. * .expand(x => Rx.Observable.of(2 * x).delay(1000))
  37. * .take(10);
  38. * powersOfTwo.subscribe(x => console.log(x));
  39. *
  40. * @see {@link mergeMap}
  41. * @see {@link mergeScan}
  42. *
  43. * @param {function(value: T, index: number) => Observable} project A function
  44. * that, when applied to an item emitted by the source or the output Observable,
  45. * returns an Observable.
  46. * @param {number} [concurrent=Number.POSITIVE_INFINITY] Maximum number of input
  47. * Observables being subscribed to concurrently.
  48. * @param {Scheduler} [scheduler=null] The IScheduler to use for subscribing to
  49. * each projected inner Observable.
  50. * @return {Observable} An Observable that emits the source values and also
  51. * result of applying the projection function to each value emitted on the
  52. * output Observable and and merging the results of the Observables obtained
  53. * from this transformation.
  54. * @method expand
  55. * @owner Observable
  56. */
  57. function expand(project, concurrent, scheduler) {
  58. if (concurrent === void 0) { concurrent = Number.POSITIVE_INFINITY; }
  59. if (scheduler === void 0) { scheduler = undefined; }
  60. concurrent = (concurrent || 0) < 1 ? Number.POSITIVE_INFINITY : concurrent;
  61. return function (source) { return source.lift(new ExpandOperator(project, concurrent, scheduler)); };
  62. }
  63. exports.expand = expand;
  64. var ExpandOperator = (function () {
  65. function ExpandOperator(project, concurrent, scheduler) {
  66. this.project = project;
  67. this.concurrent = concurrent;
  68. this.scheduler = scheduler;
  69. }
  70. ExpandOperator.prototype.call = function (subscriber, source) {
  71. return source.subscribe(new ExpandSubscriber(subscriber, this.project, this.concurrent, this.scheduler));
  72. };
  73. return ExpandOperator;
  74. }());
  75. exports.ExpandOperator = ExpandOperator;
  76. /**
  77. * We need this JSDoc comment for affecting ESDoc.
  78. * @ignore
  79. * @extends {Ignored}
  80. */
  81. var ExpandSubscriber = (function (_super) {
  82. __extends(ExpandSubscriber, _super);
  83. function ExpandSubscriber(destination, project, concurrent, scheduler) {
  84. _super.call(this, destination);
  85. this.project = project;
  86. this.concurrent = concurrent;
  87. this.scheduler = scheduler;
  88. this.index = 0;
  89. this.active = 0;
  90. this.hasCompleted = false;
  91. if (concurrent < Number.POSITIVE_INFINITY) {
  92. this.buffer = [];
  93. }
  94. }
  95. ExpandSubscriber.dispatch = function (arg) {
  96. var subscriber = arg.subscriber, result = arg.result, value = arg.value, index = arg.index;
  97. subscriber.subscribeToProjection(result, value, index);
  98. };
  99. ExpandSubscriber.prototype._next = function (value) {
  100. var destination = this.destination;
  101. if (destination.closed) {
  102. this._complete();
  103. return;
  104. }
  105. var index = this.index++;
  106. if (this.active < this.concurrent) {
  107. destination.next(value);
  108. var result = tryCatch_1.tryCatch(this.project)(value, index);
  109. if (result === errorObject_1.errorObject) {
  110. destination.error(errorObject_1.errorObject.e);
  111. }
  112. else if (!this.scheduler) {
  113. this.subscribeToProjection(result, value, index);
  114. }
  115. else {
  116. var state = { subscriber: this, result: result, value: value, index: index };
  117. this.add(this.scheduler.schedule(ExpandSubscriber.dispatch, 0, state));
  118. }
  119. }
  120. else {
  121. this.buffer.push(value);
  122. }
  123. };
  124. ExpandSubscriber.prototype.subscribeToProjection = function (result, value, index) {
  125. this.active++;
  126. this.add(subscribeToResult_1.subscribeToResult(this, result, value, index));
  127. };
  128. ExpandSubscriber.prototype._complete = function () {
  129. this.hasCompleted = true;
  130. if (this.hasCompleted && this.active === 0) {
  131. this.destination.complete();
  132. }
  133. };
  134. ExpandSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  135. this._next(innerValue);
  136. };
  137. ExpandSubscriber.prototype.notifyComplete = function (innerSub) {
  138. var buffer = this.buffer;
  139. this.remove(innerSub);
  140. this.active--;
  141. if (buffer && buffer.length > 0) {
  142. this._next(buffer.shift());
  143. }
  144. if (this.hasCompleted && this.active === 0) {
  145. this.destination.complete();
  146. }
  147. };
  148. return ExpandSubscriber;
  149. }(OuterSubscriber_1.OuterSubscriber));
  150. exports.ExpandSubscriber = ExpandSubscriber;
  151. //# sourceMappingURL=expand.js.map