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