repos / starfx

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

starfx / store
Eric Bower · 19 Jan 24

batch.ts

 1import { action } from "../deps.ts";
 2import { UpdaterCtx } from "./types.ts";
 3import { AnyState, Next } from "../types.ts";
 4
 5export function createBatchMdw<S extends AnyState>(
 6  queue: (send: () => void) => void,
 7) {
 8  let notifying = false;
 9  return function* batchMdw(_: UpdaterCtx<S>, next: Next) {
10    if (!notifying) {
11      notifying = true;
12      yield* action<void>(function* (resolve) {
13        queue(() => {
14          notifying = false;
15          resolve();
16        });
17      });
18      yield* next();
19    }
20  };
21}