fix(async-queuer): keep pendingTick true during wait period #189
main fromrefs/pull/189/head·opened 🎯 Changes#
Fixes #188
When addItem() is called on a running AsyncQueuer during the wait period between item processing, it bypasses the configured wait delay and triggers immediate processing.
Root cause: In #tick(), pendingTick is set to false synchronously at the end of the method, even when async work (execute + wait timer) has been scheduled. Since addItem() checks isRunning && !pendingTick to decide whether to trigger #tick(), it incorrectly starts immediate processing during the wait period.
Fix: Track whether async work was scheduled in the while loop with a scheduledAsyncWork flag. Only clear pendingTick when no async work is pending. When async work is scheduled, pendingTick stays true until the next #tick() call (after execute() completes and the wait timer fires), preventing addItem() from triggering a premature tick.
Test added: Verifies that items added via addItem() during the wait period are not processed until the wait completes.
✅ 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
- Corrected queuer behavior so items added during an active wait window do not bypass the configured wait delay.
- Tests
- Added test coverage verifying wait-period behavior when items are enqueued during processing.
- Documentation
- Updated reference docs’ source-location pointers for queuer-related entries.
- Chores
- Added release metadata for a patch update.
- 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/pacer/src/async-queuer.ts (1)
448-465: ⚠️ Potential issue | 🔴 CriticalAdd error recovery to async work scheduling in
#tickWhen
await this.execute()throws (e.g.,throwOnError: true), the async branch exits before scheduling the next#tick()or wait timeout. SincescheduledAsyncWorkis alreadytrue,pendingTickis never cleared at line 464, leaving it stucktrueand preventingaddItem()from retriggering processing (line 542 check:isRunning && !pendingTickstays false). This can permanently freeze the queue when errors occur duringstart().🛠️ Proposed fix
scheduledAsyncWork = true ;(async () => { - await this.execute() - - const wait = this.#getWait() - if (wait > 0) { - const timeoutId = setTimeout(() => this.#tick(), wait) - this.#timeoutIds.add(timeoutId) - return - } - - this.#tick() + try { + await this.execute() + } finally { + const wait = this.#getWait() + if (wait > 0) { + const timeoutId = setTimeout(() => this.#tick(), wait) + this.#timeoutIds.add(timeoutId) + return + } + + this.#tick() + } })() }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/pacer/src/async-queuer.ts` around lines 448 - 465, The async IIFE in `#tick` can exit early when await this.execute() throws, leaving the local scheduledAsyncWork flag true and pendingTick stuck; wrap the call to this.execute() in try/catch (or use try/finally) so that errors do not skip the scheduling logic: on both success and error compute wait via this.#getWait(), schedule the next tick (setTimeout + this.#timeoutIds.add or call this.#tick()), and ensure you set scheduledAsyncWork = false before the surrounding if (!scheduledAsyncWork) { this.#setState({ pendingTick: false }) } runs; reference the async IIFE, scheduledAsyncWork, this.execute(), this.#getWait(), this.#timeoutIds, this.#tick(), and this.#setState() when making the fix.🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed. Outside diff comments: In `@packages/pacer/src/async-queuer.ts`: - Around line 448-465: The async IIFE in `#tick` can exit early when await this.execute() throws, leaving the local scheduledAsyncWork flag true and pendingTick stuck; wrap the call to this.execute() in try/catch (or use try/finally) so that errors do not skip the scheduling logic: on both success and error compute wait via this.#getWait(), schedule the next tick (setTimeout + this.#timeoutIds.add or call this.#tick()), and ensure you set scheduledAsyncWork = false before the surrounding if (!scheduledAsyncWork) { this.#setState({ pendingTick: false }) } runs; reference the async IIFE, scheduledAsyncWork, this.execute(), this.#getWait(), this.#timeoutIds, this.#tick(), and this.#setState() when making the fix.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
c61369d7-3b97-4b0b-8b87-b8be3aa6476d📥 Commits
Reviewing files that changed from the base of the PR and between e71ead12970d5b8e777fe91f2e4a70fdb7f2fcd3 and b9f7dca6829e92ab37dd130f5dde7f16a8269413.
📒 Files selected for processing (3)
.changeset/fix-async-queuer-pending-tick.mdpackages/pacer/src/async-queuer.tspackages/pacer/tests/async-queuer.test.ts
View your CI Pipeline Execution ↗ for commit d1a8fe1b113dedad4bb10b42f298feb5110af2bc
Command Status Duration Result nx run-many --targets=build --exclude=examples/**✅ Succeeded 17s View ↗
☁️ Nx Cloud last updated this comment at
2026-03-31 13:16:31UTCMore templates
- @tanstack/pacer-example-angular-async-batch
- @tanstack/pacer-example-angular-async-debounce
- @tanstack/pacer-example-angular-async-rate-limit
- @tanstack/pacer-example-angular-async-retry
- @tanstack/pacer-example-angular-async-throttle
- @tanstack/pacer-example-angular-batch
- @tanstack/pacer-example-angular-debounce
- @tanstack/pacer-example-angular-inject-async-batched-callback
- @tanstack/pacer-example-angular-inject-async-batcher
- @tanstack/pacer-example-angular-inject-async-debounced-callback
- @tanstack/pacer-example-angular-inject-async-debouncer
- @tanstack/pacer-example-angular-inject-async-queued-signal
- @tanstack/pacer-example-angular-inject-async-queuer
- @tanstack/pacer-example-angular-inject-async-rate-limiter
- @tanstack/pacer-example-angular-inject-async-rate-limiter-with-persister
- @tanstack/pacer-example-angular-inject-async-throttled-callback
- @tanstack/pacer-example-angular-inject-async-throttler
- @tanstack/pacer-example-angular-inject-batched-callback
- @tanstack/pacer-example-angular-inject-batcher
- @tanstack/pacer-example-angular-inject-debounced-callback
- @tanstack/pacer-example-angular-inject-debounced-signal
- @tanstack/pacer-example-angular-inject-debounced-value
- @tanstack/pacer-example-angular-inject-debouncer
- @tanstack/pacer-example-angular-inject-queued-signal
- @tanstack/pacer-example-angular-inject-queued-value
- @tanstack/pacer-example-angular-inject-queuer
- @tanstack/pacer-example-angular-inject-queuer-with-persister
- @tanstack/pacer-example-angular-inject-rate-limited-callback
- @tanstack/pacer-example-angular-inject-rate-limited-signal
- @tanstack/pacer-example-angular-inject-rate-limited-value
- @tanstack/pacer-example-angular-inject-rate-limiter
- @tanstack/pacer-example-angular-inject-rate-limiter-with-persister
- @tanstack/pacer-example-angular-inject-throttled-callback
- @tanstack/pacer-example-angular-inject-throttled-signal
- @tanstack/pacer-example-angular-inject-throttled-value
- @tanstack/pacer-example-angular-inject-throttler
- @tanstack/pacer-example-angular-queue
- @tanstack/pacer-example-angular-rate-limit
- @tanstack/pacer-example-angular-throttle
- @tanstack/pacer-example-preact-async-batch
- @tanstack/pacer-example-preact-async-debounce
- @tanstack/pacer-example-preact-async-rate-limit
- @tanstack/pacer-example-preact-async-retry
- @tanstack/pacer-example-preact-async-throttle
- @tanstack/pacer-example-preact-batch
- @tanstack/pacer-example-preact-debounce
- @tanstack/pacer-example-preact-queue
- @tanstack/pacer-example-preact-rate-limit
- @tanstack/pacer-example-preact-throttle
- @tanstack/pacer-example-preact-async-batched-callback
- @tanstack/pacer-example-preact-async-batcher
- @tanstack/pacer-example-preact-use-async-debounced-callback
- @tanstack/pacer-example-preact-use-async-debouncer
- @tanstack/pacer-example-preact-async-queued-state
- @tanstack/pacer-example-preact-async-queuer
- @tanstack/pacer-example-preact-async-rate-limiter
- @tanstack/pacer-example-preact-async-rate-limiter-with-persister
- @tanstack/pacer-example-preact-async-throttled-callback
- @tanstack/pacer-example-preact-async-throttler
- @tanstack/pacer-example-preact-batched-callback
- @tanstack/pacer-example-preact-LBatcher
- @tanstack/pacer-example-preact-use-debounced-callback
- @tanstack/pacer-example-preact-use-debounced-state
- @tanstack/pacer-example-preact-use-debounced-value
- @tanstack/pacer-example-preact-use-debouncer
- @tanstack/pacer-example-preact-queued-state
- @tanstack/pacer-example-preact-queued-value
- @tanstack/pacer-example-preact-queuer
- @tanstack/pacer-example-preact-queuer-with-persister
- @tanstack/pacer-example-preact-rate-limited-callback
- @tanstack/pacer-example-preact-rate-limited-state
- @tanstack/pacer-example-preact-rate-limited-value
- @tanstack/pacer-example-preact-rate-limiter
- @tanstack/pacer-example-preact-rate-limiter-with-persister
- @tanstack/pacer-example-preact-use-throttled-callback
- @tanstack/pacer-example-preact-use-throttled-state
- @tanstack/pacer-example-preact-use-throttled-value
- @tanstack/pacer-example-preact-use-throttler
- @tanstack/pacer-example-preact-util-comparison
- @tanstack/pacer-example-solid-async-batch
- @tanstack/pacer-example-solid-async-debounce
- @tanstack/pacer-example-solid-async-rate-limit
- @tanstack/pacer-example-solid-async-throttle
- @tanstack/pacer-example-solid-batch
- @tanstack/pacer-example-solid-create-async-batcher
- @tanstack/pacer-example-solid-create-async-debouncer
- @tanstack/pacer-example-solid-create-async-queuer
- @tanstack/pacer-example-solid-create-async-rate-limiter
- @tanstack/pacer-example-solid-create-async-throttler
- @tanstack/pacer-example-solid-create-batcher
- @tanstack/pacer-example-solid-create-debounced-signal
- @tanstack/pacer-example-solid-create-debounced-value
- @tanstack/pacer-example-solid-create-debouncer
- @tanstack/pacer-example-solid-create-queued-signal
- @tanstack/pacer-example-solid-create-queuer
- @tanstack/pacer-example-solid-create-rate-limited-signal
- @tanstack/pacer-example-solid-create-rate-limited-value
- @tanstack/pacer-example-solid-create-rate-limiter
- @tanstack/pacer-example-solid-create-throttled-signal
- @tanstack/pacer-example-solid-create-throttled-value
- @tanstack/pacer-example-solid-create-throttler
- @tanstack/pacer-example-solid-debounce
- @tanstack/pacer-example-solid-queue
- @tanstack/pacer-example-solid-rate-limit
- @tanstack/pacer-example-solid-throttle
- @tanstack/pacer-example-vanilla-lite-batcher
- @tanstack/pacer-example-vanilla-lite-debouncer
- @tanstack/pacer-example-vanilla-lite-queuer
- @tanstack/pacer-example-vanilla-lite-rate-limiter
- @tanstack/pacer-example-vanilla-lite-throttler
- @tanstack/pacer-example-vanilla-lite-batch
- @tanstack/pacer-example-vanilla-lite-debounce
- @tanstack/pacer-example-vanilla-lite-queue
- @tanstack/pacer-example-vanilla-lite-rate-limit
- @tanstack/pacer-example-vanilla-lite-throttle
- @tanstack/pacer-example-react-asyncbatch
- @tanstack/pacer-example-react-async-debounce
- @tanstack/pacer-example-react-async-rate-limit
- @tanstack/pacer-example-react-asyncretry
- @tanstack/pacer-example-react-async-throttle
- @tanstack/pacer-example-react-batch
- @tanstack/pacer-example-react-debounce
- @tanstack/pacer-example-react-queue
- @tanstack/pacer-example-react-rate-limit
- @tanstack/pacer-example-react-query-debounced-prefetch
- @tanstack/pacer-example-react-query-queued-prefetch
- @tanstack/pacer-example-react-query-throttled-prefetch
- @tanstack/pacer-example-react-throttle
- @tanstack/pacer-example-react-use-async-batched-callback
- @tanstack/pacer-example-react-use-async-batcher
- @tanstack/pacer-example-react-use-async-debounced-callback
- @tanstack/pacer-example-react-use-async-debouncer
- @tanstack/pacer-example-react-use-async-queuer-state
- @tanstack/pacer-example-react-use-async-queuer
- @tanstack/pacer-example-react-use-async-rate-limiter
- @tanstack/pacer-example-react-use-async-rate-limiter-with-persister
- @tanstack/pacer-example-react-use-async-throttled-callback
- @tanstack/pacer-example-react-use-async-throttler
- @tanstack/pacer-example-react-use-batched-callback
- @tanstack/pacer-example-react-use-batcher
- @tanstack/pacer-example-react-use-debounced-callback
- @tanstack/pacer-example-react-use-debounced-state
- @tanstack/pacer-example-react-use-debounced-value
- @tanstack/pacer-example-react-use-debouncer
- @tanstack/pacer-example-react-use-queued-state
- @tanstack/pacer-example-react-use-queued-value
- @tanstack/pacer-example-react-use-queuer
- @tanstack/pacer-example-react-use-queuer-with-persister
- @tanstack/pacer-example-react-use-rate-limited-callback
- @tanstack/pacer-example-react-use-rate-limited-state
- @tanstack/pacer-example-react-use-rate-limited-value
- @tanstack/pacer-example-react-use-rate-limiter
- @tanstack/pacer-example-react-use-rate-limiter-with-persister
- @tanstack/pacer-example-react-use-throttled-callback
- @tanstack/pacer-example-react-use-throttled-state
- @tanstack/pacer-example-react-use-throttled-value
- @tanstack/pacer-example-react-use-throttler
- @tanstack/pacer-example-react-util-comparison
@tanstack/angular-pacer
npm i https://pkg.pr.new/@tanstack/angular-pacer@189@tanstack/pacer
npm i https://pkg.pr.new/@tanstack/pacer@189@tanstack/pacer-devtools
npm i https://pkg.pr.new/@tanstack/pacer-devtools@189@tanstack/pacer-lite
npm i https://pkg.pr.new/@tanstack/pacer-lite@189@tanstack/preact-pacer
npm i https://pkg.pr.new/@tanstack/preact-pacer@189@tanstack/preact-pacer-devtools
npm i https://pkg.pr.new/@tanstack/preact-pacer-devtools@189@tanstack/react-pacer
npm i https://pkg.pr.new/@tanstack/react-pacer@189@tanstack/react-pacer-devtools
npm i https://pkg.pr.new/@tanstack/react-pacer-devtools@189@tanstack/solid-pacer
npm i https://pkg.pr.new/@tanstack/solid-pacer@189@tanstack/solid-pacer-devtools
npm i https://pkg.pr.new/@tanstack/solid-pacer-devtools@189commit:
d1a8fe1gave you co-author credit in a larger PR that fixed this and even more issues at all once
KevinVandy closed this
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
Run ID:
0f7d825f-628d-42ee-ba86-9899979c22ea📥 Commits
Reviewing files that changed from the base of the PR and between b9f7dca6829e92ab37dd130f5dde7f16a8269413 and d1a8fe1b113dedad4bb10b42f298feb5110af2bc.
📒 Files selected for processing (2)
docs/reference/classes/AsyncQueuer.mddocs/reference/functions/asyncQueue.md✅ Files skipped from review due to trivial changes (2)
📝 Walkthrough
Walkthrough#
AsyncQueuer's tick logic now tracks whether async work was scheduled during a tick and only clears the
pendingTickflag when no async operations were scheduled. This prevents items added during the configured wait period from bypassing the wait and being processed immediately. A test was added and changeset created.Changes#
packages/pacer/src/async-queuer.ts#tick()now sets a localscheduledAsyncWorkflag when it schedules async executions and only clearspendingTickif no async work was scheduled, ensuringpendingTickremains true while asynchronous work/wait timers are outstanding.packages/pacer/tests/async-queuer.test.tswaitinterval are processed only after the wait completes (uses fake timers and concurrency=1 scenario)..changeset/fix-async-queuer-pending-tick.mdpendingTickbehavior fix for@tanstack/pacer.docs/reference/classes/AsyncQueuer.md,docs/reference/functions/asyncQueue.mdEstimated code review effort#
🎯 3 (Moderate) | ⏱️ ~20 minutes
Poem#
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
#188by implementing the proposed fix with a scheduledAsyncWork flag to track async work and only clearing pendingTick when appropriate.#188: the async-queuer implementation fix, test case, changeset, and documentation line number updates.✨ 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