import type { DisposableResource } from "../resource.ts"; import { type TimeoutScheduler } from "./deferredAction.ts"; /** Delivers the latest pending update when a count or waiting limit is reached. */ export interface CoalescingNotifier extends DisposableResource { /** Records the latest value. Calls after disposal do nothing. */ update(value: T): void; /** Delivers a pending value immediately; does nothing when none is pending. */ flush(): void; /** Cancels the pending notification and permanently stops accepting updates. */ dispose(): void; } /** Count and waiting limits for one independently owned notifier. */ export interface CoalescingNotifierOptions { /** * Receives the latest value synchronously and has a `void` callback contract. * Synchronous throws propagate from `update` or `flush`, or through the * scheduler's invocation of a delayed callback. A returned promise is neither * awaited nor returned, and its rejection is not handled; handle rejections * inside the callback. */ readonly onNotify: (value: T) => void; /** Positive integer increase since the last notification; omitted for time-only notification. */ readonly everyCount?: number; /** Maximum wait from the first pending update. Later updates do not postpone it. */ readonly maxWaitMs?: number; /** Extracts a cumulative, non-decreasing count. Without it, each update counts as one. */ readonly getCount?: (value: T) => number; /** Delivers the first update immediately. Defaults to false. */ readonly leading?: boolean; /** Injects the timer boundary; the default uses the current global timer methods. */ readonly scheduleTimeout?: TimeoutScheduler; } /** * Coalesces updates until either their count increase or their waiting limit * reaches its threshold. At least one limit must be provided. The count starts * at zero; a notification establishes the next count baseline. * * Values are retained by reference, so callers wanting snapshots should pass * fresh values. No timer runs without a pending update. Before invoking onNotify, * flush clears the pending value, records its count as the next baseline, and * cancels the timer, allowing reentrant updates. Synchronous callback and * scheduler errors propagate and are not retried. A timer-triggered callback * error propagates through the scheduler's invocation, not the update that * scheduled it. An onNotify promise is neither awaited nor returned, and its * rejection must be handled inside the callback. Dispose discards pending work; * call flush first when a final update is wanted. * * @example * Connect coalesced updates to the value-less notifier: * * ```ts * import { createCoalescingNotifier } from "octagonal-wheels/concurrency/coalescingNotifier"; * import { createNotifier } from "octagonal-wheels/concurrency/notifier"; * * const signal = createNotifier(); * const progress = createCoalescingNotifier({ * everyCount: 10, * onNotify: signal.notify, * }); * ``` * * @param options - Count, timing, and callback options for this notifier. * @returns An independently owned notifier for coalescing update callbacks. */ export declare function createCoalescingNotifier(options: CoalescingNotifierOptions): CoalescingNotifier;