Skip to content

fix: reset pending state after leading-only debouncer execution#234

Open
Don-Erfan wants to merge 1 commit into
TanStack:mainfrom
Don-Erfan:Fix/debouncer-leading-pending-state
Open

fix: reset pending state after leading-only debouncer execution#234
Don-Erfan wants to merge 1 commit into
TanStack:mainfrom
Don-Erfan:Fix/debouncer-leading-pending-state

Conversation

@Don-Erfan

@Don-Erfan Don-Erfan commented Jul 14, 2026

Copy link
Copy Markdown

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.

Only set isPending/lastArgs for calls not consumed by the leading edge. Apply the same fix to LiteDebouncer, which stored lastArgs unconditionally and could re-execute a leading-edge call via flush().

🎯 Changes

✅ 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

    • Fixed debounced actions configured for leading and trailing execution being invoked twice with the same arguments.
    • Improved flush() behavior so it no longer re-executes an already completed leading-edge call.
    • 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.

@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Updated both debouncer implementations so leading-edge calls are not re-executed by trailing execution or flush(). Added tests covering duplicate prevention, cleared arguments, pending state, and idle status.

Changes

Debouncer leading-edge behavior

Layer / File(s) Summary
Prevent duplicate trailing and flush execution
packages/pacer/src/debouncer.ts, packages/pacer-lite/src/lite-debouncer.ts, packages/pacer/tests/debouncer.test.ts, packages/pacer-lite/tests/lite-debouncer.test.ts
Leading-handled calls no longer retain arguments or enter pending trailing state. Tests verify that flush() does not invoke them again and that the debouncer clears arguments and returns to idle.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly summarizes the main debouncer behavior fix.
Description check ✅ Passed The description covers the bug, fix, checklist, and release impact, though the Changes section is mostly placeholder text.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant