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 OuterSubscriber_1 = require("../OuterSubscriber");
|
20 | var subscribeToResult_1 = require("../util/subscribeToResult");
|
21 | var iterator_1 = require("../../internal/symbol/iterator");
|
22 | function zip() {
|
23 | var observables = [];
|
24 | for (var _i = 0; _i < arguments.length; _i++) {
|
25 | observables[_i] = arguments[_i];
|
26 | }
|
27 | var resultSelector = observables[observables.length - 1];
|
28 | if (typeof resultSelector === 'function') {
|
29 | observables.pop();
|
30 | }
|
31 | return fromArray_1.fromArray(observables, undefined).lift(new ZipOperator(resultSelector));
|
32 | }
|
33 | exports.zip = zip;
|
34 | var ZipOperator = (function () {
|
35 | function ZipOperator(resultSelector) {
|
36 | this.resultSelector = resultSelector;
|
37 | }
|
38 | ZipOperator.prototype.call = function (subscriber, source) {
|
39 | return source.subscribe(new ZipSubscriber(subscriber, this.resultSelector));
|
40 | };
|
41 | return ZipOperator;
|
42 | }());
|
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;
|
49 | _this.iterators = [];
|
50 | _this.active = 0;
|
51 | _this.resultSelector = (typeof resultSelector === 'function') ? resultSelector : null;
|
52 | _this.values = values;
|
53 | return _this;
|
54 | }
|
55 | ZipSubscriber.prototype._next = function (value) {
|
56 | var iterators = this.iterators;
|
57 | if (isArray_1.isArray(value)) {
|
58 | iterators.push(new StaticArrayIterator(value));
|
59 | }
|
60 | else if (typeof value[iterator_1.iterator] === 'function') {
|
61 | iterators.push(new StaticIterator(value[iterator_1.iterator]()));
|
62 | }
|
63 | else {
|
64 | iterators.push(new ZipBufferIterator(this.destination, this, value));
|
65 | }
|
66 | };
|
67 | ZipSubscriber.prototype._complete = function () {
|
68 | var iterators = this.iterators;
|
69 | var len = iterators.length;
|
70 | this.unsubscribe();
|
71 | if (len === 0) {
|
72 | this.destination.complete();
|
73 | return;
|
74 | }
|
75 | this.active = len;
|
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));
|
81 | }
|
82 | else {
|
83 | this.active--;
|
84 | }
|
85 | }
|
86 | };
|
87 | ZipSubscriber.prototype.notifyInactive = function () {
|
88 | this.active--;
|
89 | if (this.active === 0) {
|
90 | this.destination.complete();
|
91 | }
|
92 | };
|
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()) {
|
100 | return;
|
101 | }
|
102 | }
|
103 | var shouldComplete = false;
|
104 | var args = [];
|
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;
|
110 | }
|
111 | if (result.done) {
|
112 | destination.complete();
|
113 | return;
|
114 | }
|
115 | args.push(result.value);
|
116 | }
|
117 | if (this.resultSelector) {
|
118 | this._tryresultSelector(args);
|
119 | }
|
120 | else {
|
121 | destination.next(args);
|
122 | }
|
123 | if (shouldComplete) {
|
124 | destination.complete();
|
125 | }
|
126 | };
|
127 | ZipSubscriber.prototype._tryresultSelector = function (args) {
|
128 | var result;
|
129 | try {
|
130 | result = this.resultSelector.apply(this, args);
|
131 | }
|
132 | catch (err) {
|
133 | this.destination.error(err);
|
134 | return;
|
135 | }
|
136 | this.destination.next(result);
|
137 | };
|
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();
|
145 | }
|
146 | StaticIterator.prototype.hasValue = function () {
|
147 | return true;
|
148 | };
|
149 | StaticIterator.prototype.next = function () {
|
150 | var result = this.nextResult;
|
151 | this.nextResult = this.iterator.next();
|
152 | return result;
|
153 | };
|
154 | StaticIterator.prototype.hasCompleted = function () {
|
155 | var nextResult = this.nextResult;
|
156 | return nextResult && nextResult.done;
|
157 | };
|
158 | return StaticIterator;
|
159 | }());
|
160 | var StaticArrayIterator = (function () {
|
161 | function StaticArrayIterator(array) {
|
162 | this.array = array;
|
163 | this.index = 0;
|
164 | this.length = 0;
|
165 | this.length = array.length;
|
166 | }
|
167 | StaticArrayIterator.prototype[iterator_1.iterator] = function () {
|
168 | return this;
|
169 | };
|
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 };
|
174 | };
|
175 | StaticArrayIterator.prototype.hasValue = function () {
|
176 | return this.array.length > this.index;
|
177 | };
|
178 | StaticArrayIterator.prototype.hasCompleted = function () {
|
179 | return this.array.length === this.index;
|
180 | };
|
181 | return StaticArrayIterator;
|
182 | }());
|
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;
|
190 | _this.buffer = [];
|
191 | _this.isComplete = false;
|
192 | return _this;
|
193 | }
|
194 | ZipBufferIterator.prototype[iterator_1.iterator] = function () {
|
195 | return this;
|
196 | };
|
197 | ZipBufferIterator.prototype.next = function () {
|
198 | var buffer = this.buffer;
|
199 | if (buffer.length === 0 && this.isComplete) {
|
200 | return { value: null, done: true };
|
201 | }
|
202 | else {
|
203 | return { value: buffer.shift(), done: false };
|
204 | }
|
205 | };
|
206 | ZipBufferIterator.prototype.hasValue = function () {
|
207 | return this.buffer.length > 0;
|
208 | };
|
209 | ZipBufferIterator.prototype.hasCompleted = function () {
|
210 | return this.buffer.length === 0 && this.isComplete;
|
211 | };
|
212 | ZipBufferIterator.prototype.notifyComplete = function () {
|
213 | if (this.buffer.length > 0) {
|
214 | this.isComplete = true;
|
215 | this.parent.notifyInactive();
|
216 | }
|
217 | else {
|
218 | this.destination.complete();
|
219 | }
|
220 | };
|
221 | ZipBufferIterator.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
|
222 | this.buffer.push(innerValue);
|
223 | this.parent.checkIterators();
|
224 | };
|
225 | ZipBufferIterator.prototype.subscribe = function (value, index) {
|
226 | return subscribeToResult_1.subscribeToResult(this, this.observable, this, index);
|
227 | };
|
228 | return ZipBufferIterator;
|
229 | }(OuterSubscriber_1.OuterSubscriber));
|
230 |
|
\ | No newline at end of file |