@yukiakai/events-async
v1.0.1
Published
Drop-in replacement for Node.js EventEmitter with async listener support (emitAsync).
Maintainers
Readme
@yukiakai/events-async
Drop-in replacement for Node.js EventEmitter with async listener support.
Supports emitAsync (parallel) and emitSeries (sequential) execution of listeners, and is fully type-safe in TypeScript.
Works with both string and symbol event names.
Features
- Fully TypeScript type-safe (
on,off) - Async listeners supported
emitAsync: run all listeners in parallelemitSeries: run listeners sequentially- Supports string and symbol keys for events
- Compatible with Node.js
EventEmitterAPI
Installation
npm install @yukiakai/events-async
# or
yarn add @yukiakai/events-asyncUsage
import { EventEmitterAsync } from '@yukiakai/events-async';
const SYMBOL_EVENT = Symbol('symbolEvent');
interface MyEvents {
hello: (name: string) => void | Promise<void>;
count: (n: number) => void | Promise<void>;
[SYMBOL_EVENT]: (msg: string) => void | Promise<void>;
}
const ee = new EventEmitterAsync<MyEvents>();
// Type-safe listener
ee.on('hello', async (name) => {
console.log('Hello', name);
});
ee.on(SYMBOL_EVENT, (msg) => {
console.log('Symbol event:', msg);
});
// Emit asynchronously (parallel)
await ee.emitAsync('hello', 'world');
await ee.emitAsync(SYMBOL_EVENT, 'ok');
// Emit sequentially (in order)
await ee.emitSeries('hello', 'world');API
See docs: API Docs
on(event, listener)
Register a listener with type safety.
event can be string or symbol.
off(event, listener)
Remove a listener (type-safe).
emitAsync(event, ...args)
Run all listeners in parallel, await completion.
emitSeries(event, ...args)
Run listeners in order, await completion.
Notes
- Supports both sync and async listeners.
string | symbolkeys fully supported.
Development
npm install
npm run build
npm run test
npm run lintChangelog
See full release notes in CHANGELOG.md
License
MIT © Yuki Akai
