repos / starfx

supercharged async flow control library.
git clone https://github.com/neurosnap/starfx.git

starfx / test
Eric Bower · 04 Mar 24

batch.test.ts

 1import { describe, expect, it } from "../test.ts";
 2import {
 3  createBatchMdw,
 4  createSchema,
 5  createStore,
 6  slice,
 7} from "../store/mod.ts";
 8import { parallel } from "../mod.ts";
 9
10const batch = describe("batch mdw");
11
12it(batch, "should batch notify subscribers based on mdw", async () => {
13  const [schema, initialState] = createSchema({
14    cache: slice.table({ empty: {} }),
15    loaders: slice.loaders(),
16  });
17  const store = createStore({
18    initialState,
19    middleware: [createBatchMdw(queueMicrotask)],
20  });
21  let counter = 0;
22  store.subscribe(() => {
23    counter += 1;
24  });
25  await store.run(function* () {
26    const group: any = yield* parallel([
27      schema.update(schema.cache.add({ "1": "one" })),
28      schema.update(schema.cache.add({ "2": "two" })),
29      schema.update(schema.cache.add({ "3": "three" })),
30      schema.update(schema.cache.add({ "4": "four" })),
31      schema.update(schema.cache.add({ "5": "five" })),
32      schema.update(schema.cache.add({ "6": "six" })),
33    ]);
34    yield* group;
35    // make sure it will still notify subscribers after batched round
36    yield* schema.update(schema.cache.add({ "7": "seven" }));
37  });
38  expect(counter).toBe(2);
39});