2014-05-23 18:14:06 +00:00
|
|
|
// Based on https://github.com/diy/intercom.js/blob/master/lib/events.js
|
|
|
|
// Copyright 2012 DIY Co Apache License, Version 2.0
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-03-12 20:49:20 +00:00
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
function removeItem(item, array) {
|
|
|
|
for (var i = array.length - 1; i >= 0; i--) {
|
|
|
|
if (array[i] === item) {
|
|
|
|
array.splice(i, 1);
|
2014-03-12 20:49:20 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-23 18:14:06 +00:00
|
|
|
return array;
|
|
|
|
}
|
2014-03-12 20:49:20 +00:00
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
var EventEmitter = function() {};
|
2014-03-12 20:49:20 +00:00
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
EventEmitter.createInterface = function(space) {
|
|
|
|
var methods = {};
|
2014-03-12 20:49:20 +00:00
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
methods.on = function(name, fn) {
|
|
|
|
if (typeof this[space] === 'undefined') {
|
|
|
|
this[space] = {};
|
|
|
|
}
|
|
|
|
if (!this[space].hasOwnProperty(name)) {
|
|
|
|
this[space][name] = [];
|
|
|
|
}
|
|
|
|
this[space][name].push(fn);
|
|
|
|
};
|
2014-03-12 20:49:20 +00:00
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
methods.off = function(name, fn) {
|
|
|
|
if (typeof this[space] === 'undefined') return;
|
|
|
|
if (this[space].hasOwnProperty(name)) {
|
|
|
|
removeItem(fn, this[space][name]);
|
|
|
|
}
|
|
|
|
};
|
2014-03-12 20:49:20 +00:00
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
methods.trigger = function(name) {
|
|
|
|
if (typeof this[space] !== 'undefined' && this[space].hasOwnProperty(name)) {
|
|
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
|
|
|
for (var i = 0; i < this[space][name].length; i++) {
|
|
|
|
this[space][name][i].apply(this[space][name][i], args);
|
2014-03-12 20:49:20 +00:00
|
|
|
}
|
2014-05-23 18:14:06 +00:00
|
|
|
}
|
|
|
|
};
|
2014-03-12 20:49:20 +00:00
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
methods.removeAllListeners = function(name) {
|
|
|
|
if (typeof this[space] === 'undefined') return;
|
|
|
|
var self = this;
|
|
|
|
self[space][name].forEach(function(fn) {
|
|
|
|
self.off(name, fn);
|
|
|
|
});
|
2014-03-12 20:49:20 +00:00
|
|
|
};
|
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
return methods;
|
|
|
|
};
|
2014-03-12 20:49:20 +00:00
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
var pvt = EventEmitter.createInterface('_handlers');
|
|
|
|
EventEmitter.prototype._on = pvt.on;
|
|
|
|
EventEmitter.prototype._off = pvt.off;
|
|
|
|
EventEmitter.prototype._trigger = pvt.trigger;
|
|
|
|
|
|
|
|
var pub = EventEmitter.createInterface('handlers');
|
|
|
|
EventEmitter.prototype.on = function() {
|
|
|
|
pub.on.apply(this, arguments);
|
|
|
|
Array.prototype.unshift.call(arguments, 'on');
|
|
|
|
this._trigger.apply(this, arguments);
|
|
|
|
};
|
|
|
|
EventEmitter.prototype.off = pub.off;
|
|
|
|
EventEmitter.prototype.trigger = pub.trigger;
|
|
|
|
EventEmitter.prototype.removeAllListeners = pub.removeAllListeners;
|
2014-03-12 20:49:20 +00:00
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
module.exports = EventEmitter;
|