A tiny, modern, bare bones event emitter for Node.js and the browser. For the latter it supports Internet Explorer 9+ and works with Browserify or as a standalone library.
npm install evmit --save
bower install evmit --save
var Evmit = require('evmit')
var emitter = new Evmit()
var emitter = new Evmit()
Subscribe to an event.
name
is the name of the event.fn
is the function that gets called when the event is emitted.
emitter.on('foo', function() {
// ...
})
emitter.on('foo', function(/* ... */) {
// ...
})
Subscribe to an event only once.
name
is the name of the event.fn
is the function that gets called when the event is emitted.
emitter.once('foo', function() {
// ...
})
emitter.once('foo', function(/* ... */) {
// ...
})
Trigger an event.
name
is the name of the event....
is the arguments that gets passed to the event.
emitter.emit('foo', { bar: 'baz' })
emitter.emit('foo', 'bar', 'baz')
Unsubscribe from an event or all events.
name
is the name of the event.fn
is a specific function that's bound to the event.
If name
is not provided it'll unsubscribe from all events.
emitter.off()
emitter.off('foo')
emitter.off('bar', function(/* ... */) {
// ...
})
Return all events or a single event.
name
is the name of the event.
emitter.listeners()
// => { foo: [...], bar: [...] }
emitter.listeners('foo')
// => [...]