A minimal and fast event-emitter for the browser. This can be used as a standalone class or attached to an existing objects prototype. See usage example below.
npm
npm install smelly-event-emitter --save
browser
<script src="/source/to/event-emitter.min.js"></script>
commonjs environment with inheritance (ES6)
import EventEmitter from 'event-emitter';
class Foo extends EventEmitter {
constructor() {
super();
this.on('foo', this.bar);
this.emit('foo');
}
bar() {
console.log('foo event emitted!');
}
}
commonjs environment with inheritance (ES5)
var EventEmitter = require('smelly-event-emitter');
function Foo() {
EventEmitter.call(this); // super
this.on('foo', this.bar);
}
Foo.prototype.bar = function() {
console.log('foo event emitted');
}
Foo.prototype = Object.create(EventEmitter.prototype);
Foo.prototype.constructor = Foo;
var test = new Foo();
test.emit('foo');
browser environment with inheritance
function Foo() {
EventEmitter.call(this); // super
this.on('foo', this.bar);
}
Foo.prototype.bar = function() {
console.log('foo event emitted');
}
Foo.prototype = Object.create(EventEmitter.prototype);
Foo.prototype.constructor = Foo;
var test = new Foo();
foo.emit('foo');
standalone (commonjs ES6)
import EventEmitter from 'event-emitter';
const ee = new EventEmitter();
class Foo {
constructor() {
ee.on('foo', this.bar);
ee.emit('foo');
}
bar() {
console.log('foo event called');
}
}
Attaches a listener to an event name. The listener can be removed by calling .off(eventName, listener)
. Note that if you pass an anonymous function as the lister, the .off()
method will not be able to correctly remove that listener from the event listener list.
Removes a listener from an event name. The listener will only correctly be removed if it is a named function (with the exception of if there is only a single listener attached).
Attaches a listener to an event name that will only be fired once and then removed after being fired.
Fire off all listeners for a specified event name and pass in any params to them as well. You can pass in any number of arguments you want e.g. this.emit('foo', 'bar', 'baz', 'boz')
or this.emit('foo', { bar: 'baz' })
. Note, a listener must have been attached with .on()
or .once()
before it can be fired with .emit()
.
Gets the specified maxListener count for the EventEmitter. Note this is used only to warn you against attaching too many listeners at once to a single event name.
Sets the maxListener count for the EventEmitter.
Grabs all listeners for a specific event name, or if none is provided simply grabs the entire EventEmitter listeners object.