Created
June 27, 2012 20:27
-
-
Save andrewimm/3006678 to your computer and use it in GitHub Desktop.
Playing JS Golf with PubSub. 171 bytes gzipped
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
| function ps(d){c={};d.pub=function(a,b){for(var i=c[a].length;i;)c[a][--i].apply(this,b)};d.sub=function(a,b){c[a]?c[a].push(b):c[a]=[b]};d.unsub=function(a,b){c[a]=(c[a]||[]).filter(function(i){return i!==b})}} |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ps() adds pub/sub/unsub methods to any object.
For instance:
ps(window);After this,
window.sub(channel, function)setsfunctionto be called whenever data is published tochannel,window.pub(channel, values)applies the array values ofvaluesas the arguments of each callback onchannel,and
window.unsub(channel, function)removesfunctionfrom the list of methods subscribed tochannel.