extras.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.merge = merge;
  6. exports.combineLatest = combineLatest;
  7. exports.zip = zip;
  8. var _Observable = require("./Observable.js");
  9. // Emits all values from all inputs in parallel
  10. function merge() {
  11. for (var _len = arguments.length, sources = new Array(_len), _key = 0; _key < _len; _key++) {
  12. sources[_key] = arguments[_key];
  13. }
  14. return new _Observable.Observable(function (observer) {
  15. if (sources.length === 0) return _Observable.Observable.from([]);
  16. var count = sources.length;
  17. var subscriptions = sources.map(function (source) {
  18. return _Observable.Observable.from(source).subscribe({
  19. next: function (v) {
  20. observer.next(v);
  21. },
  22. error: function (e) {
  23. observer.error(e);
  24. },
  25. complete: function () {
  26. if (--count === 0) observer.complete();
  27. }
  28. });
  29. });
  30. return function () {
  31. return subscriptions.forEach(function (s) {
  32. return s.unsubscribe();
  33. });
  34. };
  35. });
  36. } // Emits arrays containing the most current values from each input
  37. function combineLatest() {
  38. for (var _len2 = arguments.length, sources = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  39. sources[_key2] = arguments[_key2];
  40. }
  41. return new _Observable.Observable(function (observer) {
  42. if (sources.length === 0) return _Observable.Observable.from([]);
  43. var count = sources.length;
  44. var seen = new Set();
  45. var seenAll = false;
  46. var values = sources.map(function () {
  47. return undefined;
  48. });
  49. var subscriptions = sources.map(function (source, index) {
  50. return _Observable.Observable.from(source).subscribe({
  51. next: function (v) {
  52. values[index] = v;
  53. if (!seenAll) {
  54. seen.add(index);
  55. if (seen.size !== sources.length) return;
  56. seen = null;
  57. seenAll = true;
  58. }
  59. observer.next(Array.from(values));
  60. },
  61. error: function (e) {
  62. observer.error(e);
  63. },
  64. complete: function () {
  65. if (--count === 0) observer.complete();
  66. }
  67. });
  68. });
  69. return function () {
  70. return subscriptions.forEach(function (s) {
  71. return s.unsubscribe();
  72. });
  73. };
  74. });
  75. } // Emits arrays containing the matching index values from each input
  76. function zip() {
  77. for (var _len3 = arguments.length, sources = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
  78. sources[_key3] = arguments[_key3];
  79. }
  80. return new _Observable.Observable(function (observer) {
  81. if (sources.length === 0) return _Observable.Observable.from([]);
  82. var queues = sources.map(function () {
  83. return [];
  84. });
  85. function done() {
  86. return queues.some(function (q, i) {
  87. return q.length === 0 && subscriptions[i].closed;
  88. });
  89. }
  90. var subscriptions = sources.map(function (source, index) {
  91. return _Observable.Observable.from(source).subscribe({
  92. next: function (v) {
  93. queues[index].push(v);
  94. if (queues.every(function (q) {
  95. return q.length > 0;
  96. })) {
  97. observer.next(queues.map(function (q) {
  98. return q.shift();
  99. }));
  100. if (done()) observer.complete();
  101. }
  102. },
  103. error: function (e) {
  104. observer.error(e);
  105. },
  106. complete: function () {
  107. if (done()) observer.complete();
  108. }
  109. });
  110. });
  111. return function () {
  112. return subscriptions.forEach(function (s) {
  113. return s.unsubscribe();
  114. });
  115. };
  116. });
  117. }