-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathconcatMapEager.ts
86 lines (81 loc) · 2.25 KB
/
concatMapEager.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* @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 {
defer,
EMPTY,
from,
ObservableInput,
ObservedValueOf,
OperatorFunction,
} from "rxjs";
import { map, materialize, mergeMap } from "rxjs/operators";
export function concatMapEager<T, O extends ObservableInput<any>>(
project: (value: T, index: number) => O,
concurrency?: number
): OperatorFunction<T, ObservedValueOf<O>> {
type R = ObservedValueOf<O>;
type Inner = {
complete: boolean;
index: number;
values: R[];
};
return (source) =>
defer(() => {
let activeIndex = 0;
const innersByIndex = new Map<number, Inner>();
function flush() {
const values: R[] = [];
let activeInner = innersByIndex.get(activeIndex);
while (activeInner) {
values.push(...activeInner.values);
activeInner.values.length = 0;
if (activeInner.complete) {
innersByIndex.delete(activeIndex);
activeInner = innersByIndex.get(++activeIndex);
} else {
break;
}
}
return values;
}
return source.pipe(
mergeMap(
(value, index) =>
from(project(value, index)).pipe(
/*tslint:disable-next-line:rxjs-no-explicit-generics*/
materialize<R>(),
map((notification) => ({
index,
notification,
}))
),
concurrency
),
mergeMap(({ index, notification }) => {
let inner = innersByIndex.get(index);
if (!inner) {
inner = { complete: false, index, values: [] };
innersByIndex.set(index, inner);
}
switch (notification.kind) {
case "N":
inner.values.push(notification.value!);
break;
case "C":
inner.complete = true;
break;
case "E":
return notification.toObservable();
default:
break;
}
if (inner.index !== activeIndex) {
return EMPTY;
}
return flush();
})
);
});
}