evmit
v2.0.1
Published
A tiny, modern, bare bones event emitter.
Downloads
11
Readme
Evmit
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.
Installation
npm
npm install evmit --save
Bower
bower install evmit --save
Usage
Initialization
Node.js and Browserify
var Evmit = require('evmit')
var emitter = new Evmit()
Standalone
var emitter = new Evmit()
Evmit.on(name, fn)
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(/* ... */) {
// ...
})
Evmit.once(name, fn)
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(/* ... */) {
// ...
})
Evmit.emit(name[, ...])
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')
Evmit.off([name, fn])
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(/* ... */) {
// ...
})
Evmit.listeners([name])
Return all events or a single event.
name
is the name of the event.
emitter.listeners()
// => { foo: [...], bar: [...] }
emitter.listeners('foo')
// => [...]