-
Notifications
You must be signed in to change notification settings - Fork 0
/
chainloading.min.js
18 lines (18 loc) · 8.64 KB
/
chainloading.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
(function(y,g){function r(a,b,c){b=b||{length:0};var d;switch(b.length){case 0:for(d=0;d<a.length;d++)a[d].call(c);break;case 1:for(d=0;d<a.length;d++)a[d].call(c,b[0]);break;case 2:for(d=0;d<a.length;d++)a[d].call(c,b[0],b[1]);break;case 3:for(d=0;d<a.length;d++)a[d].call(c,b[0],b[1],b[2]);break;default:for(d=0;d<a.length;d++)a[d].apply(c,b)}}function m(a,b){this.chain=a;this.ready=b||!1;this.completed=!0;this.state="";this.dfds=[];this.failCallbacks=[];this.failedArgs=this.prevLevel=this.nextLevel=
null}function z(a,b,c,d){function m(a){h||(h=!0,e.length=0,b(a,q,q.args))}function s(){for(var a=0;a<e.length&&l&&!h&&0!==e[a].s;a++)null!==e[a].args&&q.args.push.apply(q.args,e[a].args),e.splice(a,1),a--;0===e.length&&m("resolved")}this.args=[];this.promise=new n;var q=this,p=this.promise,e=[],h=!1,l=!1,k,t;k=0;for(t=a.length;k<t;k++){if(a[k]===g)throw Error("Undefined sent to push/add. Did you forget to return a deferred?");e.push({d:a[k],s:0,args:null})}k=0;for(t=e.length;k<t&&!h;k++){if("function"!==
typeof e[k].d.then){if("function"===typeof e[k].d){e[k].s=1;this.promise.done(e[k].d);continue}throw Error("Invalid deferred sent to chain");}(function(a){a.d.then(function(){if(0===a.s){a.args=f.call(arguments);a.s=1;var b=this;p.done(function(){d.set(b);for(var a=0;a<c.length;a++)b===c[a].d&&(p.done(c[a].func),c.splice(a,1),a--)});s()}},function(){if(0===a.s){q.args=f.call(arguments);a.s=1;var b=this;p.fail(function(){d.set(b);for(var a=0;a<c.length;a++)b===c[a].d&&(p.fail(c[a].func),c.splice(a,
1),a--)});m("rejected")}})})(e[k])}l=!0;s()}function n(a){this.ctx=a||this;this.s="pending";this.callbacks=this.args=null}function u(){}function l(){function a(){if(p!==g)for(var a=0;a<p.length;a++)p[a].call(l),p.splice(a,1),a--}function b(){var b=h!==g,c=new m(l,!b||h.completed);p!==g&&c.addFailCallback(a);b&&(h.nextLevel=c,c.prevLevel=h,h.removeFailCallback(a));return h=c}function c(a,c){if(h!==g&&"rejected"===h.normalizedState())return new u;b();return h[a](c,s,q)}function d(a,c){if(h===g)b();
else if("rejected"===h.normalizedState())return new u;return h[a](c,s,q)}var l=this,s=[],q=new v,p,e,h,r;this.push=this.next=function(){return c("requireDeferreds",f.call(arguments))};this.add=function(){return d("requireDeferreds",f.call(arguments))};this.pushIgnoreFail=this.nextIgnoreFail=function(){return c("optionalDeferreds",f.call(arguments))};this.addIgnoreFail=function(){return d("optionalDeferreds",f.call(arguments))};this.stop=function(){var a=h;if(a===g)a=b();else for(;null!==a.prevLevel&&
(a=a.prevLevel););a.stop();return this};this.fail=this["catch"]=function(a){h===g?(p===g&&(p=[]),p.push(a)):h.addFailCallback(a);return this};var k=function(a,b,c){return function(){var d;d=c===g?function(){a.apply(b,f.call(arguments))}:function(){a.apply(b,c.concat(f.call(arguments)));c.length=0};if(this===l||this instanceof n||q.has(this))d.apply(this,f.call(arguments));else{if("function"!==typeof this.state)throw Error("You tried to use chain.bind with an unsupported deferred library. See README.md and stop using chain.bind");
s.push({d:this,func:d,s:this.state()})}}};this.bind=function(a,b){return 3>arguments.length?k(a,b):k(a,b,f.call(arguments,2))};this.applyArgs=function(a,b){if(3>arguments.length)return e===g&&(e=[]),k(a,b,e);var c=f.call(arguments,2);return function(){var d=(e||[]).splice(0,e.length).concat(c).concat(f.call(arguments));k(a,b,d).call(this)}};this.storeArgs=function(){var a=f.call(arguments),b=function(){e===g&&(e=[]);e.push.apply(e,a)};if(this===l)return b;this instanceof n||q.has(this)?b():s.push({d:this,
func:b,s:this.state()})};this.state=function(){return h===g?"pending":h.normalizedState()};this.promise=function(){if(r===g){var a=function(a,b){return function(c,d,e){return a.call(b,c,d,e)}};r={state:this.state,done:a(this.done,this),fail:a(this.fail,this),always:a(this.always,this),then:a(this.then,this)}}return r}}var v=y.WeakMap,f=[].slice,w=[].indexOf,A=function(){return this};v===g&&(v=function(){var a=[];this.has=function(b){var c=-1;for(b!==g&&(c=a.length-1);0<=c&&a[c]!==b;)c--;return-1<
c};this.set=function(b){b!==g&&a.push(b)}});w===g&&(w=function(a,b){var c=this.length,d=+b||0;if(0===c||d>=c)return-1;isFinite(d)||(d=0);for(0>d&&(d=Math.max(c-Math.abs(d),0));d<c;){if(this[d]===a)return d;d++}return-1});m.prototype.newGroup=function(a,b,c,d){""===this.state&&(this.completed=!1);var g={s:0,r:this.ready},f=this;this.dfds.push(g);return new z(a,function(a,c,d){g.g=c;g.s=1;switch(a){case "resolved":""===f.state&&(f.state="resolved");break;case "rejected":"stopped"!==f.state&&(f.state=
b,f.failedArgs=d)}f.complete(d)},c,d)};m.prototype.requireDeferreds=function(a,b,c){return this.newGroup(a,"rejected",b,c).promise};m.prototype.optionalDeferreds=function(a,b,c){return this.newGroup(a,"ignored",b,c).promise};m.prototype.addFailCallback=function(a){"ignored"===this.state||"rejected"===this.state?r([a],this.failedArgs,this.chain):this.completed&&""!==this.state||this.failCallbacks.push(a)};m.prototype.removeFailCallback=function(a){a=w.call(this.failCallbacks,a);-1!==a&&this.failCallbacks.splice(a,
1)};m.prototype.complete=function(a){if(!0===this.ready){var b="ignored"===this.state?"rejected":this.normalizedState(),c;for(c=0;c<this.dfds.length;c++){if("function"===typeof this.dfds[c])this.dfds[c]();else{this.dfds[c].r=!0;if(0===this.dfds[c].s)break;this.dfds[c].g.complete(b)}this.dfds.splice(c,1);c--}0<this.dfds.length||("rejected"===this.state?this.completeFailed(a):(this.cleanup(),null!==this.nextLevel&&(this.nextLevel.ready=!0,this.nextLevel.complete(a)),this.cleanupLevelRefs()))}};m.prototype.completeFailed=
function(a){this.failedArgs=this.failedArgs||a;r(this.failCallbacks,this.failedArgs,this.chain);this.failCallbacks.splice(0,this.failCallbacks.length);this.cleanup();null!==this.nextLevel&&this.nextLevel.completeFailed(this.failedArgs);this.cleanupLevelRefs()};m.prototype.cleanup=function(a){var b;a=0;for(b=this.dfds.length;a<b;a++)this.dfds[a].r=!1;this.dfds.splice(0,this.dfds.length);this.completed=!0;this.failCallbacks.splice(0,this.failCallbacks.length)};m.prototype.cleanupLevelRefs=function(){this.nextLevel=
null;null!==this.prevLevel&&(this.prevLevel=this.prevLevel.failedArgs=null)};m.prototype.normalizedState=function(){return"stopped"===this.state?"rejected":"ignored"===this.state?"resolved":this.state||"pending"};m.prototype.stop=function(a){this.ready=!1;this.state="stopped";this.cleanup();null!==this.nextLevel&&this.nextLevel.stop();this.cleanupLevelRefs()};z.prototype.complete=function(a){var b=this.promise.callbacks,c=[];if("pending"===this.promise.s&&(this.promise.args=this.args,this.promise.s=
a,null!==b)){for(a=0;a<b.length;a++)b[a].s===this.promise.s&&c.push(b[a].f);this.promise.callbacks=null;r(c,this.promise.args,this.promise.ctx)}};n.prototype.done=function(){var a,b;if("resolved"===this.s)r(f.call(arguments),this.args,this.ctx);else if("pending"===this.s)for(a=0,b=arguments.length;a<b;a++){if("function"!==typeof arguments[a])throw Error("Invalid function sent to done "+typeof arguments[a]);null===this.callbacks&&(this.callbacks=[]);this.callbacks.push({s:"resolved",f:arguments[a]})}return this};
n.prototype.then=function(a,b){"function"===typeof a?this.done(a):a instanceof Array&&this.done.apply(this,a);"function"===typeof b?this.fail(b):b instanceof Array&&this.fail.apply(this,b);return this};n.prototype.fail=function(){var a,b;if("rejected"===this.s)r(f.call(arguments),this.args,this.ctx);else if("pending"===this.s)for(a=0,b=arguments.length;a<b;a++){if("function"!==typeof arguments[a])throw Error("Invalid function sent to fail "+typeof arguments[a]);null===this.callbacks&&(this.callbacks=
[]);this.callbacks.push({s:"rejected",f:arguments[a]})}return this};n.prototype["catch"]=n.prototype.fail;n.prototype.always=function(){var a=f.call(arguments);return this.fail.apply(this,a).done.apply(this,a)};n.prototype.state=function(){return this.s};for(var x in n.prototype)n.prototype.hasOwnProperty(x)&&"function"===typeof n.prototype[x]&&(u.prototype[x]=A);u.prototype.state=function(){return"pending"};l.prototype.done=l.prototype.after=function(a,b){b?this.add(a):this.push(a);return this};
l.prototype.then=function(a,b){var c,d;if("function"===typeof a)this.done(a);else if(a instanceof Array)for(c=0,d=a.length;c<d;c++)this.done(a[c]);if("function"===typeof b)this.fail(b);else if(b instanceof Array)for(c=0,d=b.length;c<d;c++)this.fail(b[c]);return this};l.prototype.always=function(a){if(1===arguments.length)return this.fail(a).done(a);var b=f.call(arguments);return this.fail.apply(this,b).done.apply(this,b)};l.prototype.fork=function(){var a=new l;a.push(this);return a};"undefined"!==
typeof module?module.exports=l:(y.ChainLoading=l,"function"===typeof define&&"object"===typeof define.amd&&define.amd&&define("ChainLoading",function(){return l}))})(this);