zip.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. }
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. var fromArray_1 = require("./fromArray");
  17. var isArray_1 = require("../util/isArray");
  18. var Subscriber_1 = require("../Subscriber");
  19. var iterator_1 = require("../../internal/symbol/iterator");
  20. var innerSubscribe_1 = require("../innerSubscribe");
  21. function zip() {
  22. var observables = [];
  23. for (var _i = 0; _i < arguments.length; _i++) {
  24. observables[_i] = arguments[_i];
  25. }
  26. var resultSelector = observables[observables.length - 1];
  27. if (typeof resultSelector === 'function') {
  28. observables.pop();
  29. }
  30. return fromArray_1.fromArray(observables, undefined).lift(new ZipOperator(resultSelector));
  31. }
  32. exports.zip = zip;
  33. var ZipOperator = (function () {
  34. function ZipOperator(resultSelector) {
  35. this.resultSelector = resultSelector;
  36. }
  37. ZipOperator.prototype.call = function (subscriber, source) {
  38. return source.subscribe(new ZipSubscriber(subscriber, this.resultSelector));
  39. };
  40. return ZipOperator;
  41. }());
  42. exports.ZipOperator = ZipOperator;
  43. var ZipSubscriber = (function (_super) {
  44. __extends(ZipSubscriber, _super);
  45. function ZipSubscriber(destination, resultSelector, values) {
  46. if (values === void 0) { values = Object.create(null); }
  47. var _this = _super.call(this, destination) || this;
  48. _this.resultSelector = resultSelector;
  49. _this.iterators = [];
  50. _this.active = 0;
  51. _this.resultSelector = (typeof resultSelector === 'function') ? resultSelector : undefined;
  52. return _this;
  53. }
  54. ZipSubscriber.prototype._next = function (value) {
  55. var iterators = this.iterators;
  56. if (isArray_1.isArray(value)) {
  57. iterators.push(new StaticArrayIterator(value));
  58. }
  59. else if (typeof value[iterator_1.iterator] === 'function') {
  60. iterators.push(new StaticIterator(value[iterator_1.iterator]()));
  61. }
  62. else {
  63. iterators.push(new ZipBufferIterator(this.destination, this, value));
  64. }
  65. };
  66. ZipSubscriber.prototype._complete = function () {
  67. var iterators = this.iterators;
  68. var len = iterators.length;
  69. this.unsubscribe();
  70. if (len === 0) {
  71. this.destination.complete();
  72. return;
  73. }
  74. this.active = len;
  75. for (var i = 0; i < len; i++) {
  76. var iterator = iterators[i];
  77. if (iterator.stillUnsubscribed) {
  78. var destination = this.destination;
  79. destination.add(iterator.subscribe());
  80. }
  81. else {
  82. this.active--;
  83. }
  84. }
  85. };
  86. ZipSubscriber.prototype.notifyInactive = function () {
  87. this.active--;
  88. if (this.active === 0) {
  89. this.destination.complete();
  90. }
  91. };
  92. ZipSubscriber.prototype.checkIterators = function () {
  93. var iterators = this.iterators;
  94. var len = iterators.length;
  95. var destination = this.destination;
  96. for (var i = 0; i < len; i++) {
  97. var iterator = iterators[i];
  98. if (typeof iterator.hasValue === 'function' && !iterator.hasValue()) {
  99. return;
  100. }
  101. }
  102. var shouldComplete = false;
  103. var args = [];
  104. for (var i = 0; i < len; i++) {
  105. var iterator = iterators[i];
  106. var result = iterator.next();
  107. if (iterator.hasCompleted()) {
  108. shouldComplete = true;
  109. }
  110. if (result.done) {
  111. destination.complete();
  112. return;
  113. }
  114. args.push(result.value);
  115. }
  116. if (this.resultSelector) {
  117. this._tryresultSelector(args);
  118. }
  119. else {
  120. destination.next(args);
  121. }
  122. if (shouldComplete) {
  123. destination.complete();
  124. }
  125. };
  126. ZipSubscriber.prototype._tryresultSelector = function (args) {
  127. var result;
  128. try {
  129. result = this.resultSelector.apply(this, args);
  130. }
  131. catch (err) {
  132. this.destination.error(err);
  133. return;
  134. }
  135. this.destination.next(result);
  136. };
  137. return ZipSubscriber;
  138. }(Subscriber_1.Subscriber));
  139. exports.ZipSubscriber = ZipSubscriber;
  140. var StaticIterator = (function () {
  141. function StaticIterator(iterator) {
  142. this.iterator = iterator;
  143. this.nextResult = iterator.next();
  144. }
  145. StaticIterator.prototype.hasValue = function () {
  146. return true;
  147. };
  148. StaticIterator.prototype.next = function () {
  149. var result = this.nextResult;
  150. this.nextResult = this.iterator.next();
  151. return result;
  152. };
  153. StaticIterator.prototype.hasCompleted = function () {
  154. var nextResult = this.nextResult;
  155. return Boolean(nextResult && nextResult.done);
  156. };
  157. return StaticIterator;
  158. }());
  159. var StaticArrayIterator = (function () {
  160. function StaticArrayIterator(array) {
  161. this.array = array;
  162. this.index = 0;
  163. this.length = 0;
  164. this.length = array.length;
  165. }
  166. StaticArrayIterator.prototype[iterator_1.iterator] = function () {
  167. return this;
  168. };
  169. StaticArrayIterator.prototype.next = function (value) {
  170. var i = this.index++;
  171. var array = this.array;
  172. return i < this.length ? { value: array[i], done: false } : { value: null, done: true };
  173. };
  174. StaticArrayIterator.prototype.hasValue = function () {
  175. return this.array.length > this.index;
  176. };
  177. StaticArrayIterator.prototype.hasCompleted = function () {
  178. return this.array.length === this.index;
  179. };
  180. return StaticArrayIterator;
  181. }());
  182. var ZipBufferIterator = (function (_super) {
  183. __extends(ZipBufferIterator, _super);
  184. function ZipBufferIterator(destination, parent, observable) {
  185. var _this = _super.call(this, destination) || this;
  186. _this.parent = parent;
  187. _this.observable = observable;
  188. _this.stillUnsubscribed = true;
  189. _this.buffer = [];
  190. _this.isComplete = false;
  191. return _this;
  192. }
  193. ZipBufferIterator.prototype[iterator_1.iterator] = function () {
  194. return this;
  195. };
  196. ZipBufferIterator.prototype.next = function () {
  197. var buffer = this.buffer;
  198. if (buffer.length === 0 && this.isComplete) {
  199. return { value: null, done: true };
  200. }
  201. else {
  202. return { value: buffer.shift(), done: false };
  203. }
  204. };
  205. ZipBufferIterator.prototype.hasValue = function () {
  206. return this.buffer.length > 0;
  207. };
  208. ZipBufferIterator.prototype.hasCompleted = function () {
  209. return this.buffer.length === 0 && this.isComplete;
  210. };
  211. ZipBufferIterator.prototype.notifyComplete = function () {
  212. if (this.buffer.length > 0) {
  213. this.isComplete = true;
  214. this.parent.notifyInactive();
  215. }
  216. else {
  217. this.destination.complete();
  218. }
  219. };
  220. ZipBufferIterator.prototype.notifyNext = function (innerValue) {
  221. this.buffer.push(innerValue);
  222. this.parent.checkIterators();
  223. };
  224. ZipBufferIterator.prototype.subscribe = function () {
  225. return innerSubscribe_1.innerSubscribe(this.observable, new innerSubscribe_1.SimpleInnerSubscriber(this));
  226. };
  227. return ZipBufferIterator;
  228. }(innerSubscribe_1.SimpleOuterSubscriber));
  229. //# sourceMappingURL=zip.js.map