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.
Install the module via NPM
npm install eventemitter-wrapper --save
This module is written with ES6 features.
How to use
Require the module, wrap an existing eventEmitter instance, and use methods as you usually would.
const eventEmitter = ;const eventEmitterWrapper = ;const events = ;const eventGroup1 = eventEmitterWrapper;const eventGroup2 = eventEmitterWrapper;...
The first argument for
createWrapper, can be any object that is an EventEmitter directly or prototyped.
The methods are meant to be identical to Node.js's EventEmitter (minus deprecated features).
Methods on the wrapper:
_eventStore (object key store of events and listeners),
_eventEmitterInstance (the main EventEmitter)
// Require modulesconst eventEmitter = ;const eventEmitterWrapper = ;// Create EventEmitter instanceconst events = ;// Attach a listener to `events`events;// Create a wrapped events instanceconst eventGroup1 = eventEmitterWrapper;// All events on the main EventEmitter are still emittable// Any wrapper or the main EventEmitter can call .emit to run any listenereventGroup1;// Logs: events: Hello World!// Any event listener can be created, even if there are other listeners on the main EventEmitter or on other wrappers.eventGroup1;eventGroup1;// Emit again (same with eventGroup1.emit...)events;// Logs: events: Foo Bar!// Logs: eventGroup1: No, I have joined the dark side// Emit again (same with eventGroup1.emit...)// The emit method falls directly to the main EventEmitter, so it will also work across all wrappersevents;// Logs: eventGroup1: Okay// Fetch a list of event names on the main EventEmitter// Notice that the main EventEmitter contains all listeners, including those on a wrapper// The "removeListener" event is attached internally via a wrapperconsole// Logs: ["logSomething", "removeListener", "dance"]// Fetch a list of event names on the wrapper// Notice that the wrapper ignores all listeners outside this wrapperconsole// Logs: ["logSomething", "dance"]// Remove all listeners on the wrappereventGroup1// List event names on the main EventEmitter// Notice that "dance" has been removed, also the "logSomething" event now only has 1 listener instead of 2console// Logs: ["logSomething", "removeListener"]// List event names on the wrapper// All events attached via the wrapper have been removedconsole// Logs: 
None at the moment.
Create issues on the GitHub project or create pull requests.
All the help is appreciated.
Copyright (c) 2017 Jason Sheppard @ https://github.com/Unchosen
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.
Github Repository: https://github.com/Unchosen/eventemitter-wrapper
NPM Package: https://www.npmjs.com/package/eventemitter-wrapper