-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution.ts
57 lines (53 loc) · 1.6 KB
/
solution.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
/*
------------------------------->
TIME 00000000001111111111
01234567890123456789
------------------------------->
hashes: a
b
c d e f g h
--------------------------------
a : |x | | | | |
b : |x | | | | |
c : |-----x | | | |
d : |x | | | |
e : |x| | |
f : |x | |
g : |---x
h : |x
------------------------------->
TIME 00000000001111111111
01234567890123456789
------------------------------->
*/
import { endWith, exhaustMap, map, mergeMap, switchMap, takeWhile } from "rxjs"
import { concatMapEager } from "rxjs-etc/dist/cjs/operators/index.js"
import type { TestFn } from "@/types.js"
import { suite } from "@/api.js"
import { Merger, fromAbortControllerFn, fromCbCreator } from "./helpers.js"
const solveItWith =
(merger: Merger): TestFn =>
({ listenToHashes, getBlockNumber, getBlockTime, output }) => {
const getBlockTime$ = fromAbortControllerFn(getBlockTime)
fromCbCreator(listenToHashes)
.pipe(
takeWhile((hash) => hash !== "h", true),
merger((hash) =>
getBlockTime$(hash).pipe(
map((blockTime) => ({
hash,
blockTime,
blockNumber: getBlockNumber(hash),
})),
),
),
endWith(null),
)
.subscribe(output)
}
export default suite("rxjs", {
asTheyCome: solveItWith(mergeMap),
ignoreNewOnesUntilResolved: solveItWith(exhaustMap),
allInOrder: solveItWith(concatMapEager),
newOneCancelsOldOne: solveItWith(switchMap),
})