execute handler at the right time. Use Debounce jobs when many events arrive in rapid succession but you only want to process them once: webhook coalescing, search-as-you-type, and update batching.
Defining a debounce job
Configuration
Effect Schema for validating incoming events. Each event added via
client.debounce(...).add() is validated against this schema.Handler called when the batch flushes. Receives the accumulated state and flush metadata. Must return
Effect<void, E, never>.How long to wait after the first event before flushing. The timer resets if
maxEvents is not reached.Optional schema for the persisted state type. Defaults to
eventSchema, which causes the state to be overwritten with the latest event on each onEvent call.Optional maximum number of events to accumulate before flushing immediately, even if
flushAfter has not elapsed.Optional reducer called for each incoming event. Returns the new accumulated state. Default: replace state with the latest event.
Optional retry configuration for
execute failures. Events remain in the buffer during retry attempts.Control logging verbosity.
false (default) logs only errors. true enables debug-level logging.Flush triggers
A Debounce job flushes its accumulated events when any of the following conditions are met:flushReason | When |
|---|---|
"flushAfter" | The configured delay has elapsed since the first event arrived |
"maxEvents" | The number of accumulated events has reached maxEvents |
"manual" | client.debounce(...).flush() was called directly |
Execute context
| Property | Type | Description |
|---|---|---|
state | Effect<S> | Accumulated state. Yield to read. |
eventCount | Effect<number> | Total number of events received in this batch. |
flushReason | "maxEvents" | "flushAfter" | "manual" | What triggered this flush. |
debounceStartedAt | Effect<number> | Timestamp (ms) when the first event arrived. |
attempt | number | Current retry attempt (1 = first try). |
isRetry | boolean | Whether this flush is a retry. |
onEvent context
The onEvent handler receives a context with the incoming event and current accumulated state.
| Property | Type | Description |
|---|---|---|
event | I | The incoming event (already validated). |
state | S | Current accumulated state before this event. |
eventCount | number | Number of events received so far (before this one). |
instanceId | string | Durable Object instance ID. |
On the first event,
state is initialized to the event value itself when no stateSchema is provided. If you provide a custom stateSchema, ensure your onEvent handler handles the initial state correctly.