From aad8fabd4aa2e0bd4f1ec5a2e906dac0510408c5 Mon Sep 17 00:00:00 2001 From: Divyansh Gupta Date: Fri, 4 Dec 2026 21:45:20 +0530 Subject: [PATCH] feat(batch): add pendingCount, flush(), and clear() lifecycle helpers (#7) --- src/batch.ts | 32 ++++++++++++++++++++++++++++- test/batch.test.ts | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/src/batch.ts b/src/batch.ts index a41a493..84bb400 100644 --- a/src/batch.ts +++ b/src/batch.ts @@ -95,6 +95,11 @@ export class BatchFlight { validateNonNegativeDuration('timeoutMs', this.timeoutMs); } + /** Returns the number of unique keys currently queued for the next batch flush. */ + get pendingCount(): number { + return this.pending.size; + } + /** Queues `key` for the next batch flush and resolves with its individual value. */ load(key: K): Promise { try { @@ -179,7 +184,8 @@ export class BatchFlight { }, this.maxWindowMs); } - private flush(): void { + /** Triggers an immediate flush of currently pending keys (no-op if empty). */ + flush(): void { if (this.pending.size === 0) { return; } @@ -196,6 +202,30 @@ export class BatchFlight { void this.executeBatch(entries, keys); } + /** + * Cancels any active flush timer and rejects all currently pending waiters + * with `reason`, returning the number of cleared entries. + */ + clear(reason?: unknown): number { + if (this.flushTimer !== undefined) { + clearTimeout(this.flushTimer); + this.flushTimer = undefined; + } + + const count = this.pending.size; + if (count === 0) { + return 0; + } + + const entries = Array.from(this.pending.values()); + this.pending.clear(); + const rejection = reason !== undefined ? reason : new Error('BatchFlight cleared'); + for (const entry of entries) { + entry.reject(rejection); + } + return count; + } + private async executeBatch( entries: readonly PendingKey[], keys: readonly K[], diff --git a/test/batch.test.ts b/test/batch.test.ts index 15672a5..25dba8d 100644 --- a/test/batch.test.ts +++ b/test/batch.test.ts @@ -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( + 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( + async (keys) => { + called = true; + return keys.map((k) => `val:${k}`); + }, + { maxWindowMs: 5_000 }, + ); + + 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); + }); }); // =========================================================================