/**
* internal/timing/debounce — a timer-based call transformer that coalesces a burst
* of calls into a single fire.
*
* Sibling of `../throttle` — the two share a SYMMETRIC surface
* (`Debounced` / `Throttled`, both callable + `.cancel()` + `.flush()`,
* both `{ leading?, trailing? }`) so they read as a pair. They are kept in
* separate files with NO cross-import: each is a self-contained timer, and the
* coalesce-vs-drop policies are different enough that sharing code would hide
* the distinction. Read one to understand the other; do not couple them.
*
* Layering with `../throttled-input`: `debounce` and `throttle` are
* the two host-boundary PRIMITIVES — two distinct algorithms (collapse-a-burst
* vs at-most-once-per-window) behind one signature shape. `throttled-input` is
* NOT a third primitive: it is a TEA machine composed OVER them that
* re-exports both (see its module doc for the full layering map).
*
* WHAT THIS IS NOT — and why it lives outside `update`:
*
* - NOT a reducer / Transitions cell. Reducers are PURE (invariant 2 — same
* inputs → same outputs, no `Date.now`, no timers). A debounce holds a live
* `setTimeout` handle and mutable pending-args state; folding it into
* `update` would smuggle a timer into the pure core.
* - NOT a Sub. A Sub is a continuous SOURCE of Msgs the substrate reconciles
* by id every transition (invariant 4). A debounce is a SINK transformer:
* it wraps a `void`-returning handler (the canonical one is
* `Runtime.dispatch`) so bursty HOST events coalesce BEFORE they ever
* become a Msg.
*
* It is a higher-order function at the host boundary (pattern 14 — TEA owns
* state, JS owns timing). The host attaches the wrapped function to a noisy
* event source (`scroll`, `resize`, `input`, a ResizeObserver) and the
* substrate downstream only ever sees the coalesced Msg:
*
* const onResized = debounce(
* () => runtime.dispatch({ _tag: "BrowserResizedWindow" }),
* 150,
* );
* window.addEventListener("resize", onResized);
* // cleanup: onResized.cancel(); window.removeEventListener("resize", onResized);
*
* Internal since #47 — not published on any subpath; reached from inside the
* package as `internal/timing/debounce`. Same one-shape-per-package rule as
* `rate-limit`, `deadline`, and `work-queue`.
*
* Uses the universal `setTimeout` / `clearTimeout` timer idiom (same discipline
* as `subs/from-timeout.ts`): exactly one live handle at a time, every arm
* clears the previous, every fire / cancel clears the handle and nulls it so a
* stale callback can never run.
*/
/**
* A debounced wrapper around `fn`. Callable with the SAME argument tuple as
* `fn` — `(...args: A) => void` — plus two control methods that share names and
* shapes with `Throttled` so the two modules read as a symmetric pair.
*/
interface Debounced {
/** Schedule (or re-schedule) a fire of the wrapped `fn`. See `debounce`. */
(...args: A): void;
/**
* Drop any pending trailing fire WITHOUT invoking `fn`. Clears the timer and
* forgets the captured args. Idempotent — calling it with nothing pending is
* a no-op. The leading-edge latch (if `leading` is enabled) also resets, so
* the next call after `cancel` is treated as a fresh burst.
*
* The host-cleanup partner of `removeEventListener`: cancel the pending fire
* when the component unmounts / the listener detaches, so a queued
* `dispatch` can't land after teardown.
*/
cancel(): void;
/**
* Fire any pending trailing call IMMEDIATELY with its captured args, then
* clear the timer. No-op when nothing is pending. Use to force the last
* coalesced call out early — e.g. flush a debounced save on `beforeunload`,
* or flush a debounced search when the user presses Enter.
*/
flush(): void;
}
/**
* Wrap `fn` so a BURST of calls collapses to a single invocation.
*
* Trailing edge (the default): each call (re)starts a `ms` timer; `fn` fires
* once with the LAST args after `ms` of quiet. Every call within `ms` of the
* previous resets the timer — the classic "wait until the user stops typing"
* behavior.
*
* @param fn The handler to coalesce. Typically `(...) => runtime.dispatch(...)`,
* but any `(...args: A) => void` works. The return value is
* ignored — debounce is a fire transformer, not a value pipe.
* @param ms Quiet window in milliseconds. Calls closer together than `ms`
* coalesce; a gap of at least `ms` lets the trailing fire through.
* @param opts.leading Fire on the FIRST call of a burst (the leading edge),
* with that first call's args. Default `false`.
* @param opts.trailing Fire on the trailing edge with the LAST call's args.
* Default `true`.
*
* Edge combinations (matching lodash's settled semantics, the de-facto
* standard the ecosystem rediscovered):
*
* - `{ trailing: true }` (default): one fire, after the burst, last args.
* - `{ leading: true, trailing: false }`: one fire, at the burst START,
* first args. Subsequent calls within the window are swallowed; the timer
* only re-opens the "can lead again" latch after `ms` of quiet.
* - `{ leading: true, trailing: true }`: fires at the start AND end of a
* burst — BUT the trailing fire is SUPPRESSED for a burst of exactly ONE
* call (the leading fire already covered it, so a lone call doesn't
* double-fire). This matches lodash and is the behavior tests pin.
* - `{ leading: false, trailing: false }`: never fires. Degenerate but legal;
* we don't throw — the caller asked for a no-op transformer.
*
* WHY a default of trailing-only: a debounce's whole job is "act after the
* activity settles." The trailing edge IS that semantic — fire with the final
* state of the burst (the last keystroke, the final scroll position). Leading
* is the opt-in for "respond instantly, then go quiet."
*/
declare function debounce(fn: (...args: A) => void, ms: number, opts?: {
leading?: boolean;
trailing?: boolean;
}): Debounced;
export { type Debounced as D, debounce as d };