Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@audc/eventemitter-wrapper

Package Overview
Dependencies
Maintainers
0
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@audc/eventemitter-wrapper

Control event groups

  • 2.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
119
decreased by-27.44%
Maintainers
0
Weekly downloads
 
Created
Source

eventemitter-wrapper - Nodejs Module

Control event groups

NPM Version Downloads Stats Tests

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.

Method / PropertyTypeNotes
eventEmitterpropOriginal EventEmitter
addListener(eventName,listener)methodListens on original & wrapped
on(eventName,listener)methodListens on original & wrapped
once(eventName,listener)methodListens on original & wrapped
prependListener(eventName,listener)methodListens on original & wrapped
prependOnceListener(eventName,listener)methodListens on original & wrapped
rawListeners(eventName)methodLists only wrapped listeners
listeners(eventName)methodLists only wrapped listeners
listenerCount(eventName[,listener])methodCounts only wrapped listeners
eventNames()methodLists only wrapped listeners
emit(eventName[,...args])methodDirectly calls original method
removeAllListeners([eventName])methodRemoves only wrapped listeners
removeListener(eventName,listener)methodRemoves on both original & wrapped
off(eventName,listener)methodRemoves on both original & wrapped
getMaxListeners()methodDirectly calls original method
setMaxListeners(n)methodDirectly calls original method

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

// Require modules
const EventEmitter = require('node:events');
const EventEmitterWrapper = require('eventemitter-wrapper');

// Create instances
const events = new EventEmitter();
const eventsWrapped = new EventEmitterWrapper(events);

// Attach a listener to original EventEmitter
events.on('original',(...args)=>{
	console.log('original:',...args);
});

// Fire event on either original or wrapped
events.emit('original','foo');
// Logs: original: foo

// Attach a listener to wrapped EventEmitter
eventsWrapped.on('wrapped',(...args)=>{
	console.log('wrapped:',...args);
});

// Fire event on either original or wrapped
events.emit('wrapped','bar');
// Logs: wrapped: bar

// Fetch a list of events on the original
// "removeListener" event is used by the wrapper for event clean-up
console.log(events.eventNames());
// Logs: [ 'original', 'wrapped', 'removeListener' ]

// Fetch a list of events on the wrapper
console.log(eventsWrapped.eventNames());
// Logs: [ 'wrapped' ]

// Remove all listeners on the wrapper
eventsWrapped.removeAllListeners();

// Fetch a list of events on the original
console.log(events.eventNames());
// Logs: [ 'original' ]

// Fetch a list of events on the wrapper
console.log(eventsWrapped.eventNames());
// Logs: []

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.

Github Repository: https://github.com/Jashepp/eventemitter-wrapper

NPM Package: https://www.npmjs.com/package/eventemitter-wrapper

Keywords

FAQs

Package last updated on 14 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc