combineLatest.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.combineLatestInit = exports.combineLatest = void 0;
  4. var Observable_1 = require("../Observable");
  5. var argsArgArrayOrObject_1 = require("../util/argsArgArrayOrObject");
  6. var from_1 = require("./from");
  7. var identity_1 = require("../util/identity");
  8. var mapOneOrManyArgs_1 = require("../util/mapOneOrManyArgs");
  9. var args_1 = require("../util/args");
  10. var createObject_1 = require("../util/createObject");
  11. var OperatorSubscriber_1 = require("../operators/OperatorSubscriber");
  12. var executeSchedule_1 = require("../util/executeSchedule");
  13. function combineLatest() {
  14. var args = [];
  15. for (var _i = 0; _i < arguments.length; _i++) {
  16. args[_i] = arguments[_i];
  17. }
  18. var scheduler = args_1.popScheduler(args);
  19. var resultSelector = args_1.popResultSelector(args);
  20. var _a = argsArgArrayOrObject_1.argsArgArrayOrObject(args), observables = _a.args, keys = _a.keys;
  21. if (observables.length === 0) {
  22. return from_1.from([], scheduler);
  23. }
  24. var result = new Observable_1.Observable(combineLatestInit(observables, scheduler, keys
  25. ?
  26. function (values) { return createObject_1.createObject(keys, values); }
  27. :
  28. identity_1.identity));
  29. return resultSelector ? result.pipe(mapOneOrManyArgs_1.mapOneOrManyArgs(resultSelector)) : result;
  30. }
  31. exports.combineLatest = combineLatest;
  32. function combineLatestInit(observables, scheduler, valueTransform) {
  33. if (valueTransform === void 0) { valueTransform = identity_1.identity; }
  34. return function (subscriber) {
  35. maybeSchedule(scheduler, function () {
  36. var length = observables.length;
  37. var values = new Array(length);
  38. var active = length;
  39. var remainingFirstValues = length;
  40. var _loop_1 = function (i) {
  41. maybeSchedule(scheduler, function () {
  42. var source = from_1.from(observables[i], scheduler);
  43. var hasFirstValue = false;
  44. source.subscribe(OperatorSubscriber_1.createOperatorSubscriber(subscriber, function (value) {
  45. values[i] = value;
  46. if (!hasFirstValue) {
  47. hasFirstValue = true;
  48. remainingFirstValues--;
  49. }
  50. if (!remainingFirstValues) {
  51. subscriber.next(valueTransform(values.slice()));
  52. }
  53. }, function () {
  54. if (!--active) {
  55. subscriber.complete();
  56. }
  57. }));
  58. }, subscriber);
  59. };
  60. for (var i = 0; i < length; i++) {
  61. _loop_1(i);
  62. }
  63. }, subscriber);
  64. };
  65. }
  66. exports.combineLatestInit = combineLatestInit;
  67. function maybeSchedule(scheduler, execute, subscription) {
  68. if (scheduler) {
  69. executeSchedule_1.executeSchedule(subscription, scheduler, execute);
  70. }
  71. else {
  72. execute();
  73. }
  74. }
  75. //# sourceMappingURL=combineLatest.js.map