Repository navigation
feat(batch): add pendingCount, flush(), and clear() lifecycle helpers #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -320,6 +320,56 @@ describe('BatchFlight', () => { | |
|
|
||
| await expect(pending).resolves.toBe('val:1'); | ||
| }); | ||
|
|
||
| it('tracks pendingCount and flushes immediately with flush()', async () => { | ||
| const batches: number[][] = []; | ||
| const batch = new BatchFlight<number, string>( | ||
| async (keys) => { | ||
| batches.push([...keys]); | ||
| return keys.map((k) => `val:${k}`); | ||
| }, | ||
| { maxWindowMs: 5_000 }, | ||
| ); | ||
|
|
||
| expect(batch.pendingCount).toBe(0); | ||
| batch.flush(); // no-op when empty | ||
| expect(batches).toEqual([]); | ||
|
|
||
| const p1 = batch.load(1); | ||
| const p2 = batch.load(2); | ||
| const p2Dup = batch.load(2); | ||
| expect(batch.pendingCount).toBe(2); | ||
|
|
||
| batch.flush(); | ||
| expect(batch.pendingCount).toBe(0); | ||
| await expect(Promise.all([p1, p2, p2Dup])).resolves.toEqual(['val:1', 'val:2', 'val:2']); | ||
| expect(batches).toEqual([[1, 2]]); | ||
| }); | ||
|
|
||
| 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 }, | ||
|
Comment on lines
+349
to
+356
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new Use fake timers and assert that no timer remains after Prompt To Fix With AIThis 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. |
||
| ); | ||
|
|
||
| expect(batch.clear()).toBe(0); | ||
|
|
||
| const p1 = batch.load(1); | ||
| const p2 = batch.load(2); | ||
| expect(batch.pendingCount).toBe(2); | ||
|
|
||
| const reason = new Error('shutting down'); | ||
| expect(batch.clear(reason)).toBe(2); | ||
| expect(batch.pendingCount).toBe(0); | ||
|
|
||
| await expect(p1).rejects.toBe(reason); | ||
| await expect(p2).rejects.toBe(reason); | ||
| expect(called).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| // ========================================================================= | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The public API references in
README.mdandllms.txtstill list onlyload()andloadMany(). AddpendingCount,flush(), andclear()there. Explain that they only affect queued keys, thatflush()does not wait for completion, and thatclear()returns a unique-key count. These details matter for the shutdown use case this change adds.Prompt To Fix With AI
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!