npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

node-events-batcher

v1.0.1

Published

Universal events batcher for NodeJS

Readme

node-events-batcher

Universal events batcher for NodeJS. Collects events and flushes them in batches via configurable strategies (size-based or time-based) with array or set accumulation.

Install

[!NOTE] It's highly recommended to use with volta.sh.

npm install node-events-batcher

Quick start

import { EventsBatcher } from 'node-events-batcher';

const batcher = new EventsBatcher<string>(
	(events) => {
		console.log('Flushed:', events);
	},
	(err) => console.error(err),
	{ size: 5, accumulatorType: 'array', timeoutMs: 5000 }
);

batcher.add('a');
batcher.add('b');
batcher.add('c');
batcher.add('d');
batcher.add('e'); // callback runs with ['a','b','c','d','e']

batcher.flush(); // flush any remaining events manually

API

EventsBatcher<EventType>

Constructor

new EventsBatcher<EventType>(
	callback: (events: ReadonlyArray<EventType>) => void | Promise<void>,
	errorHandler?: ((error: unknown) => void) | null,
	options?: SizeOptions | DebounceOptions
)
  • callback — Called with the batched events when a flush occurs. May return a Promise; rejections are passed to errorHandler.
  • errorHandler — Optional. If provided, callback errors are passed here; otherwise they are rethrown.
  • options — Flush strategy and accumulator. Defaults to { accumulatorType: 'array', timeoutMs: 2000, debounceMs: 50 } (debounce strategy).

Methods

  • add(event: EventType): void — Add an event. May trigger a flush depending on the strategy.
  • flush(): void — Flush all accumulated events immediately.

Options

Two strategy types:

| Option | Type | Description | |--------|------|-------------| | Size strategy | SizeOptions | Flush when the batch reaches size events. | | Debounce strategy | DebounceOptions | Flush after debounceMs ms of no new events. |

Common options

| Option | Type | Description | |--------|------|-------------| | accumulatorType | 'array' | 'set' | 'array' — order preserved, duplicates allowed. 'set' — unique events only, order not guaranteed. | | timeoutMs | number | null | Max wait (ms) before flushing; null disables. |

Size strategy (SizeOptions)

| Option | Type | Description | |--------|------|-------------| | size | number | Flush when batch size reaches this value. |

Debounce strategy (DebounceOptions)

| Option | Type | Description | |--------|------|-------------| | debounceMs | number | Flush after this many ms without a new event. Must be < timeoutMs when timeoutMs is set. |

Size strategy is preferred if both size and debounceMs is set.

Examples

Size-based batching (e.g. send when 10 items):

const batcher = new EventsBatcher<Payload>(
	sendToServer,
	null,
	{
		size: 10,
		accumulatorType: 'array',
		timeoutMs: 3000,
	}
);

Time-based debounce (e.g. persist after 100 ms idle):

const batcher = new EventsBatcher<WindowId>(
	persistWindowState,
	console.error,
	{
		debounceMs: 100,
		timeoutMs: 2000,
		accumulatorType: 'set',
	}
);

Exports

import { EventsBatcher } from 'node-events-batcher';
import type { SizeOptions, DebounceOptions } from 'node-events-batcher';

Example app

The example/ folder contains an Electron app that uses the batcher to persist window state when windows are closed. Run it from the repo root:

cd example && pnpm install && pnpm start

License

MIT