Skip to content

Instantly share code, notes, and snippets.

@mrbobbybryant
Last active December 17, 2015 20:28
Show Gist options
  • Save mrbobbybryant/ad08682b028669e3f3c4 to your computer and use it in GitHub Desktop.
Save mrbobbybryant/ad08682b028669e3f3c4 to your computer and use it in GitHub Desktop.
pub/sub pattern
var events = {
events: {},
subscribe: function (eventName, fn) {
this.events[eventName] = this.events[eventName] || [];
this.events[eventName].push(fn);
},
unsubscribe: function(eventName, fn) {
if (this.events[eventName]) {
this.events[ eventName ] = this.events[ eventName ].filter( function( eventFn ) {
return eventFn !== fn;
} );
}
},
publish: function (eventName, data) {
if (this.events[eventName]) {
this.events[eventName].forEach(function(fn) {
fn(data);
});
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment