-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathzipPadded.ts
45 lines (39 loc) · 1.16 KB
/
zipPadded.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* @license Use of this source code is governed by an MIT-style license that
* can be found in the LICENSE file at https://github.com/cartant/rxjs-etc
*/
import { concat, ConnectableObservable, merge, Observable, zip } from "rxjs";
import {
distinctUntilChanged,
map,
mapTo,
publish,
scan,
} from "rxjs/operators";
export function zipPadded<T>(
sources: Observable<T>[],
padValue?: any
): Observable<T[]> {
return new Observable<T[]>((observer) => {
const publishedSources = sources.map(
(source) => source.pipe(publish()) as ConnectableObservable<T>
);
const indices = merge(
...publishedSources.map((source) =>
source.pipe(map((unused, index) => index))
)
).pipe(
scan((max, index) => Math.max(max, index), 0),
distinctUntilChanged(),
publish()
) as ConnectableObservable<number>;
const subscription = zip(
...publishedSources.map((source) =>
concat(source, indices.pipe(mapTo(padValue)))
)
).subscribe(observer);
subscription.add(indices.connect());
publishedSources.forEach((source) => subscription.add(source.connect()));
return subscription;
});
}