fix: reset pending state after leading-only debouncer execution #234
Open
Don-Erfan(GitHub profile, opens in a new tab)wants to merge 2 commits into
main fromrefs/pull/234/head·opened 🎯 Changes#
With leading: true and trailing: true, a single call to maybeExecute executed on the leading edge but still set isPending: true and stored lastArgs. The trailing timeout correctly skips calls already handled by the leading edge, so nothing ever reset that state: status stayed 'pending' forever, and a later flush() re-executed the function with stale, already-executed arguments.
Fix
- Only set
isPending/lastArgsfor calls that were not consumed by the leading edge, so the debouncer returns to'idle'after a single execution. - Applied the same fix to
LiteDebouncer, which storedlastArgsunconditionally and could therefore re-execute a leading-edge call viaflush().
Tests
- Added coverage for leading + trailing execution,
flush()behavior after a leading-edge call, pending state, and idle transitions.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested this code locally with
pnpm run test:pr.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Summary by CodeRabbit#
Bug Fixes
- Prevented debounced actions configured for leading and trailing execution from being invoked twice with the same arguments.
- Improved
flush()behavior so it no longer re-executes completed leading-edge calls. - Corrected pending-state and idle-status handling after a single debounced execution.
Tests
- Added coverage for leading/trailing execution,
flush(), pending state, and idle transitions.
- Added coverage for leading/trailing execution,
- 1
Note
GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.
Sign in to join this conversation.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID:
6407c534-35fd-48dc-8ad3-b63667e82198📥 Commits
Reviewing files that changed from the base of the PR and between 5bea64ffc5861ffe16ed99d00755a32f5005c578 and 2b2cd3fe15f14f056f6633eadceeb71cb9446e2f.
📒 Files selected for processing (1)
.changeset/leading-edge-pending-reset.mdIncluded review availability: Your plan includes up to 4 reviews per rolling hour; 3 remain after this review.
📝 Walkthrough
Walkthrough#
Both debouncer implementations now prevent leading-edge calls from running again through trailing execution or
flush(). Tests cover duplicate prevention, cleared arguments, pending state, and idle status. A changeset records patch releases for both packages.Changes#
Debouncer leading-edge behavior
packages/pacer/src/debouncer.ts,packages/pacer-lite/src/lite-debouncer.ts,packages/pacer/tests/debouncer.ts,packages/pacer-lite/tests/lite-debouncer.test.ts,.changeset/leading-edge-pending-reset.mdflush()does not invoke them again and that the debouncer clears arguments and returns toidle. The changeset records patch releases for both packages.Estimated code review effort: 2 (Simple) | ~10 minutes
Merge Risk: ⚪ Minimal · up to
2b2cdThis change resets debouncer state correctly after a leading-edge execution and prevents stale re-execution through flush; no actionable merge-blocking risk remains beyond normal checks and review.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share