Skip to content

Instantly share code, notes, and snippets.

@robotlolita
Forked from Raynos/real-easy.js
Last active September 28, 2015 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robotlolita/1412325 to your computer and use it in GitHub Desktop.
Save robotlolita/1412325 to your computer and use it in GitHub Desktop.
pub sub
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)
}
}
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