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);
9 return function (d, b) {
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
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 OuterSubscriber_1 = require("../OuterSubscriber");
20 var subscribeToResult_1 = require("../util/subscribeToResult");
21 var iterator_1 = require("../../internal/symbol/iterator");
24 for (var _i = 0; _i < arguments.length; _i++) {
25 observables[_i] = arguments[_i];
27 var resultSelector = observables[observables.length - 1];
28 if (typeof resultSelector === 'function') {
31 return fromArray_1.fromArray(observables, undefined).lift(new ZipOperator(resultSelector));
34 var ZipOperator = (function () {
35 function ZipOperator(resultSelector) {
36 this.resultSelector = resultSelector;
38 ZipOperator.prototype.call = function (subscriber, source) {
39 return source.subscribe(new ZipSubscriber(subscriber, this.resultSelector));
43 exports.ZipOperator = ZipOperator;
44 var ZipSubscriber = (function (_super) {
45 __extends(ZipSubscriber, _super);
46 function ZipSubscriber(destination, resultSelector, values) {
47 if (values === void 0) { values = Object.create(null); }
48 var _this = _super.call(this, destination) || this;
51 _this.resultSelector = (typeof resultSelector === 'function') ? resultSelector : null;
52 _this.values = values;
55 ZipSubscriber.prototype._next = function (value) {
56 var iterators = this.iterators;
57 if (isArray_1.isArray(value)) {
58 iterators.push(new StaticArrayIterator(value));
60 else if (typeof value[iterator_1.iterator] === 'function') {
61 iterators.push(new StaticIterator(value[iterator_1.iterator]()));
64 iterators.push(new ZipBufferIterator(this.destination, this, value));
67 ZipSubscriber.prototype._complete = function () {
68 var iterators = this.iterators;
69 var len = iterators.length;
72 this.destination.complete();
76 for (var i = 0; i < len; i++) {
77 var iterator = iterators[i];
78 if (iterator.stillUnsubscribed) {
79 var destination = this.destination;
80 destination.add(iterator.subscribe(iterator, i));
87 ZipSubscriber.prototype.notifyInactive = function () {
89 if (this.active === 0) {
90 this.destination.complete();
93 ZipSubscriber.prototype.checkIterators = function () {
94 var iterators = this.iterators;
95 var len = iterators.length;
96 var destination = this.destination;
97 for (var i = 0; i < len; i++) {
98 var iterator = iterators[i];
99 if (typeof iterator.hasValue === 'function' && !iterator.hasValue()) {
103 var shouldComplete = false;
105 for (var i = 0; i < len; i++) {
106 var iterator = iterators[i];
107 var result = iterator.next();
108 if (iterator.hasCompleted()) {
109 shouldComplete = true;
112 destination.complete();
115 args.push(result.value);
117 if (this.resultSelector) {
118 this._tryresultSelector(args);
121 destination.next(args);
123 if (shouldComplete) {
124 destination.complete();
127 ZipSubscriber.prototype._tryresultSelector = function (args) {
130 result = this.resultSelector.apply(this, args);
133 this.destination.error(err);
136 this.destination.next(result);
138 return ZipSubscriber;
139 }(Subscriber_1.Subscriber));
140 exports.ZipSubscriber = ZipSubscriber;
141 var StaticIterator = (function () {
142 function StaticIterator(iterator) {
143 this.iterator = iterator;
144 this.nextResult = iterator.next();
146 StaticIterator.prototype.hasValue = function () {
149 StaticIterator.prototype.next = function () {
150 var result = this.nextResult;
151 this.nextResult = this.iterator.next();
154 StaticIterator.prototype.hasCompleted = function () {
155 var nextResult = this.nextResult;
156 return nextResult && nextResult.done;
158 return StaticIterator;
160 var StaticArrayIterator = (function () {
161 function StaticArrayIterator(array) {
165 this.length = array.length;
167 StaticArrayIterator.prototype[iterator_1.iterator] = function () {
170 StaticArrayIterator.prototype.next = function (value) {
171 var i = this.index++;
172 var array = this.array;
173 return i < this.length ? { value: array[i], done: false } : { value: null, done: true };
175 StaticArrayIterator.prototype.hasValue = function () {
176 return this.array.length > this.index;
178 StaticArrayIterator.prototype.hasCompleted = function () {
179 return this.array.length === this.index;
181 return StaticArrayIterator;
183 var ZipBufferIterator = (function (_super) {
184 __extends(ZipBufferIterator, _super);
185 function ZipBufferIterator(destination, parent, observable) {
186 var _this = _super.call(this, destination) || this;
187 _this.parent = parent;
188 _this.observable = observable;
189 _this.stillUnsubscribed = true;
191 _this.isComplete = false;
194 ZipBufferIterator.prototype[iterator_1.iterator] = function () {
197 ZipBufferIterator.prototype.next = function () {
198 var buffer = this.buffer;
199 if (buffer.length === 0 && this.isComplete) {
200 return { value: null, done: true };
203 return { value: buffer.shift(), done: false };
206 ZipBufferIterator.prototype.hasValue = function () {
207 return this.buffer.length > 0;
209 ZipBufferIterator.prototype.hasCompleted = function () {
210 return this.buffer.length === 0 && this.isComplete;
212 ZipBufferIterator.prototype.notifyComplete = function () {
213 if (this.buffer.length > 0) {
214 this.isComplete = true;
215 this.parent.notifyInactive();
218 this.destination.complete();
221 ZipBufferIterator.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
222 this.buffer.push(innerValue);
223 this.parent.checkIterators();
225 ZipBufferIterator.prototype.subscribe = function (value, index) {
226 return subscribeToResult_1.subscribeToResult(this, this.observable, this, index);
228 return ZipBufferIterator;
229 }(OuterSubscriber_1.OuterSubscriber));
230 //# sourceMappingURL=zip.js.map