a zip code crypto-currency system good for red ONLY

ForkJoinObservable.js 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 EmptyObservable_1 = require('./EmptyObservable');
  9. var isArray_1 = require('../util/isArray');
  10. var subscribeToResult_1 = require('../util/subscribeToResult');
  11. var OuterSubscriber_1 = require('../OuterSubscriber');
  12. /**
  13. * We need this JSDoc comment for affecting ESDoc.
  14. * @extends {Ignored}
  15. * @hide true
  16. */
  17. var ForkJoinObservable = (function (_super) {
  18. __extends(ForkJoinObservable, _super);
  19. function ForkJoinObservable(sources, resultSelector) {
  20. _super.call(this);
  21. this.sources = sources;
  22. this.resultSelector = resultSelector;
  23. }
  24. /* tslint:enable:max-line-length */
  25. /**
  26. * Joins last values emitted by passed Observables.
  27. *
  28. * <span class="informal">Wait for Observables to complete and then combine last values they emitted.</span>
  29. *
  30. * <img src="./img/forkJoin.png" width="100%">
  31. *
  32. * `forkJoin` is an operator that takes any number of Observables which can be passed either as an array
  33. * or directly as arguments. If no input Observables are provided, resulting stream will complete
  34. * immediately.
  35. *
  36. * `forkJoin` will wait for all passed Observables to complete and then it will emit an array with last
  37. * values from corresponding Observables. So if you pass `n` Observables to the operator, resulting
  38. * array will have `n` values, where first value is the last thing emitted by the first Observable,
  39. * second value is the last thing emitted by the second Observable and so on. That means `forkJoin` will
  40. * not emit more than once and it will complete after that. If you need to emit combined values not only
  41. * at the end of lifecycle of passed Observables, but also throughout it, try out {@link combineLatest}
  42. * or {@link zip} instead.
  43. *
  44. * In order for resulting array to have the same length as the number of input Observables, whenever any of
  45. * that Observables completes without emitting any value, `forkJoin` will complete at that moment as well
  46. * and it will not emit anything either, even if it already has some last values from other Observables.
  47. * Conversely, if there is an Observable that never completes, `forkJoin` will never complete as well,
  48. * unless at any point some other Observable completes without emitting value, which brings us back to
  49. * the previous case. Overall, in order for `forkJoin` to emit a value, all Observables passed as arguments
  50. * have to emit something at least once and complete.
  51. *
  52. * If any input Observable errors at some point, `forkJoin` will error as well and all other Observables
  53. * will be immediately unsubscribed.
  54. *
  55. * Optionally `forkJoin` accepts project function, that will be called with values which normally
  56. * would land in emitted array. Whatever is returned by project function, will appear in output
  57. * Observable instead. This means that default project can be thought of as a function that takes
  58. * all its arguments and puts them into an array. Note that project function will be called only
  59. * when output Observable is supposed to emit a result.
  60. *
  61. * @example <caption>Use forkJoin with operator emitting immediately</caption>
  62. * const observable = Rx.Observable.forkJoin(
  63. * Rx.Observable.of(1, 2, 3, 4),
  64. * Rx.Observable.of(5, 6, 7, 8)
  65. * );
  66. * observable.subscribe(
  67. * value => console.log(value),
  68. * err => {},
  69. * () => console.log('This is how it ends!')
  70. * );
  71. *
  72. * // Logs:
  73. * // [4, 8]
  74. * // "This is how it ends!"
  75. *
  76. *
  77. * @example <caption>Use forkJoin with operator emitting after some time</caption>
  78. * const observable = Rx.Observable.forkJoin(
  79. * Rx.Observable.interval(1000).take(3), // emit 0, 1, 2 every second and complete
  80. * Rx.Observable.interval(500).take(4) // emit 0, 1, 2, 3 every half a second and complete
  81. * );
  82. * observable.subscribe(
  83. * value => console.log(value),
  84. * err => {},
  85. * () => console.log('This is how it ends!')
  86. * );
  87. *
  88. * // Logs:
  89. * // [2, 3] after 3 seconds
  90. * // "This is how it ends!" immediately after
  91. *
  92. *
  93. * @example <caption>Use forkJoin with project function</caption>
  94. * const observable = Rx.Observable.forkJoin(
  95. * Rx.Observable.interval(1000).take(3), // emit 0, 1, 2 every second and complete
  96. * Rx.Observable.interval(500).take(4), // emit 0, 1, 2, 3 every half a second and complete
  97. * (n, m) => n + m
  98. * );
  99. * observable.subscribe(
  100. * value => console.log(value),
  101. * err => {},
  102. * () => console.log('This is how it ends!')
  103. * );
  104. *
  105. * // Logs:
  106. * // 5 after 3 seconds
  107. * // "This is how it ends!" immediately after
  108. *
  109. * @see {@link combineLatest}
  110. * @see {@link zip}
  111. *
  112. * @param {...SubscribableOrPromise} sources Any number of Observables provided either as an array or as an arguments
  113. * passed directly to the operator.
  114. * @param {function} [project] Function that takes values emitted by input Observables and returns value
  115. * that will appear in resulting Observable instead of default array.
  116. * @return {Observable} Observable emitting either an array of last values emitted by passed Observables
  117. * or value from project function.
  118. * @static true
  119. * @name forkJoin
  120. * @owner Observable
  121. */
  122. ForkJoinObservable.create = function () {
  123. var sources = [];
  124. for (var _i = 0; _i < arguments.length; _i++) {
  125. sources[_i - 0] = arguments[_i];
  126. }
  127. if (sources === null || arguments.length === 0) {
  128. return new EmptyObservable_1.EmptyObservable();
  129. }
  130. var resultSelector = null;
  131. if (typeof sources[sources.length - 1] === 'function') {
  132. resultSelector = sources.pop();
  133. }
  134. // if the first and only other argument besides the resultSelector is an array
  135. // assume it's been called with `forkJoin([obs1, obs2, obs3], resultSelector)`
  136. if (sources.length === 1 && isArray_1.isArray(sources[0])) {
  137. sources = sources[0];
  138. }
  139. if (sources.length === 0) {
  140. return new EmptyObservable_1.EmptyObservable();
  141. }
  142. return new ForkJoinObservable(sources, resultSelector);
  143. };
  144. /** @deprecated internal use only */ ForkJoinObservable.prototype._subscribe = function (subscriber) {
  145. return new ForkJoinSubscriber(subscriber, this.sources, this.resultSelector);
  146. };
  147. return ForkJoinObservable;
  148. }(Observable_1.Observable));
  149. exports.ForkJoinObservable = ForkJoinObservable;
  150. /**
  151. * We need this JSDoc comment for affecting ESDoc.
  152. * @ignore
  153. * @extends {Ignored}
  154. */
  155. var ForkJoinSubscriber = (function (_super) {
  156. __extends(ForkJoinSubscriber, _super);
  157. function ForkJoinSubscriber(destination, sources, resultSelector) {
  158. _super.call(this, destination);
  159. this.sources = sources;
  160. this.resultSelector = resultSelector;
  161. this.completed = 0;
  162. this.haveValues = 0;
  163. var len = sources.length;
  164. this.total = len;
  165. this.values = new Array(len);
  166. for (var i = 0; i < len; i++) {
  167. var source = sources[i];
  168. var innerSubscription = subscribeToResult_1.subscribeToResult(this, source, null, i);
  169. if (innerSubscription) {
  170. innerSubscription.outerIndex = i;
  171. this.add(innerSubscription);
  172. }
  173. }
  174. }
  175. ForkJoinSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  176. this.values[outerIndex] = innerValue;
  177. if (!innerSub._hasValue) {
  178. innerSub._hasValue = true;
  179. this.haveValues++;
  180. }
  181. };
  182. ForkJoinSubscriber.prototype.notifyComplete = function (innerSub) {
  183. var destination = this.destination;
  184. var _a = this, haveValues = _a.haveValues, resultSelector = _a.resultSelector, values = _a.values;
  185. var len = values.length;
  186. if (!innerSub._hasValue) {
  187. destination.complete();
  188. return;
  189. }
  190. this.completed++;
  191. if (this.completed !== len) {
  192. return;
  193. }
  194. if (haveValues === len) {
  195. var value = resultSelector ? resultSelector.apply(this, values) : values;
  196. destination.next(value);
  197. }
  198. destination.complete();
  199. };
  200. return ForkJoinSubscriber;
  201. }(OuterSubscriber_1.OuterSubscriber));
  202. //# sourceMappingURL=ForkJoinObservable.js.map