Skip to content

Instantly share code, notes, and snippets.

@datibbaw
Last active December 17, 2015 08:49
Show Gist options
  • Save datibbaw/5582687 to your computer and use it in GitHub Desktop.
Save datibbaw/5582687 to your computer and use it in GitHub Desktop.
(function(ns) {
ns.PubSub = function() {
// private variables
var events = {},
expando = 'ps' + ("" + Math.random()).substr(2),
guid = 0;
// private functions
function newq(event)
{
return events[event] = {};
}
function queue(event)
{
var key = '$' + event;
return events[key] || newq(key);
}
function add(event, fn)
{
var q = queue(event),
id = fn[expando];
if (id === void 0) {
id = fn[expando] = ++guid;
}
// new function overrides the old
q[id] = fn;
}
function remove(event, fn)
{
var q = queue(event);
if (fn === void 0) {
newq(event);
} else {
delete q[fn[expando]];
}
}
return {
on: function(event, fn) {
add(event, fn);
},
one: function(event, fn) {
var wrapper = function() {
fn.apply(fn, arguments);
remove(event, fn);
}
// wrapper function shares guid with fn
wrapper[expando] = fn[expando] || (fn[expando] = ++guid);
add(event, wrapper);
},
off: function(event, fn) {
remove(event, fn);
},
trigger: function(event) {
var q = queue(event);
for (var id in q) {
if (q.hasOwnProperty(id)) {
q[id].apply(q[id], arguments);
}
}
}
};
}
}(window.datibbaw = window.datibbaw || {}));
@ardydedase
Copy link

Tried it on the console. Missing a closing curly brace :)

(window.datibbaw = window.datibbaw || {}) })

@timotheeg
Copy link

erm, no, the last line is supposed to be:

}})(window.datibbaw = window.datibbaw || {});

:)

@datibbaw
Copy link
Author

Damn, that was a stupid booboo :( I could have sworn it was copied from a working copy ... fixed it, and Tim, I'll look into the proposed enhancements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment