Skip to content

Instantly share code, notes, and snippets.

@ichabodcole
Forked from anasnakawa/tiny-js-pubsub.js
Last active August 29, 2015 14:08
Show Gist options
  • Save ichabodcole/0777b59e395452e378bf to your computer and use it in GitHub Desktop.
Save ichabodcole/0777b59e395452e378bf to your computer and use it in GitHub Desktop.
/*! Js Pub/Sub
* http://anasnakawa.com/
* Copyright (c) Anas Nakawa
* inspired by Ben Alman's one <https://gist.github.com/cowboy/661855>
* MIT License
*/
(function( p ) {
var e = p.e = {};
p.publish = function( name, data ) {
( e[ name ] = e[ name ] || new Event( name ) ).data = data;
dispatchEvent( e[ name ] );
};
p.subscribe = function( name, handler, context ) {
addEventListener( name, handler.bind( context ) );
};
p.unsubscribe = function( name, handler, context ) {
removeEventListener( name, handler.bind( context ) );
};
})( this.pubsub = {} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment