diff --git a/.changeset/young-planes-clap.md b/.changeset/young-planes-clap.md new file mode 100644 index 000000000..ca8f78490 --- /dev/null +++ b/.changeset/young-planes-clap.md @@ -0,0 +1,5 @@ +--- +'@tanstack/pacer': patch +--- + +fix(async-queuer): Fix falsy item handling in AsyncQueuer#tick — items like 0, "", and false are no longer silently dropped from the processing loop. diff --git a/packages/pacer/src/async-queuer.ts b/packages/pacer/src/async-queuer.ts index 22925a170..c17d41059 100644 --- a/packages/pacer/src/async-queuer.ts +++ b/packages/pacer/src/async-queuer.ts @@ -437,7 +437,7 @@ export class AsyncQueuer { this.store.state.items.length > 0 ) { const nextItem = this.peekNextItem() - if (!nextItem) { + if (nextItem === undefined) { break } activeItems.push(nextItem) diff --git a/packages/pacer/tests/async-queuer.test.ts b/packages/pacer/tests/async-queuer.test.ts index 5497797bc..0bc835cd5 100644 --- a/packages/pacer/tests/async-queuer.test.ts +++ b/packages/pacer/tests/async-queuer.test.ts @@ -1095,4 +1095,70 @@ describe('AsyncQueuer', () => { expect(asyncQueuer.getAbortSignal()).toBeNull() }) }) + + describe('falsy item handling', () => { + it('should process items with value 0', async () => { + const processed: Array = [] + const asyncQueuer = new AsyncQueuer( + async (item) => { + processed.push(item) + return item + }, + { started: false }, + ) + + asyncQueuer.addItem(0) + asyncQueuer.addItem(1) + asyncQueuer.addItem(2) + asyncQueuer.start() + + await vi.advanceTimersByTimeAsync(100) + + expect(processed).toEqual([0, 1, 2]) + expect(asyncQueuer.store.state.successCount).toBe(3) + }) + + it('should process items with value "" (empty string)', async () => { + const processed: Array = [] + const asyncQueuer = new AsyncQueuer( + async (item) => { + processed.push(item) + return item + }, + { started: false }, + ) + + asyncQueuer.addItem('') + asyncQueuer.addItem('hello') + asyncQueuer.addItem('') + asyncQueuer.start() + + await vi.advanceTimersByTimeAsync(100) + + expect(processed).toEqual(['', 'hello', '']) + expect(asyncQueuer.store.state.successCount).toBe(3) + }) + + it('should process items with value false', async () => { + const processed: Array = [] + const asyncQueuer = new AsyncQueuer( + async (item) => { + processed.push(item) + return item + }, + { started: false }, + ) + + asyncQueuer.addItem(false) + asyncQueuer.addItem(true) + asyncQueuer.addItem(false) + asyncQueuer.start() + + await vi.advanceTimersByTimeAsync(100) + + expect(processed).toEqual([false, true, false]) + expect(asyncQueuer.store.state.successCount).toBe(3) + }) + + }) })