Skip to content

A package for filtering Node.js events. Your listeners only get executed when the specified predicate is satisfied.

License

Notifications You must be signed in to change notification settings

OrionNebula/event-filter

Repository files navigation

event-filter

npm npm npm type definitions GitHub last commit Build Status

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.

About

A package for filtering Node.js events. Your listeners only get executed when the specified predicate is satisfied.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published