UNPKG

dfd

Version:

a minimal deferred-like library.

1 lines 1.95 kB
(function(){"use strict";var t,s;t=function(){function t(t){this.scope=null!=t?t:{},this.__queuesStack=[],this.__always=null,this.__fail=null,this.__done=null}return t.prototype.__next=function(){var t,s,e,n,i,o,u;if(o=this.__queuesStack.shift(),!o)return this.__always&&this.__always.apply(this.scope),this.__done&&this.__done.apply(this.scope),void 0;for(n=0,u=o.length,e=!1,t=function(t){return function(s){return t.process=null,e?void 0:s?(t.__always&&t.__always.apply(t.scope),t.__fail&&t.__fail.apply(t.scope,[s]),e=!0,void 0):(--u<=0&&t.__next(),void 0)}}(this),this.processes=[];n<o.length;){i=o[n++];try{this.processes.push(i.apply(this.scope,[t]))}catch(r){return s=r,this.__always&&this.__always.apply(this.scope),this.__fail&&this.__fail.apply(this.scope,[s]),void 0}}},t.prototype.always=function(t){return this.__always=t,this},t.prototype.fail=function(t){return this.__fail=t,this},t.prototype.done=function(t){return this.__done=t,this},t.prototype.then=function(){return"function"==typeof arguments[0]?this.__queuesStack.push(Array.prototype.slice.apply(arguments)):this.__queuesStack.push(arguments[0]),this},t.prototype.wait=function(t){return this.then(function(s){return setTimeout(function(){return s()},t)}),this},t.prototype.resolve=function(){return this.__next(),this},t.prototype.interrupt=function(){var t,s,e,n;if(this.processes){for(n=this.processes,t=0,s=n.length;s>t;t++)e=n[t],"function"==typeof e.abort&&e.abort(),"function"==typeof e.interrupt&&e.interrupt();this.processes=null}return this.__queuesStack=[],this.__always&&this.__always.apply(this.scope),this.__done&&this.__done.apply(this.scope),this.__always=null,this.__done=null,this},t}(),("undefined"!=typeof module&&null!==module?module.exports:void 0)?module.exports=function(s){return new t(s)}:"undefined"!=typeof s&&null!==s?s=function(s){return new t(s)}:"undefined"!=typeof window&&null!==window&&(window.dfd=function(s){return new t(s)})}).call(this);