Skip to content
Change behavior of a Node.js EventEmitter depending on the existence of a listener.
JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
.travis.yml
CHANGELOG.md
CONTRIBUTING.md
LICENSE
README.md
example.js
index.d.ts
index.js
package.json
test.js

README.md

has-listener

Build Status JavaScript Style Guide Maintainability Test Coverage

has-listener is a little util, useful in combination with EventEmitter. It allows to do some operation only if an event listener is added to an emitter.

npm i has-listener --save

Why

Let's embrace the Observer Effect! 👀 With this little tool you can let your classes do different things depending on whether there is an observer or not.

Usage

const { hasListener } = require('has-listener')
const { EventEmitter } = require('events')

const emitter = new EventEmitter()

function onHasListener (hasListener) {
  console.log(`hasListener: ${hasListener}`)
}

const stop = hasListener(emitter, 'loop', onHasListener) // hasListener: false

emitter.on('loop', () => {}) // hasListener: true
emitter.removeAllListeners() // hasListener: false

stop() // stops updating

emitter.on('loop', () => {}) // nothing, because stop was called.

License

MIT

You can’t perform that action at this time.