eemitter

0.2.0 • Public • Published

eemitter

Simple custom event emitter.

Build Status

install

npm install eemitter

usage

Basic usage:

var EventEmitter = require('eemitter');
var ee = new EventEmitter();
 
ee.on('message', function (msg) {
    console.log(msg);
});
 
ee.emit('message', 'hello, world!');
// hello, world!
ee.off('message');

As a mixin:

var EventEmitter = require('eemitter');
 
function Person(name) {
    this.name = name;
}
Person.prototype = new EventEmitter();
Person.prototype.constructor = Person;
// you can also do Person.prototype = Object.create(EventEmitter.prototype);
 
Person.prototype.sayName = function () {
    this.emit('talk', this.name);
};
 
var cameron = new Person('Cameron');
cameron.on('talk', function (msg) {
    console.log(msg);
})
cameron.sayName();
// Cameron
 

Bind an event handler for one message:

var ee = new EventEmitter();
ee.one('myevent', function () {
    console.log('should only be called once')
});
ee.emit('myevent');
ee.emit('myevent'); // nothing

Remove all handlers:

var ee = new EventEmitter();
ee.on('myevent1', function () {
    console.log('should not be called')
});
ee.on('myevent2', function () {
    console.log('should not be called')
});
ee.off();
ee.emit('myevent1'); // nothing
ee.emit('myevent2'); // nothing

License

(The MIT License)

Copyright 2014 Cameron Lakenen

Readme

Keywords

none

Package Sidebar

Install

npm i eemitter

Weekly Downloads

1

Version

0.2.0

License

MIT

Last publish

Collaborators

  • lakenen