event-filter
v1.2.0
Published
A package for filtering Node.js events. Your listeners only get executed when the specified predicate is satisfied.
Maintainers
Readme
event-filter
A package for filtering Node.js events. Your listeners only get executed when the specified predicate is satisfied.
Usage
import { EventEmitter } from 'event'
import 'event-filter'
const event = new EventEmitter()
event.onWhen('someEvent', (...) => /* predicate */, (...) => /* normal listener */)
event.onceWhen('someEvent', (...) => /* predicate */, (...) => /* normal listener */)event-filter adds the following methods to the EventEmitter prototype:
Method Name | Description ----------- | ----------- onWhen (event: string | symbol, predicate: (...args: any[]) => boolean, listener: (...args: any[]) => void): this | Subscribe to the specified event, but only call the listener when the predicate is satisfied. onceWhen (event: string | symbol, predicate: (...args: any[]) => boolean, listener: (...args: any[]) => void): this | Subscribe to the specified event, calling the listener the first time the predicate is satisfied.
event-filter also exports the following methods:
Method Name | Description ----------- | ----------- onWhen<TEvent extends EventLike> (evt: EventLike, event: string | symbol, predicate: (...args: any[]) => boolean, listener: (...args: any[]) => void): TEvent | Subscribe to the specified event, but only call the listener when the predicate is satisfied. onceWhen<TEvent extends EventLike> (evt: EventLike, event: string | symbol, predicate: (...args: any[]) => boolean, listener: (...args: any[]) => void): TEvent | Subscribe to the specified event, calling the listener the first time the predicate is satisfied.
Using these allows you to call onWhen and onceWhen on objects that are like EventEmitter, but don't actually extend the superclass.
event-filter contains defines TypeScript definitions for type safety.
event-filter adds JSDoc comments to both new methods.
Building
To build, install all devDependencies and execute npm run build.
To build with a watch, execute npm run watch:build.
Testing
To test, install all devDependencies and execute npm run test.
