import { b as Cmd, T as TimerDeps, S as Subscribe } from './core-sEWVgnpI.js'; import { D as DeadlineExceeded, a as DeadlineSub, b as DeadlinesSub } from './index-CCs-QGGu.js'; /** * internal/flow/batch-window — coalesce a stream of items into size- or * time-bounded BATCHES, then flush each batch as a single Cmd. * * What this module actually DELEGATES to, vs. what it merely RE-EXPORTS: * * - `../deadline` — REAL delegation (the only one). The window timer's Msg * is `deadlineExceeded`, keyed on the window's identity. `timerFor` arms it * through the engine's built-in `timer` Sub: `maxMs` counted from the * transition that opened the window, so a machine needs no runner. The * absolute-instant form is still here too — `subsFor` lists the same * deadline for `deadlinesSub` + `subscribeDeadline`, which re-arms for the * REMAINING time after a `Store` rehydrate rather than a fresh `maxMs`. * - `../work-queue` — TYPE re-export only, NOT a sink this module calls. A * flush emits a Cmd; the CONSUMER's interpret handler is what enqueues onto a * `createQueue(store)`. This module never touches queue status — it has no * queue state and no status flip to delegate. It re-exports `EnqueueInput` * purely so the consumer's flush Cmd and queue agree on the item shape; any * actual status flip belongs in the consumer and is delegated to * `../work-queue/ops` there, not here. * - `../debounce` — VALUE/TYPE re-export only, NOT wired into `update`. A * bursty source (keystrokes, scroll, websocket frames) can be `debounce`d at * the host BEFORE it ever becomes an `add` Msg, so the batch window only sees * settled items. `debounce` is a sink transformer OUTSIDE `update`; * batch-window is the durable, replayable half inside it. Re-exported for * one-import ergonomics — this module does not call it. * * Why this is a knob and not hand-wired logic: the managed state (the buffer + * the window-open instant) lives in the Model, so it survives DO eviction / * reload (durable) and every transition is still a Msg through `update` * (replayable). A closure holding a `setTimeout` and a mutable array would break * both — banned. `add` / `onWindow` are PURE: time arrives as an `at` * parameter, never `Date.now()`; they return NEW slice values, never mutate. * * Typical wiring (combinator form): * * const bw = createBatchWindow({ * maxItems: 100, * maxMs: 5_000, * flush: (items) => ({ _tag: "FlushBatch", items }), * }); * * // Model: { batch: BatchWindow, ... } * // init: (loaded) => [loaded ?? { batch: bw.init(), ... }, []] * // update cells: * // LogArrived: (s, m) => mapBatch(s, bw.add(s.batch, m.line, m.at)) * // deadline_exceeded: (s, m) => mapBatch(s, bw.onWindow(s.batch, m.atMs)) * // subs: [{ _tag: "timer", deps: (s: Model) => bw.timer(s.batch) }] * // at run: run(machine, { interpret }) // `timer` is built in * * where `mapBatch(s, [batch, cmds]) => [{ ...s, batch }, cmds]` lifts the slice * transition into the consumer's Model. The `FlushBatch` Cmd's interpret handler * is the consumer's — it is where the batch meets I/O (an `enqueue`, a network * POST, an R2 write). * * Internal since #48 — not published on any subpath; reached from inside the * package as `internal/flow/batch-window`. Same one-shape-per-package rule as `deadline`, `cache`, and * `work-queue`. */ /** * Configuration for a batch window — the knob. Pure config, no mutable state * (the `BatchWindow` slice carries that). One config object is shared across * every window of one logical stream (often a module-scope const). * * `flush` is a pure Cmd CONSTRUCTOR, not a closure-with-state: it maps the * buffered items to the single Cmd the runtime performs when a window closes. * Keeping it a `(items) => Cmd` (not a `(items) => void`) is what keeps the * trigger replayable — the flush is a Cmd in the transition's output, never a * side effect inside the verb. */ interface BatchWindowConfig { /** * Size trigger. A batch flushes the instant its buffer reaches `maxItems`, * WITHOUT waiting for the time window — the classic "send as soon as the * page is full" cutoff. Must be `>= 1`; a config of `maxItems: 0` would flush * an empty batch on every `add`, which is never useful, so callers treat `1` * as the floor (the verb still behaves: with `maxItems <= 1` every item * flushes on arrival, a degenerate but legal "no batching" window). */ readonly maxItems: number; /** * Time trigger, in milliseconds. A batch flushes no later than `openedAt + * maxMs` — `openedAt` being the `at` of the item that opened the window. This * is the latency ceiling: an item never waits longer than `maxMs` to ship, * even if the buffer never reaches `maxItems`. */ readonly maxMs: number; /** * Map a flushed batch to the single Cmd the runtime performs. Called with the * buffered items in arrival order; the returned Cmd is what `add` / `onWindow` * emit when a window closes. PURE constructor — no I/O, no clock; the I/O * happens later in the consumer's interpret handler for the returned Cmd. */ readonly flush: (items: readonly I[]) => C; } /** * The Model slice a batch window owns. `buffer` is the items accumulated for the * current open window; `openedAt` is the `at` of the item that OPENED it (the * anchor the time trigger measures against), or `null` when the window is * closed (buffer empty). The pair is the single source of truth for "is a * window open, and when must it flush by?" — both plain data so the whole slice * is trivially JSON-serializable into a consumer's `Store`. * * Invariant the verbs preserve: `buffer.length === 0` IFF `openedAt === null`. * A non-empty buffer always has an `openedAt`; an empty buffer never does. The * window timer Sub keys its arm/disarm off exactly this invariant. */ interface BatchWindow { readonly buffer: readonly I[]; readonly openedAt: number | null; } /** The starting slice: closed window, empty buffer. */ declare function initBatchWindow(): BatchWindow; /** * The deadline a batch window's open timer lists: it IS `../deadline`'s * `DeadlineSub`, not a re-tagged copy. `atMs` is the absolute flush-by instant * (`openedAt + maxMs`). Aliasing the deadline type (rather than redeclaring an * identical shape) is what lets `subsFor` return `deadlineSub(...)` with no * cast — the value's static type already satisfies this name. Consumers running * several windows route each one by the deadline's `id`, not by a distinct type. */ type BatchWindowSub = DeadlineSub; /** The Msg a closed-by-time window dispatches. Alias of `deadline`'s Msg. */ type BatchWindowExpired = DeadlineExceeded; /** * Construct the window-expired Msg for the window identified by `id`, flushing * by `atMs`. Re-exported alias of `deadlineExceeded` so the runner and * the consumer's reducer share one constructor rather than two literals that * can drift. The consumer handles it in a reducer cell by calling `onWindow`. */ declare function batchWindowExpired(id: string, atMs: number): BatchWindowExpired; /** * The bound knob returned by `createBatchWindow`. `init` + the two pure verbs + * `timer` + `subs`, all closing over one `BatchWindowConfig`. The verbs * return `readonly [BatchWindow, readonly C[]]`: the next slice and the Cmds * to emit (one `flush` Cmd when a window closes, none otherwise). */ interface BatchWindowKnob { /** The starting slice — `initBatchWindow()`. */ init(): BatchWindow; /** * Buffer `item` (arrival-stamped `at`), flushing immediately if that brings * the buffer to `maxItems`. See `addItem`. */ add(state: BatchWindow, item: I, at: number): readonly [BatchWindow, readonly C[]]; /** * Flush whatever is buffered because the time window closed. See `onWindow`. */ onWindow(state: BatchWindow, at: number): readonly [BatchWindow, readonly C[]]; /** * The built-in `timer` Sub's deps while a window is open, `null` while it is * closed. See `timerFor`. `id` keys the Msg so several windows on one * machine route distinctly. */ timer(state: BatchWindow, id?: string): TimerDeps | null; /** * The window timer as an absolute deadline, listed only while a window is * open — for `deadlinesSub` + `subscribeBatchWindow`. See `subsFor`. */ subs(state: BatchWindow, id?: string): readonly BatchWindowSub[]; } /** * Buffer `item` and decide whether the size trigger fires. PURE. * * - Empty (closed) window → `item` OPENS it: `openedAt` becomes `at`, buffer * becomes `[item]`. This is the anchor the time window measures against. * - Open window → `item` appends; `openedAt` is left untouched (the window's * latency clock keeps running from the FIRST item, not this one). * * After buffering, if the buffer has reached `maxItems` the window flushes * immediately: emit `config.flush(buffer)` and RESET to the closed slice. The * size trigger pre-empts the time trigger — a full page ships now, the window * Sub for the (now closed) window disarms on the next reconcile because `subs` * returns `[]` for an empty buffer. * * `maxItems <= 1` is the degenerate "no batching" case: every item flushes on * arrival (buffer reaches 1 immediately). Legal and useful as a feature flag to * disable batching without restructuring the wiring. * * @param config the knob (maxItems / flush constructor) * @param state the current slice * @param item the item to buffer * @param at the arrival instant (epoch ms) — the caller stamps it; the verb * never reads the clock */ declare function addItem(config: BatchWindowConfig, state: BatchWindow, item: I, at: number): readonly [BatchWindow, readonly C[]]; /** * Flush the open window because its time bound was reached. PURE. * * - Open window (buffer non-empty) → emit `config.flush(buffer)` and RESET to * the closed slice. * - Closed window (buffer empty) → no-op: return the same slice and no Cmds. * This guards the race where the size trigger already flushed and reset the * window before the time Sub's Msg lands — a stale `onWindow` after a * size-flush must NOT emit an empty batch. * * `at` is accepted for symmetry with `add` and so the consumer's reducer cell * has a uniform `(state, at)` shape; the verb doesn't need it for the flush * decision (the buffer's emptiness is the whole signal), but taking it keeps a * future "flush only if the window is genuinely past `openedAt + maxMs`" * tightening a non-breaking change. * * @param config the knob (flush constructor) * @param state the current slice * @param at the firing instant (epoch ms) — the caller stamps it; the verb * never reads the clock */ declare function onWindow(config: BatchWindowConfig, state: BatchWindow, _at: number): readonly [BatchWindow, readonly C[]]; /** * The window timer deadline, derived from the slice. PURE. * * Returns the single deadline targeting `openedAt + maxMs` while a window is * open (buffer non-empty), or `[]` while it is closed. Fed through * `deadlinesSub`, the engine arms the timer when the list gains the deadline * and disarms it (clearing the pending `setTimeout`) when the list loses it — * so a size-flush that empties the buffer auto-cancels the now-irrelevant time * trigger on the next transition. * * `id` defaults to `"batch-window"`; pass a distinct id per window when a * machine runs several so the dispatched `BatchWindowExpired` Msg routes to the * right one. * * @param config the knob (maxMs) * @param state the current slice * @param id stable identity for this window's timer (default * `"batch-window"`) */ declare function subsFor(config: BatchWindowConfig, state: BatchWindow, id?: string): readonly BatchWindowSub[]; /** * The built-in `timer` Sub's deps for a batch window. PURE. * * While a window is open it counts `maxMs` down from the transition that * opened it — the deps are fixed for the window's life (`openedAt` never moves * while items append), so the engine starts the countdown exactly once — and * fires `batchWindowExpired(id, openedAt + maxMs)`. A closed window is `null`, * so a size-flush that empties the buffer cancels the countdown on the next * transition. * * Relative by construction: a process that rehydrates an open window counts a * fresh `maxMs`. Reach for `subsFor` + `subscribeBatchWindow` when the flush * must land at the original absolute instant. */ declare function timerFor(config: BatchWindowConfig, state: BatchWindow, id?: string): TimerDeps | null; /** * The `deadline` runner for a batch window's absolute timer — the exact * `../deadline` runner. Arms a one-shot timer per listed deadline for * `max(0, atMs - now)` and dispatches `batchWindowExpired(...)`; returns a * cleanup that clears them. Hand it to `run` as `subscribe.deadline` when the * machine declares `deadlinesSub((s) => bw.subs(s.batch))`. */ declare const subscribeBatchWindow: Subscribe["deadline"]; /** * Build a batch window knob from a config. Combinator form (the default L2 * ergonomic): hand it the config, splice the ~4 returned hooks into your * machine. The pure verbs (`add` / `onWindow`) close over the config so the * consumer's reducer cells stay a uniform `(state, ...args)` shape; `timer` * arms the window timer. * * @typeParam I the item type buffered into each batch * @typeParam C the Cmd type `config.flush` returns */ declare function createBatchWindow(config: BatchWindowConfig): BatchWindowKnob; export { type BatchWindow as B, type BatchWindowSub as a, type BatchWindowExpired as b, type BatchWindowConfig as c, type BatchWindowKnob as d, addItem as e, batchWindowExpired as f, createBatchWindow as g, subscribeBatchWindow as h, initBatchWindow as i, onWindow as o, subsFor as s, timerFor as t };