-
-
Save robotlolita/1412325 to your computer and use it in GitHub Desktop.
pub sub
This file contains hidden or 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
var pubsub = { | |
// A map of event names to the listeners attached to it. | |
// :: { "String" -> [Function] } | |
events: { } | |
// Notifies all the listeners of an event | |
// :: String, a... -> Undefined | |
, pub: function(event) { | |
var events = this.events[event] || [] | |
var args = arguments | |
events.forEach(function(listener){ listener.apply(null, args) }) | |
} | |
// Attaches a listener to an event | |
// :: String, Function -> Undefined | |
, sub: function(event, listener) { | |
this.events[event] = this.events[event] || [] | |
this.events[event].push(listener) | |
} | |
} |
This file contains hidden or 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
var pubsub = { | |
evs: {} | |
, pub: function(e,d){(this.evs[e]||[]).forEach(function(h){h(d)})} | |
, sub: function(e,h){(this.evs[e]||(this.evs[e]=[])).push(h)} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment