has-listener
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

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

Package Sidebar

Install

npm i has-listener

Weekly Downloads

1

Version

1.1.1

License

MIT

Unpacked Size

9.07 kB

Total Files

10

Last publish

Collaborators

  • leichtgewicht