Skip to content

feat(batch): add pendingCount, flush(), and clear() lifecycle helpers - #12

Open
Divyanshgupta2580 wants to merge 1 commit into
mayankcodes-dev:mainfrom
Divyanshgupta2580:fix/issue-7
Open

Divyanshgupta2580 wants to merge 1 commit into
mayankcodes-dev:mainfrom
Divyanshgupta2580:fix/issue-7

Conversation

@Divyanshgupta2580

Copy link
Copy Markdown

Closes #7

Added get pendingCount(), made flush() public, and added clear(reason?) to BatchFlight in src/batch.ts so callers can inspect queued keys, drain batches early, or cancel pending waiters during shutdown. Added unit tests in test/batch.test.ts covering both normal queuing and empty-queue calls.

Tested locally with npm run build && npm test && npm run check:types (84/84 tests passed and publint reported no problems).

@greptile-apps

greptile-apps Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Confidence Score: 4/5RetriggerView in Greptile

[Medium impact] The changes affect callers managing queued BatchFlight work.

The PR appears safe to merge, with non-blocking improvements to tests and documentation.

Findings

  1. P2 Timer cleanup goes unchecked ▶
  2. P2 API references omit new helpers ▶
Fix with agent prompt
### Issue 1
test/batch.test.ts:349-356
The new `clear()` test never checks that its timer was removed. Both promise assertions finish before the 5-second window ends. `called` stays false even if the timer later fires because the queue is already empty.

Use fake timers and assert that no timer remains after `clear()`. Cover the same cleanup after public `flush()`. Otherwise, a change that leaves Node.js running unnecessarily would pass these tests.

### Issue 2
src/batch.ts:205-209
The public API references in `README.md` and `llms.txt` still list only `load()` and `loadMany()`. Add `pendingCount`, `flush()`, and `clear()` there. Explain that they only affect queued keys, that `flush()` does not wait for completion, and that `clear()` returns a unique-key count. These details matter for the shutdown use case this change adds.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

Adds three queue helpers to BatchFlight:

  • BatchFlight callers can check queued entries and flush them right away.
  • BatchFlight callers can cancel queued keys and reject their waiters.

No blocking behavior issue was found. Non-blocking feedback covers timer-cleanup tests and API documentation.

Reviews (1) · Last reviewed commit: "feat(batch): add pendingCount, flush(), ..." · Reviewed by Greptile

Comment thread test/batch.test.ts
Comment on lines +349 to +356
it('clear() cancels active timer, rejects pending waiters, and returns cleared count', async () => {
let called = false;
const batch = new BatchFlight<number, string>(
async (keys) => {
called = true;
return keys.map((k) => `val:${k}`);
},
{ maxWindowMs: 5_000 },

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Timer cleanup goes unchecked

The new clear() test never checks that its timer was removed. Both promise assertions finish before the 5-second window ends. called stays false even if the timer later fires because the queue is already empty.

Use fake timers and assert that no timer remains after clear(). Cover the same cleanup after public flush(). Otherwise, a change that leaves Node.js running unnecessarily would pass these tests.

Prompt To Fix With AI
This is a comment left during a code review.
Path: test/batch.test.ts
Line: 349-356

Comment:
**Timer cleanup goes unchecked**

The new `clear()` test never checks that its timer was removed. Both promise assertions finish before the 5-second window ends. `called` stays false even if the timer later fires because the queue is already empty.

Use fake timers and assert that no timer remains after `clear()`. Cover the same cleanup after public `flush()`. Otherwise, a change that leaves Node.js running unnecessarily would pass these tests.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Comment thread src/batch.ts
Comment on lines +205 to +209
/**
* Cancels any active flush timer and rejects all currently pending waiters
* with `reason`, returning the number of cleared entries.
*/
clear(reason?: unknown): number {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 API references omit new helpers

The public API references in README.md and llms.txt still list only load() and loadMany(). Add pendingCount, flush(), and clear() there. Explain that they only affect queued keys, that flush() does not wait for completion, and that clear() returns a unique-key count. These details matter for the shutdown use case this change adds.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/batch.ts
Line: 205-209

Comment:
**API references omit new helpers**

The public API references in `README.md` and `llms.txt` still list only `load()` and `loadMany()`. Add `pendingCount`, `flush()`, and `clear()` there. Explain that they only affect queued keys, that `flush()` does not wait for completion, and that `clear()` returns a unique-key count. These details matter for the shutdown use case this change adds.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

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.

[Feature]: Add BatchFlight.pendingCount, flush(), and clear() lifecycle helpers

1 participant