has-listener
Handler that is triggered when a listener is added to an event.
Last updated 2 years ago by leichtgewicht .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install has-listener 
SYNC missed versions from official npm registry.

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

Current Tags

  • 1.1.1                                ...           latest (2 years ago)

3 Versions

  • 1.1.1                                ...           2 years ago
  • 1.1.0                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (4)

Copyright 2014 - 2017 © taobao.org |