sflow
Version:
sflow is a powerful and highly-extensible library designed for processing and manipulating streams of data effortlessly. Inspired by the functional programming paradigm, it provides a rich set of utilities for transforming streams, including chunking, fil
61 lines (57 loc) • 2.04 kB
text/typescript
import Keyv from "keyv";
import { logs, maps, pageFlow } from "./index";
import { sleep } from "./utils";
it.skip("page caches stream", async () => {
const pageData1 = [
[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 pageData2 = [
[6, 5],
[4, 3],
[2, 1],
];
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");
});