| 1 |
import { Observable } from '../Observable'; |
| 2 |
import { ObservableInputTuple } from '../types'; |
| 3 |
import { innerFrom } from './innerFrom'; |
| 4 |
import { argsOrArgArray } from '../util/argsOrArgArray'; |
| 5 |
import { EMPTY } from './empty'; |
| 6 |
import { createOperatorSubscriber } from '../operators/OperatorSubscriber'; |
| 7 |
import { popResultSelector } from '../util/args'; |
| 8 |
|
| 9 |
export function zip<A extends readonly unknown[]>(sources: [...ObservableInputTuple<A>]): Observable<A>; |
| 10 |
export function zip<A extends readonly unknown[], R>( |
| 11 |
sources: [...ObservableInputTuple<A>], |
| 12 |
resultSelector: (...values: A) => R |
| 13 |
): Observable<R>; |
| 14 |
export function zip<A extends readonly unknown[]>(...sources: [...ObservableInputTuple<A>]): Observable<A>; |
| 15 |
export function zip<A extends readonly unknown[], R>( |
| 16 |
...sourcesAndResultSelector: [...ObservableInputTuple<A>, (...values: A) => R] |
| 17 |
): Observable<R>; |
| 18 |
|
| 19 |
/** |
| 20 |
* Combines multiple Observables to create an Observable whose values are calculated from the values, in order, of each |
| 21 |
* of its input Observables. |
| 22 |
* |
| 23 |
* If the last parameter is a function, this function is used to compute the created value from the input values. |
| 24 |
* Otherwise, an array of the input values is returned. |
| 25 |
* |
| 26 |
* ## Example |
| 27 |
* |
| 28 |
* Combine age and name from different sources |
| 29 |
* |
| 30 |
* ```ts |
| 31 |
* import { of, zip, map } from 'rxjs'; |
| 32 |
* |
| 33 |
* const age$ = of(27, 25, 29); |
| 34 |
* const name$ = of('Foo', 'Bar', 'Beer'); |
| 35 |
* const isDev$ = of(true, true, false); |
| 36 |
* |
| 37 |
* zip(age$, name$, isDev$).pipe( |
| 38 |
* map(([age, name, isDev]) => ({ age, name, isDev })) |
| 39 |
* ) |
| 40 |
* .subscribe(x => console.log(x)); |
| 41 |
* |
| 42 |
* // Outputs |
| 43 |
* // { age: 27, name: 'Foo', isDev: true } |
| 44 |
* // { age: 25, name: 'Bar', isDev: true } |
| 45 |
* // { age: 29, name: 'Beer', isDev: false } |
| 46 |
* ``` |
| 47 |
* |
| 48 |
* @param args Any number of `ObservableInput`s provided either as an array or as an object |
| 49 |
* to combine with each other. |
| 50 |
* @return An Observable of array values of the values emitted at the same index from each |
| 51 |
* individual `ObservableInput`. |
| 52 |
*/ |
| 53 |
export function zip(...args: unknown[]): Observable<unknown> { |
| 54 |
const resultSelector = popResultSelector(args); |
| 55 |
|
| 56 |
const sources = argsOrArgArray(args) as Observable<unknown>[]; |
| 57 |
|
| 58 |
return sources.length |
| 59 |
? new Observable<unknown[]>((subscriber) => { |
| 60 |
// A collection of buffers of values from each source. |
| 61 |
// Keyed by the same index with which the sources were passed in. |
| 62 |
let buffers: unknown[][] = sources.map(() => []); |
| 63 |
|
| 64 |
// An array of flags of whether or not the sources have completed. |
| 65 |
// This is used to check to see if we should complete the result. |
| 66 |
// Keyed by the same index with which the sources were passed in. |
| 67 |
let completed = sources.map(() => false); |
| 68 |
|
| 69 |
// When everything is done, release the arrays above. |
| 70 |
subscriber.add(() => { |
| 71 |
buffers = completed = null!; |
| 72 |
}); |
| 73 |
|
| 74 |
// Loop over our sources and subscribe to each one. The index `i` is |
| 75 |
// especially important here, because we use it in closures below to |
| 76 |
// access the related buffers and completion properties |
| 77 |
for (let sourceIndex = 0; !subscriber.closed && sourceIndex < sources.length; sourceIndex++) { |
| 78 |
innerFrom(sources[sourceIndex]).subscribe( |
| 79 |
createOperatorSubscriber( |
| 80 |
subscriber, |
| 81 |
(value) => { |
| 82 |
buffers[sourceIndex].push(value); |
| 83 |
// if every buffer has at least one value in it, then we |
| 84 |
// can shift out the oldest value from each buffer and emit |
| 85 |
// them as an array. |
| 86 |
if (buffers.every((buffer) => buffer.length)) { |
| 87 |
const result: any = buffers.map((buffer) => buffer.shift()!); |
| 88 |
// Emit the array. If theres' a result selector, use that. |
| 89 |
subscriber.next(resultSelector ? resultSelector(...result) : result); |
| 90 |
// If any one of the sources is both complete and has an empty buffer |
| 91 |
// then we complete the result. This is because we cannot possibly have |
| 92 |
// any more values to zip together. |
| 93 |
if (buffers.some((buffer, i) => !buffer.length && completed[i])) { |
| 94 |
subscriber.complete(); |
| 95 |
} |
| 96 |
} |
| 97 |
}, |
| 98 |
() => { |
| 99 |
// This source completed. Mark it as complete so we can check it later |
| 100 |
// if we have to. |
| 101 |
completed[sourceIndex] = true; |
| 102 |
// But, if this complete source has nothing in its buffer, then we |
| 103 |
// can complete the result, because we can't possibly have any more |
| 104 |
// values from this to zip together with the other values. |
| 105 |
!buffers[sourceIndex].length && subscriber.complete(); |
| 106 |
} |
| 107 |
) |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
// When everything is done, release the arrays above. |
| 112 |
return () => { |
| 113 |
buffers = completed = null!; |
| 114 |
}; |
| 115 |
}) |
| 116 |
: EMPTY; |
| 117 |
} |