Debounce a flurry

Collapse fifty events into one action. Durable across restarts.

Use this to
  • Rebuild a search index for a document after an editor stops making changes
  • Send one Slack alert per incident instead of one per failing request
  • Regenerate a product feed after a batch of catalog updates settles
  • Recompute dashboard stats after a burst of order imports finishes
Code
Debounce on each event
// called once per edit — only fires after silence
await dk.debounce("reindex", {
  key: "doc_789",
  wait: "5s",
});
Cap the wait with maxWait
// fires at most every 60s, even if edits keep arriving
await dk.debounce("reindex", {
  key: "doc_789",
  wait: "5s",
  maxWait: "60s",
});
When the time comes, rebuild the index
dk.handle("reindex", async ({ key }) => {
  await searchIndex.rebuild(key);
});
npm install delaykit