Coalesce into a digest
Collapse a burst of events into one handler run per window. The handler fires at the end of the window with the latest state.
Use this to
- Send one Slack summary per minute instead of one message per event in a busy channel
- Batch incoming webhook events into a single handler call per 60-second window
- Rebuild a search index at most once per minute even during a bulk import
- Push aggregated metrics to a dashboard every 30 seconds instead of per-write
Code
Throttle on each event
// fires once per minute with the latest state — every call // inside the window coalesces into the next handler run await dk.throttle("digest", { key: "chan_42", wait: "1m", });
When the time comes, send the digest
dk.handle("digest", async ({ key }) => { // runs once per window with whatever state is current const events = await db.events.unread(key); await sendDigest(key, events); await db.events.markRead(key, events); });
npm install delaykit