source: trip-planner-front/node_modules/rxjs/src/internal/observable/zip.ts@ e29cc2e

Last change on this file since e29cc2e was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 12.1 KB
Line 
1import { Observable } from '../Observable';
2import { fromArray } from './fromArray';
3import { isArray } from '../util/isArray';
4import { Operator } from '../Operator';
5import { ObservableInput, PartialObserver, ObservedValueOf } from '../types';
6import { Subscriber } from '../Subscriber';
7import { Subscription } from '../Subscription';
8import { iterator as Symbol_iterator } from '../../internal/symbol/iterator';
9import { SimpleOuterSubscriber, SimpleInnerSubscriber, innerSubscribe } from '../innerSubscribe';
10
11/* tslint:disable:max-line-length */
12/** @deprecated resultSelector is no longer supported, pipe to map instead */
13export function zip<O1 extends ObservableInput<any>, R>(v1: O1, resultSelector: (v1: ObservedValueOf<O1>) => R): Observable<R>;
14/** @deprecated resultSelector is no longer supported, pipe to map instead */
15export function zip<O1 extends ObservableInput<any>, O2 extends ObservableInput<any>, R>(v1: O1, v2: O2, resultSelector: (v1: ObservedValueOf<O1>, v2: ObservedValueOf<O2>) => R): Observable<R>;
16/** @deprecated resultSelector is no longer supported, pipe to map instead */
17export function zip<O1 extends ObservableInput<any>, O2 extends ObservableInput<any>, O3 extends ObservableInput<any>, R>(v1: O1, v2: O2, v3: O3, resultSelector: (v1: ObservedValueOf<O1>, v2: ObservedValueOf<O2>, v3: ObservedValueOf<O3>) => R): Observable<R>;
18/** @deprecated resultSelector is no longer supported, pipe to map instead */
19export function zip<O1 extends ObservableInput<any>, O2 extends ObservableInput<any>, O3 extends ObservableInput<any>, O4 extends ObservableInput<any>, R>(v1: O1, v2: O2, v3: O3, v4: O4, resultSelector: (v1: ObservedValueOf<O1>, v2: ObservedValueOf<O2>, v3: ObservedValueOf<O3>, v4: ObservedValueOf<O4>) => R): Observable<R>;
20/** @deprecated resultSelector is no longer supported, pipe to map instead */
21export function zip<O1 extends ObservableInput<any>, O2 extends ObservableInput<any>, O3 extends ObservableInput<any>, O4 extends ObservableInput<any>, O5 extends ObservableInput<any>, R>(v1: O1, v2: O2, v3: O3, v4: O4, v5: O5, resultSelector: (v1: ObservedValueOf<O1>, v2: ObservedValueOf<O2>, v3: ObservedValueOf<O3>, v4: ObservedValueOf<O4>, v5: ObservedValueOf<O5>) => R): Observable<R>;
22/** @deprecated resultSelector is no longer supported, pipe to map instead */
23export function zip<O1 extends ObservableInput<any>, O2 extends ObservableInput<any>, O3 extends ObservableInput<any>, O4 extends ObservableInput<any>, O5 extends ObservableInput<any>, O6 extends ObservableInput<any>, R>(v1: O1, v2: O2, v3: O3, v4: O4, v5: O5, v6: O6, resultSelector: (v1: ObservedValueOf<O1>, v2: ObservedValueOf<O2>, v3: ObservedValueOf<O3>, v4: ObservedValueOf<O4>, v5: ObservedValueOf<O5>, v6: ObservedValueOf<O6>) => R): Observable<R>;
24
25export function zip<O1 extends ObservableInput<any>, O2 extends ObservableInput<any>>(v1: O1, v2: O2): Observable<[ObservedValueOf<O1>, ObservedValueOf<O2>]>;
26export function zip<O1 extends ObservableInput<any>, O2 extends ObservableInput<any>, O3 extends ObservableInput<any>>(v1: O1, v2: O2, v3: O3): Observable<[ObservedValueOf<O1>, ObservedValueOf<O2>, ObservedValueOf<O3>]>;
27export function zip<O1 extends ObservableInput<any>, O2 extends ObservableInput<any>, O3 extends ObservableInput<any>, O4 extends ObservableInput<any>>(v1: O1, v2: O2, v3: O3, v4: O4): Observable<[ObservedValueOf<O1>, ObservedValueOf<O2>, ObservedValueOf<O3>, ObservedValueOf<O4>]>;
28export function zip<O1 extends ObservableInput<any>, O2 extends ObservableInput<any>, O3 extends ObservableInput<any>, O4 extends ObservableInput<any>, O5 extends ObservableInput<any>>(v1: O1, v2: O2, v3: O3, v4: O4, v5: O5): Observable<[ObservedValueOf<O1>, ObservedValueOf<O2>, ObservedValueOf<O3>, ObservedValueOf<O4>, ObservedValueOf<O5>]>;
29export function zip<O1 extends ObservableInput<any>, O2 extends ObservableInput<any>, O3 extends ObservableInput<any>, O4 extends ObservableInput<any>, O5 extends ObservableInput<any>, O6 extends ObservableInput<any>>(v1: O1, v2: O2, v3: O3, v4: O4, v5: O5, v6: O6): Observable<[ObservedValueOf<O1>, ObservedValueOf<O2>, ObservedValueOf<O3>, ObservedValueOf<O4>, ObservedValueOf<O5>, ObservedValueOf<O6>]>;
30
31export function zip<O extends ObservableInput<any>>(array: O[]): Observable<ObservedValueOf<O>[]>;
32export function zip<R>(array: ObservableInput<any>[]): Observable<R>;
33/** @deprecated resultSelector is no longer supported, pipe to map instead */
34export function zip<O extends ObservableInput<any>, R>(array: O[], resultSelector: (...values: ObservedValueOf<O>[]) => R): Observable<R>;
35/** @deprecated resultSelector is no longer supported, pipe to map instead */
36export function zip<R>(array: ObservableInput<any>[], resultSelector: (...values: any[]) => R): Observable<R>;
37
38export function zip<O extends ObservableInput<any>>(...observables: O[]): Observable<ObservedValueOf<O>[]>;
39export function zip<O extends ObservableInput<any>, R>(...observables: Array<O | ((...values: ObservedValueOf<O>[]) => R)>): Observable<R>;
40export function zip<R>(...observables: Array<ObservableInput<any> | ((...values: Array<any>) => R)>): Observable<R>;
41/* tslint:enable:max-line-length */
42
43/**
44 * Combines multiple Observables to create an Observable whose values are calculated from the values, in order, of each
45 * of its input Observables.
46 *
47 * If the last parameter is a function, this function is used to compute the created value from the input values.
48 * Otherwise, an array of the input values is returned.
49 *
50 * ## Example
51 * Combine age and name from different sources
52 * ```ts
53 * import { zip, of } from 'rxjs';
54 * import { map } from 'rxjs/operators';
55 *
56 * let age$ = of<number>(27, 25, 29);
57 * let name$ = of<string>('Foo', 'Bar', 'Beer');
58 * let isDev$ = of<boolean>(true, true, false);
59 *
60 * zip(age$, name$, isDev$).pipe(
61 * map(([age, name, isDev]) => ({ age, name, isDev })),
62 * )
63 * .subscribe(x => console.log(x));
64 *
65 * // outputs
66 * // { age: 27, name: 'Foo', isDev: true }
67 * // { age: 25, name: 'Bar', isDev: true }
68 * // { age: 29, name: 'Beer', isDev: false }
69 * ```
70 * @param observables
71 * @return {Observable<R>}
72 * @static true
73 * @name zip
74 * @owner Observable
75 */
76export function zip<O extends ObservableInput<any>, R>(
77 ...observables: Array<O | ((...values: ObservedValueOf<O>[]) => R)>
78): Observable<ObservedValueOf<O>[]|R> {
79 const resultSelector = <((...ys: Array<any>) => R)> observables[observables.length - 1];
80 if (typeof resultSelector === 'function') {
81 observables.pop();
82 }
83 return fromArray(observables, undefined).lift(new ZipOperator(resultSelector));
84}
85
86export class ZipOperator<T, R> implements Operator<T, R> {
87
88 resultSelector?: (...values: Array<any>) => R;
89
90 constructor(resultSelector?: (...values: Array<any>) => R) {
91 this.resultSelector = resultSelector;
92 }
93
94 call(subscriber: Subscriber<R>, source: any): any {
95 return source.subscribe(new ZipSubscriber(subscriber, this.resultSelector));
96 }
97}
98
99/**
100 * We need this JSDoc comment for affecting ESDoc.
101 * @ignore
102 * @extends {Ignored}
103 */
104export class ZipSubscriber<T, R> extends Subscriber<T> {
105 private iterators: LookAheadIterator<any>[] = [];
106 private active = 0;
107
108 constructor(destination: Subscriber<R>,
109 private resultSelector?: (...values: Array<any>) => R,
110 values: any = Object.create(null)) {
111 super(destination);
112 this.resultSelector = (typeof resultSelector === 'function') ? resultSelector : undefined;
113 }
114
115 protected _next(value: any) {
116 const iterators = this.iterators;
117 if (isArray(value)) {
118 iterators.push(new StaticArrayIterator(value));
119 } else if (typeof value[Symbol_iterator] === 'function') {
120 iterators.push(new StaticIterator(value[Symbol_iterator]()));
121 } else {
122 iterators.push(new ZipBufferIterator(this.destination, this, value));
123 }
124 }
125
126 protected _complete() {
127 const iterators = this.iterators;
128 const len = iterators.length;
129
130 this.unsubscribe();
131
132 if (len === 0) {
133 this.destination.complete!();
134 return;
135 }
136
137 this.active = len;
138 for (let i = 0; i < len; i++) {
139 let iterator: ZipBufferIterator<any, any> = <any>iterators[i];
140 if (iterator.stillUnsubscribed) {
141 const destination = this.destination as Subscription;
142 destination.add(iterator.subscribe());
143 } else {
144 this.active--; // not an observable
145 }
146 }
147 }
148
149 notifyInactive() {
150 this.active--;
151 if (this.active === 0) {
152 this.destination.complete!();
153 }
154 }
155
156 checkIterators() {
157 const iterators = this.iterators;
158 const len = iterators.length;
159 const destination = this.destination;
160
161 // abort if not all of them have values
162 for (let i = 0; i < len; i++) {
163 let iterator = iterators[i];
164 if (typeof iterator.hasValue === 'function' && !iterator.hasValue()) {
165 return;
166 }
167 }
168
169 let shouldComplete = false;
170 const args: any[] = [];
171 for (let i = 0; i < len; i++) {
172 let iterator = iterators[i];
173 let result = iterator.next();
174
175 // check to see if it's completed now that you've gotten
176 // the next value.
177 if (iterator.hasCompleted()) {
178 shouldComplete = true;
179 }
180
181 if (result.done) {
182 destination.complete!();
183 return;
184 }
185
186 args.push(result.value);
187 }
188
189 if (this.resultSelector) {
190 this._tryresultSelector(args);
191 } else {
192 destination.next!(args);
193 }
194
195 if (shouldComplete) {
196 destination.complete!();
197 }
198 }
199
200 protected _tryresultSelector(args: any[]) {
201 let result: any;
202 try {
203 result = this.resultSelector!.apply(this, args);
204 } catch (err) {
205 this.destination.error!(err);
206 return;
207 }
208 this.destination.next!(result);
209 }
210}
211
212interface LookAheadIterator<T> extends Iterator<T> {
213 hasValue(): boolean;
214 hasCompleted(): boolean;
215}
216
217class StaticIterator<T> implements LookAheadIterator<T> {
218 private nextResult: IteratorResult<T>;
219
220 constructor(private iterator: Iterator<T>) {
221 this.nextResult = iterator.next();
222 }
223
224 hasValue() {
225 return true;
226 }
227
228 next(): IteratorResult<T> {
229 const result = this.nextResult;
230 this.nextResult = this.iterator.next();
231 return result;
232 }
233
234 hasCompleted(): boolean {
235 const nextResult = this.nextResult;
236 return Boolean(nextResult && nextResult.done);
237 }
238}
239
240class StaticArrayIterator<T> implements LookAheadIterator<T> {
241 private index = 0;
242 private length = 0;
243
244 constructor(private array: T[]) {
245 this.length = array.length;
246 }
247
248 [Symbol_iterator]() {
249 return this;
250 }
251
252 next(value?: any): IteratorResult<T> {
253 const i = this.index++;
254 const array = this.array;
255 return i < this.length ? { value: array[i], done: false } : { value: null, done: true };
256 }
257
258 hasValue() {
259 return this.array.length > this.index;
260 }
261
262 hasCompleted() {
263 return this.array.length === this.index;
264 }
265}
266
267/**
268 * We need this JSDoc comment for affecting ESDoc.
269 * @ignore
270 * @extends {Ignored}
271 */
272class ZipBufferIterator<T, R> extends SimpleOuterSubscriber<T, R> implements LookAheadIterator<T> {
273 stillUnsubscribed = true;
274 buffer: T[] = [];
275 isComplete = false;
276
277 constructor(destination: PartialObserver<T>,
278 private parent: ZipSubscriber<T, R>,
279 private observable: Observable<T>) {
280 super(destination);
281 }
282
283 [Symbol_iterator]() {
284 return this;
285 }
286
287 // NOTE: there is actually a name collision here with Subscriber.next and Iterator.next
288 // this is legit because `next()` will never be called by a subscription in this case.
289 next(): IteratorResult<T> {
290 const buffer = this.buffer;
291 if (buffer.length === 0 && this.isComplete) {
292 return { value: null, done: true };
293 } else {
294 return { value: buffer.shift()!, done: false };
295 }
296 }
297
298 hasValue() {
299 return this.buffer.length > 0;
300 }
301
302 hasCompleted() {
303 return this.buffer.length === 0 && this.isComplete;
304 }
305
306 notifyComplete() {
307 if (this.buffer.length > 0) {
308 this.isComplete = true;
309 this.parent.notifyInactive();
310 } else {
311 this.destination.complete!();
312 }
313 }
314
315 notifyNext(innerValue: any): void {
316 this.buffer.push(innerValue);
317 this.parent.checkIterators();
318 }
319
320 subscribe() {
321 return innerSubscribe(this.observable, new SimpleInnerSubscriber(this));
322 }
323}
Note: See TracBrowser for help on using the repository browser.