-
Notifications
You must be signed in to change notification settings - Fork 0
/
page-caches.spec.ts
60 lines (57 loc) · 1.98 KB
/
page-caches.spec.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
import { sleep } from "bun";
import Keyv from "keyv";
import { logs, maps, pageFlow } from "./index";
it("caches stream", async () => {
const pageData1 = [
[4, 3],
[2, 1],
];
const pageData2 = [
[6, 5],
[4, 3],
[2, 1],
];
const store = new Keyv<any>();
const fetcher1 = jest.fn((page) => pageData1[page]);
const ret1 = await pageFlow(0, (page: number) => {
const data = fetcher1(page);
return { data, next: data ? page + 1 : null };
})
.flat()
.byLazy(maps(async (e) => (await sleep(10), e)))
.byLazy(maps(async (e) => (await sleep(10), e)))
.byLazy(maps(async (e) => (await sleep(10), e)))
.byLazy(maps(async (e) => (await sleep(10), e)))
.byLazy(maps(async (e) => (await sleep(10), e)))
.byLazy(maps(async (e) => (await sleep(10), e)))
.byLazy(maps(async (e) => (await sleep(10), e)))
.byLazy(maps(async (e) => (await sleep(10), e)))
.byLazy(maps(async (e) => (await sleep(10), e)))
.cacheTail(store, "cache")
.log()
.toArray();
expect(ret1).toEqual([4, 3, 2, 1]);
expect(fetcher1).toHaveBeenCalledTimes(3);
const fetcher2 = jest.fn((page) => pageData2[page]);
const ret2 = await pageFlow(0, (page: number) => {
const data = fetcher2(page);
return { data, next: data ? page + 1 : null };
})
.flat()
.byLazy(logs((e) => "head data: " + e))
.byLazy(maps(async (e) => (await sleep(10), e)))
.byLazy(maps(async (e) => (await sleep(10), e)))
.byLazy(maps(async (e) => (await sleep(10), e)))
.byLazy(maps(async (e) => (await sleep(10), e)))
.byLazy(maps(async (e) => (await sleep(10), e)))
.byLazy(maps(async (e) => (await sleep(10), e)))
.byLazy(maps(async (e) => (await sleep(10), e)))
.byLazy(maps(async (e) => (await sleep(10), e)))
.byLazy(maps(async (e) => (await sleep(10), e)))
.cacheTail(store, "cache")
.log()
.toArray();
expect(ret2).toEqual([6, 5, 4, 3, 2, 1]);
expect(fetcher2).toHaveBeenCalledTimes(2); // [6,5],
console.log("done");
});