-
Notifications
You must be signed in to change notification settings - Fork 6
/
nerve.min.js
1 lines (1 loc) · 1.36 KB
/
nerve.min.js
1
(function(){function t(e,t){if(!t)return null;var n=0,r=t.length;for(;n<r;n++){if(t[n].callee===e)return t[n]}return null}var e={};window.nerve={on:function(n,r,i,s){var o=n,u=null,a=null,f=null;if(arguments.length==1){throw Error("A channel and a callback must be specified")}else if(arguments.length==2){if(Object.prototype.toString.call(arguments[1])=="[object Function]"){a=arguments[1];f=arguments.callee}}else if(arguments.length==3&&Object.prototype.toString.call(arguments[2])=="[object Function]"){if(Object.prototype.toString.call(arguments[2])=="[object Function]"){u=arguments[1];a=arguments[2];f=arguments[3]||arguments.callee}else{throw Error("Last parameter must be a callback function")}}else if(arguments.length==4){o=n;u=r;a=i;f=s||arguments.callee}if(!a){return}if(!e[n]){e[n]=[]}if(!u){u="root"}if(u&&!e[n][u]){e[n][u]=[]}if(t(f,e[n][u]))return;e[n][u].push({callee:f,callback:a})},off:function(t,n,r){if(e[t]){var i="root",s=r||arguments.callee;if(n)i=n;if(!e[t][i])return;var o=0,u=e[t][i].length;for(;o<u;o++){if(e[t][i][o].callee===s)delete e[t][i][o]}}},send:function(t,n,r){var i="root",s=null;if(arguments.length==2){s=arguments[1]}else if(arguments.length==3){i=n;s=r}if(!e[t]||!e[t][i]){return}var o=e[t][i],u=0,a=o.length;for(;u<a;u++){(function(t,n,r){var i=setTimeout(function(){try{e[t][n][r].callback(s);clearTimeout(i)}catch(o){return}})})(t,i,u)}}}})()