better-emitter
A very simple event emitter with a better API than all the others
Last updated 10 months ago by tehshrike .
WTFPL · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install better-emitter 
SYNC missed versions from official npm registry.

A very simple event emitter with a better API than all the others.

It's better because:

  1. on and once return an unsubscribe function
  2. It doesn't use this, which means you can pass around the on or emit functions
  3. It can take in any existing object and turn it into an emitter

Also, it's pretty simple. Check out the source code.

npm i better-emitter
const createEmitter = require('better-emitter')

API

const emitter = createEmitter()

const unsubscribe = emitter.on('thing happened', (really, why) => {
	really // => true
	why // => 'I dunno'
})

emitter.emit('thing happened', true, 'I dunno')

unsubscribe()

emitter = createEmitter([emitter])

This is the function exported by the module. It creates a new event emitter object. You can also pass in any object you want, and event emitter functions will be added to it.

unsubscribe = emitter.on(eventString, listenerFunction)

Adds an event listener function. Returns an unsubscribe function that, when called, prevents the listener from firing any more.

unsubscribe = emitter.once(eventString, listenerFunction)

Just like the on function, except the listener is automatically unsubscribed after the first time the event is emitted.

emitter.emit(eventString, [...args])

Calls all listeners of the given event string, with any arguments.

emitter.removeAllListeners()

Unsubscribes all current event listeners.

License

WTFPL

Current Tags

  • 2.3.0                                ...           latest (10 months ago)

13 Versions

  • 2.3.0                                ...           10 months ago
  • 2.2.2                                ...           2 years ago
  • 2.2.1                                ...           2 years ago
  • 2.2.0                                ...           2 years ago
  • 2.1.2                                ...           2 years ago
  • 2.1.1                                ...           2 years ago
  • 2.1.0                                ...           2 years ago
  • 2.0.2                                ...           2 years ago
  • 2.0.1                                ...           2 years ago
  • 2.0.0                                ...           2 years ago
  • 1.0.2                                ...           3 years ago
  • 1.0.1                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 0
Last Month 6
Dependencies (1)
Dev Dependencies (2)

Copyright 2014 - 2017 © taobao.org |