zip.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. var __read = (this && this.__read) || function (o, n) {
  3. var m = typeof Symbol === "function" && o[Symbol.iterator];
  4. if (!m) return o;
  5. var i = m.call(o), r, ar = [], e;
  6. try {
  7. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  8. }
  9. catch (error) { e = { error: error }; }
  10. finally {
  11. try {
  12. if (r && !r.done && (m = i["return"])) m.call(i);
  13. }
  14. finally { if (e) throw e.error; }
  15. }
  16. return ar;
  17. };
  18. var __spreadArray = (this && this.__spreadArray) || function (to, from) {
  19. for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
  20. to[j] = from[i];
  21. return to;
  22. };
  23. Object.defineProperty(exports, "__esModule", { value: true });
  24. exports.zip = void 0;
  25. var Observable_1 = require("../Observable");
  26. var innerFrom_1 = require("./innerFrom");
  27. var argsOrArgArray_1 = require("../util/argsOrArgArray");
  28. var empty_1 = require("./empty");
  29. var OperatorSubscriber_1 = require("../operators/OperatorSubscriber");
  30. var args_1 = require("../util/args");
  31. function zip() {
  32. var args = [];
  33. for (var _i = 0; _i < arguments.length; _i++) {
  34. args[_i] = arguments[_i];
  35. }
  36. var resultSelector = args_1.popResultSelector(args);
  37. var sources = argsOrArgArray_1.argsOrArgArray(args);
  38. return sources.length
  39. ? new Observable_1.Observable(function (subscriber) {
  40. var buffers = sources.map(function () { return []; });
  41. var completed = sources.map(function () { return false; });
  42. subscriber.add(function () {
  43. buffers = completed = null;
  44. });
  45. var _loop_1 = function (sourceIndex) {
  46. innerFrom_1.innerFrom(sources[sourceIndex]).subscribe(OperatorSubscriber_1.createOperatorSubscriber(subscriber, function (value) {
  47. buffers[sourceIndex].push(value);
  48. if (buffers.every(function (buffer) { return buffer.length; })) {
  49. var result = buffers.map(function (buffer) { return buffer.shift(); });
  50. subscriber.next(resultSelector ? resultSelector.apply(void 0, __spreadArray([], __read(result))) : result);
  51. if (buffers.some(function (buffer, i) { return !buffer.length && completed[i]; })) {
  52. subscriber.complete();
  53. }
  54. }
  55. }, function () {
  56. completed[sourceIndex] = true;
  57. !buffers[sourceIndex].length && subscriber.complete();
  58. }));
  59. };
  60. for (var sourceIndex = 0; !subscriber.closed && sourceIndex < sources.length; sourceIndex++) {
  61. _loop_1(sourceIndex);
  62. }
  63. return function () {
  64. buffers = completed = null;
  65. };
  66. })
  67. : empty_1.EMPTY;
  68. }
  69. exports.zip = zip;
  70. //# sourceMappingURL=zip.js.map