netsoul
Version:
netsoul protocol library
51 lines (43 loc) • 1.23 kB
JavaScript
// Generated by CoffeeScript 1.6.3
(function() {
var PubSub,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__slice = [].slice;
PubSub = (function() {
function PubSub() {
this.emit = __bind(this.emit, this);
this.on = __bind(this.on, this);
this.subs = {};
}
PubSub.prototype.on = function(event, callback, id) {
if (id == null) {
id = null;
}
if (this.subs[event] == null) {
this.subs[event] = [];
}
return this.subs[event].push({
callback: callback,
id: id
});
};
PubSub.prototype.emit = function() {
var args, event, subscription, _i, _len, _ref, _results;
event = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
if (this.subs[event] == null) {
return false;
}
_ref = this.subs[event];
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
subscription = _ref[_i];
_results.push(subscription.callback.apply(subscription, args));
}
return _results;
};
return PubSub;
})();
module.exports = {
PubSub: PubSub
};
}).call(this);