Socket
Socket
Sign inDemoInstall

event-cleanup

Package Overview
Dependencies
2
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    event-cleanup

Wrap an EventEmitter for easy listener cleanup


Version published
Weekly downloads
24
decreased by-46.67%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

event-cleanup

npm version Build Status Dependency Status

Wrap an EventEmitter for easy listener cleanup

Usage

npm install event-cleanup

var wrap = require('event-cleanup')

// wrap an EventEmitter called `emitter`
var wrapper = wrap(emitter)

// wrapper has all of the EventEmitter methods
// and receives all events emitted by `emitter`
wrapper.on(...)
wrapper.once(...)
wrapper.removeListener(...)
...

// when done, remove all listeners on `wrapper`
// (leaving `emitter` untouched)
wrapper.removeAll()

event-cleanup lets you wrap an EventEmitter, letting you add/remove listeners to a separate EventEmitter without polluting the original. When you are done, you can clean up all of the listeners added to the wrapper EventEmitter.

API

var wrapper = wrap(emitter)

Creates a wrapper EventEmitter which receives all events emitted by emitter.

The returned object has all EventEmitter methods (e.g. wrapper.on(event, listener)). Calling wrapper.emit(event, listener) will only emit the event to the listeners added directly to the wrapper, not the wrapped emitter.

wrapper.removeAll()

Removes all listeners added directly to wrapper.

Rationale

I often found myself in a code pattern where I would temporarily add event listeners to some EventEmitter, then need to remove them all when done. However, this requires keeping a reference to all listener functions so that they can be removed with removeListener(), which can be inconvenient. It also makes it possible to accidentally leak listeners if they never got removed.

This module makes this use-case easy: in a function where you add temporary listeners, wrap the EventEmitter and add the listeners to it, then call removeAll() when done to ensure all listeners are cleaned up.

Keywords

FAQs

Last updated on 17 May 2016

Did you know?

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc