@js-nerds/batch-collector
v0.1.1
Published
Lightweight TypeScript batch collector with delayed flush and optional persistence.
Readme
@js-nerds/batch-collector
Lightweight TypeScript batch collector with delayed flush, optional timer reset, and local/session storage persistence.
Install
npm install @js-nerds/batch-collectorpnpm add @js-nerds/batch-collectoryarn add @js-nerds/batch-collectorUsage
import { BATCH_FLUSH_EVENT, BatchCollector } from "@js-nerds/batch-collector";
const collector = new BatchCollector<{ action: string; id: string }>({
delayMs: 5000,
resetTimerOnPush: true,
storageType: "localStorage",
storageKey: "my-app-log-batch"
});
collector.subscribe(BATCH_FLUSH_EVENT, (items) => {
console.log("Flush:", items);
});
collector.push({ action: "button_click", id: "submit-btn" });
collector.push({ action: "menu_open", id: "profile" });API
new BatchCollector(config)
Creates a collector instance.
Config
delayMs: number— delay before flush.resetTimerOnPush?: boolean— reset timer on every push (defaulttrue).storageType?: "memory" | "localStorage" | "sessionStorage"— where to keep pending batch (default"memory").storageKey?: string— key for local/session storage (default"batch-collector-pending").autoClear?: boolean— clear after timer flush (defaulttrue).
subscribe(event, callback)
Subscribes to collector events.
Params
event: string— useBATCH_FLUSH_EVENT("flush").callback: (items: T[]) => void
Returns
() => void— unsubscribe function.
push(item)
Adds item to batch and schedules flush.
items()
Returns a shallow copy of current batch.
clear()
Clears timer, in-memory batch, and persisted batch.
License
MIT
Changelog
See CHANGELOG.md.
