@vates/event-listeners-manager
v1.0.1
Published
<!-- DO NOT EDIT MANUALLY, THIS FILE HAS BEEN GENERATED -->
Downloads
32
Readme
@vates/event-listeners-manager
Install
Installation of the npm package:
> npm install --save @vates/event-listeners-manager
Usage
This library is compatible with Node's
EventEmitter
and web browsers'EventTarget
APIs.
API
import { EventListenersManager } from '@vates/event-listeners-manager'
const events = new EventListenersManager(emitter)
// adding listeners
events.add('foo', onFoo).add('bar', onBar).on('baz', onBaz)
// removing a specific listener
events.remove('foo', onFoo)
// removing all listeners for a specific event
events.removeAll('foo')
// removing all listeners
events.removeAll()
Typical use case
Removing all listeners when no longer necessary.
Manually:
const onFoo = () => {}
const onBar = () => {}
const onBaz = () => {}
emitter.on('foo', onFoo).on('bar', onBar).on('baz', onBaz)
// CODE LOGIC
emitter.off('foo', onFoo).off('bar', onBar).off('baz', onBaz)
With this library:
const events = new EventListenersManager(emitter)
events.add('foo', () => {})).add('bar', () => {})).add('baz', () => {}))
// CODE LOGIC
events.removeAll()
Contributions
Contributions are very welcomed, either on the documentation or on the code.
You may:
- report any issue you've encountered;
- fork and create a pull request.