Last active
December 17, 2015 08:49
-
-
Save datibbaw/5582687 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 || {})); |
erm, no, the last line is supposed to be:
}})(window.datibbaw = window.datibbaw || {});
:)
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
Tried it on the console. Missing a closing curly brace :)
(window.datibbaw = window.datibbaw || {}) })