Batcher docs: class example reads as a fixed window, but wait restarts on every addItem #289
Send to my agentTanStack Pacer version#
@tanstack/pacer 0.23.0 (also checked 0.23.1 and main)
Framework/Library version#
Framework-agnostic Batcher class; seen via @tanstack/react-pacer 0.24.0, React 19.3
Describe the bug and the steps to reproduce it#
The Batcher class reference example says:
batcher.addItem(1);
batcher.addItem(2);
// After 2 seconds or when 5 items are added, whichever comes first,
// the batch will be processed
Read on its own, this suggests wait is a maximum delay measured from the first item. In the implementation, addItem clears and restarts the timer on every call (this.#clearTimeout() // clear any pending timeout to replace it with a new one), so wait measures quiet time since the last item. The batching guide does describe it that way ("executes a batch after no new items arrive for the configured duration"), but the class reference, where many people land first, does not. The example only adds two items at once, where both readings give the same result.
Under a steady stream with gaps shorter than wait, nothing is processed until the stream pauses or maxSize is reached:
import { Batcher } from '@tanstack/pacer'
const t0 = Date.now()
const b = new Batcher(
(items) => console.log(Date.now() - t0, 'ms:', items.length, 'items'),
{ wait: 100 },
)
let i = 0
const id = setInterval(() => {
b.addItem(i++)
if (i === 100) clearInterval(id)
}, 10)
// Logs once, ~1200 ms: 100 items. Nothing is processed during the first second.
Suggestion: in the class description and the wait option docs, state that the timer restarts on each addItem, and adjust the example comment, e.g. "2 seconds after the last item is added, or as soon as 5 items are collected, whichever comes first".
Your Minimal, Reproducible Example - (Sandbox Highly Recommended)#
The snippet above (Node, no framework, @tanstack/pacer 0.23.0).
Screenshots or Videos (Optional)#
No response
Do you intend to try to help solve this bug with your own PR?#
None
Terms & Code of Conduct#
- I agree to follow this project's Code of Conduct
- I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
MFA-G(GitHub profile, opens in a new tab) linked this to #290
Sign in to join this conversation.