forkJoin.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /** PURE_IMPORTS_START tslib,_Observable,_util_isArray,_empty,_util_subscribeToResult,_OuterSubscriber,_operators_map PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { Observable } from '../Observable';
  4. import { isArray } from '../util/isArray';
  5. import { EMPTY } from './empty';
  6. import { subscribeToResult } from '../util/subscribeToResult';
  7. import { OuterSubscriber } from '../OuterSubscriber';
  8. import { map } from '../operators/map';
  9. export function forkJoin() {
  10. var sources = [];
  11. for (var _i = 0; _i < arguments.length; _i++) {
  12. sources[_i] = arguments[_i];
  13. }
  14. var resultSelector;
  15. if (typeof sources[sources.length - 1] === 'function') {
  16. resultSelector = sources.pop();
  17. }
  18. if (sources.length === 1 && isArray(sources[0])) {
  19. sources = sources[0];
  20. }
  21. if (sources.length === 0) {
  22. return EMPTY;
  23. }
  24. if (resultSelector) {
  25. return forkJoin(sources).pipe(map(function (args) { return resultSelector.apply(void 0, args); }));
  26. }
  27. return new Observable(function (subscriber) {
  28. return new ForkJoinSubscriber(subscriber, sources);
  29. });
  30. }
  31. var ForkJoinSubscriber = /*@__PURE__*/ (function (_super) {
  32. tslib_1.__extends(ForkJoinSubscriber, _super);
  33. function ForkJoinSubscriber(destination, sources) {
  34. var _this = _super.call(this, destination) || this;
  35. _this.sources = sources;
  36. _this.completed = 0;
  37. _this.haveValues = 0;
  38. var len = sources.length;
  39. _this.values = new Array(len);
  40. for (var i = 0; i < len; i++) {
  41. var source = sources[i];
  42. var innerSubscription = subscribeToResult(_this, source, null, i);
  43. if (innerSubscription) {
  44. _this.add(innerSubscription);
  45. }
  46. }
  47. return _this;
  48. }
  49. ForkJoinSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  50. this.values[outerIndex] = innerValue;
  51. if (!innerSub._hasValue) {
  52. innerSub._hasValue = true;
  53. this.haveValues++;
  54. }
  55. };
  56. ForkJoinSubscriber.prototype.notifyComplete = function (innerSub) {
  57. var _a = this, destination = _a.destination, haveValues = _a.haveValues, values = _a.values;
  58. var len = values.length;
  59. if (!innerSub._hasValue) {
  60. destination.complete();
  61. return;
  62. }
  63. this.completed++;
  64. if (this.completed !== len) {
  65. return;
  66. }
  67. if (haveValues === len) {
  68. destination.next(values);
  69. }
  70. destination.complete();
  71. };
  72. return ForkJoinSubscriber;
  73. }(OuterSubscriber));
  74. //# sourceMappingURL=forkJoin.js.map