eventemitter-wrapper - Nodejs Module
Control event groups
What is this?
This module lets you group event listeners so you can seperate/isolate listeners from other listeners, such that you can call removeAllListeners
and it will only remove the listeners on the current event wrapper. No need to keep track of specific groups of listeners when this can do it for you.
Why use this?
If you have an EventEmitter
which has important event listeners on it, and you have a module that you want others to use without them removing those said event listeners by mistake, then this will let you protect them by exporting the wrapped EventEmitter
instead of the main one.
Or, in the case of why I needed this module, I have portions of my applications as reloadable, and each time some code unloads, I have the events on a wrapper that I simply remove the listeners from without effecting other parts of my application.
Installation
Install via NPM: with the NPM package
npm install eventemitter-wrapper
Install via NPM from Github: with a GitHub Tag to specify version (specifed as #hash)
npm install jashepp/eventemitter-wrapper#v2.0.1
Or download the latest release, or use github packages, or git clone the repository on GitHub.
This module is written with ES6 features.
How To Use / API
This module is available as both CommonJS and ES Module. The ES Module calls the CommonJS file under the hood.
Require or import the module, wrap an existing EventEmitter
instance, and use methods as you usually would.
This should behave like the original EventEmitter, with the same methods and functionality, since it wraps it and uses it under the hood.
CommonJS Method:
const EventEmitter = require('node:events');
const EventEmitterWrapper = require('eventemitter-wrapper');
const events = new EventEmitter();
const eventsWrapped = new EventEmitterWrapper(events);
ES Module Method:
import { EventEmitter } from 'node:events';
import { EventEmitterWrapper } from 'eventemitter-wrapper';
const events = new EventEmitter();
const eventsWrapped = new EventEmitterWrapper(events);
The passed argument for EventEmitterWrapper
can be any object that is an EventEmitter directly or prototyped.
The old v1.0
method of creating the wrapped EventEmitter is still available:
const eventsWrapped = EventEmitterWrapper.createWrapper(events);
Methods & Properties
API for the wrapped instance created via new EventEmitterWrapper(events);
Only eventEmitter
is new, along with unlisted internal methods & properties.
When the wrapper has events listening on the original EventEmitter, a removeListener
event will be internally listened on for clean-up after an event is removed.
On this wrapper, there are internal methods & properties prefixed with '_eew
'. These are available (see source code) to use, but they may change in future releases.
Examples
const EventEmitter = require('node:events');
const EventEmitterWrapper = require('eventemitter-wrapper');
const events = new EventEmitter();
const eventsWrapped = new EventEmitterWrapper(events);
events.on('original',(...args)=>{
console.log('original:',...args);
});
events.emit('original','foo');
eventsWrapped.on('wrapped',(...args)=>{
console.log('wrapped:',...args);
});
events.emit('wrapped','bar');
console.log(events.eventNames());
console.log(eventsWrapped.eventNames());
eventsWrapped.removeAllListeners();
console.log(events.eventNames());
console.log(eventsWrapped.eventNames());
Tests
Tests are located within ./tests/
on the git repository on GitHub or locally if pulled. NPM version does not include tests.
To get started with tests, we need to install some dev dependencies. Enter local directory of this repository and run:
npm install --only=dev
To run the tests, run:
npm run test
To continuously run tests while editing, run:
npm run test-watch
Contributors
To submit a contribution, create issues or pull requests on the GitHub repository.
Please be sure to run tests after any changes.
All help is appreciated. Even if it's just improvements to this readme or the tests.
License
MIT License
Copyright (c) 2023 Jason Sheppard @ https://github.com/Jashepp
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Links
Github Repository: https://github.com/Jashepp/eventemitter-wrapper
NPM Package: https://www.npmjs.com/package/eventemitter-wrapper