forked from Takazudo/jQuery.TmplDeck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsdeferred.js
3 lines (3 loc) · 6.62 KB
/
jsdeferred.js
1
2
3
// JSDeferred 0.4.0 Copyright (c) 2007 cho45 ( www.lowreal.net )
// See http://github.com/cho45/jsdeferred
function Deferred(){return(this instanceof Deferred)?this.init():new Deferred();}Deferred.ok=function(a){return a;};Deferred.ng=function(a){throw a;};Deferred.prototype={_id:250149748310446,init:function(){this._next=null;this.callback={ok:Deferred.ok,ng:Deferred.ng};return this;},next:function(a){return this._post("ok",a);},error:function(a){return this._post("ng",a);},call:function(a){return this._fire("ok",a);},fail:function(a){return this._fire("ng",a);},cancel:function(){(this.canceller||function(){})();return this.init();},_post:function(b,a){this._next=new Deferred();this._next.callback[b]=a;return this._next;},_fire:function(a,c){var b="ok";try{c=this.callback[a].call(this,c);}catch(d){b="ng";c=d;if(Deferred.onerror){Deferred.onerror(d);}}if(Deferred.isDeferred(c)){c._next=this._next;}else{if(this._next){this._next._fire(b,c);}}return this;}};Deferred.isDeferred=function(a){return !!(a&&a._id==Deferred.prototype._id);};Deferred.next_default=function(a){var b=new Deferred();var c=setTimeout(function(){b.call();},0);b.canceller=function(){clearTimeout(c);};if(a){b.callback.ok=a;}return b;};Deferred.next_faster_way_readystatechange=((typeof window==="object")&&(location.protocol=="http:")&&!window.opera&&/\bMSIE\b/.test(navigator.userAgent))&&function(a){var f=new Deferred();var c=new Date().getTime();if(c-arguments.callee._prev_timeout_called<150){var e=false;var b=document.createElement("script");b.type="text/javascript";b.src="data:text/javascript,";b.onreadystatechange=function(){if(!e){f.canceller();f.call();}};f.canceller=function(){if(!e){e=true;b.onreadystatechange=null;document.body.removeChild(b);}};document.body.appendChild(b);}else{arguments.callee._prev_timeout_called=c;var g=setTimeout(function(){f.call();},0);f.canceller=function(){clearTimeout(g);};}if(a){f.callback.ok=a;}return f;};Deferred.next_faster_way_Image=((typeof window==="object")&&(typeof(Image)!="undefined")&&!window.opera&&document.addEventListener)&&function(a){var e=new Deferred();var b=new Image();var c=function(){e.canceller();e.call();};b.addEventListener("load",c,false);b.addEventListener("error",c,false);e.canceller=function(){b.removeEventListener("load",c,false);b.removeEventListener("error",c,false);};b.src="data:image/png,"+Math.random();if(a){e.callback.ok=a;}return e;};Deferred.next_tick=(typeof process==="object"&&typeof process.nextTick==="function")&&function(a){var b=new Deferred();process.nextTick(function(){b.call();});if(a){b.callback.ok=a;}return b;};Deferred.next=Deferred.next_faster_way_readystatechange||Deferred.next_faster_way_Image||Deferred.next_tick||Deferred.next_default;Deferred.chain=function(){var c=Deferred.next();for(var b=0,a=arguments.length;b<a;b++){(function(g){switch(typeof g){case"function":var d=null;try{d=g.toString().match(/^\s*function\s+([^\s()]+)/)[1];}catch(f){}if(d!="error"){c=c.next(g);}else{c=c.error(g);}break;case"object":c=c.next(function(){return Deferred.parallel(g);});break;default:throw"unknown type in process chains";}})(arguments[b]);}return c;};Deferred.wait=function(e){var b=new Deferred(),a=new Date();var c=setTimeout(function(){b.call((new Date).getTime()-a.getTime());},e*1000);b.canceller=function(){clearTimeout(c);};return b;};Deferred.call=function(a){var b=Array.prototype.slice.call(arguments,1);return Deferred.next(function(){return a.apply(this,b);});};Deferred.parallel=function(d){if(arguments.length>1){d=Array.prototype.slice.call(arguments);}var c=new Deferred(),a={},b=0;for(var e in d){if(d.hasOwnProperty(e)){(function(g,f){if(typeof g=="function"){g=Deferred.next(g);}g.next(function(h){a[f]=h;if(--b<=0){if(d instanceof Array){a.length=d.length;a=Array.prototype.slice.call(a,0);}c.call(a);}}).error(function(h){c.fail(h);});b++;})(d[e],e);}}if(!b){Deferred.next(function(){c.call();});}c.canceller=function(){for(var f in d){if(d.hasOwnProperty(f)){d[f].cancel();}}};return c;};Deferred.earlier=function(d){if(arguments.length>1){d=Array.prototype.slice.call(arguments);}var c=new Deferred(),a={},b=0;for(var e in d){if(d.hasOwnProperty(e)){(function(g,f){g.next(function(h){a[f]=h;if(d instanceof Array){a.length=d.length;a=Array.prototype.slice.call(a,0);}c.canceller();c.call(a);}).error(function(h){c.fail(h);});b++;})(d[e],e);}}if(!b){Deferred.next(function(){c.call();});}c.canceller=function(){for(var f in d){if(d.hasOwnProperty(f)){d[f].cancel();}}};return c;};Deferred.loop=function(e,a){var d={begin:e.begin||0,end:(typeof e.end=="number")?e.end:e-1,step:e.step||1,last:false,prev:null};var b,c=d.step;return Deferred.next(function(){function f(g){if(g<=d.end){if((g+c)>d.end){d.last=true;d.step=d.end-g+1;}d.prev=b;b=a.call(this,g,d);if(Deferred.isDeferred(b)){return b.next(function(h){b=h;return Deferred.call(f,g+c);});}else{return Deferred.call(f,g+c);}}else{return b;}}return(d.begin<=d.end)?Deferred.call(f,d.begin):null;});};Deferred.repeat=function(e,b){var d=0,a={},c=null;return Deferred.next(function(){var f=(new Date()).getTime();do{if(d>=e){return null;}c=b(d++);}while((new Date()).getTime()-f<20);return Deferred.call(arguments.callee);});};Deferred.register=function(b,a){this.prototype[b]=function(){var c=arguments;return this.next(function(){return a.apply(this,c);});};};Deferred.register("loop",Deferred.loop);Deferred.register("wait",Deferred.wait);Deferred.connect=function(c,b){var f,d,e;if(typeof arguments[1]=="string"){f=arguments[0];d=f[arguments[1]];e=arguments[2]||{};}else{d=arguments[0];e=arguments[1]||{};f=e.target;}var a=e.args?Array.prototype.slice.call(e.args,0):[];var h=isFinite(e.ok)?e.ok:e.args?e.args.length:undefined;var g=e.ng;return function(){var k=new Deferred().next(function(m){var n=this._next.callback.ok;this._next.callback.ok=function(){return n.apply(this,m.args);};});var i=a.concat(Array.prototype.slice.call(arguments,0));if(!(isFinite(h)&&h!==null)){h=i.length;}var l=function(){k.call(new Deferred.Arguments(arguments));};i.splice(h,0,l);if(isFinite(g)&&g!==null){var j=function(){k.fail(arguments);};i.splice(g,0,j);}Deferred.next(function(){d.apply(f,i);});return k;};};Deferred.Arguments=function(a){this.args=Array.prototype.slice.call(a,0);};Deferred.retry=function(f,g,b){if(!b){b={};}var c=b.wait||0;var e=new Deferred();var a=function(){var d=g(f);d.next(function(h){e.call(h);}).error(function(h){if(--f<=0){e.fail(["retry failed",h]);}else{setTimeout(a,c*1000);}});};setTimeout(a,0);return e;};Deferred.methods=["parallel","wait","next","call","loop","repeat","chain"];Deferred.define=function(d,c){if(!c){c=Deferred.methods;}if(!d){d=(function b(){return this;})();}for(var a=0;a<c.length;a++){var e=c[a];d[e]=Deferred[e];}return Deferred;};this.Deferred=Deferred;