/*! * promise-queue-plus v1.1.0 * Homepage https://github.com/cnwhy/promise-queue-plus * License BSD-2-Clause */ !function e(t,r,n){function o(u,s){if(!r[u]){if(!t[u]){var c="function"==typeof require&&require;if(!s&&c)return c(u,!0);if(i)return i(u,!0);throw new Error("Cannot find module '"+u+"'")}var f=r[u]={exports:{}};t[u][0].call(f.exports,function(e){var r=t[u][1][e];return o(r?r:e)},f,f.exports,e,t,r,n)}return r[u].exports}for(var i="function"==typeof require&&require,u=0;u0?+t:0),function(r){var n,o=h.defer(),s=0,f=function(i){var c=r[i];h.resolve(c).then(function(r){"undefined"!=typeof t?n.push(r):n[i]=r,(0==--s||!e&&t&&n.length>=t)&&o.resolve(n)},function(e){u(t)?o.reject(e):0==--s&&o.resolve(n)}),s++};if(i(r)){n=[],0==r.length&&o.resolve(n);for(var a=0;a=0?+t:0,o(!1,t)(e)}),n("map")&&(h.map=function(e,t,r){function n(){if(c0&&c>0;if(t>0)return t;throw new Error('The "max" value is invalid')}function u(e){try{return j.resolve(e())}catch(e){return j.reject(e)}}function s(e,t){function n(){if(m0){var e,t=d.shift(),r=0,n=+f("timeout",t,s),i=f("retry",t,s),c=f("retryIsJump",t,s),p=t._options.self,h=function(){return e&&clearTimeout(e),e=0,!!r++||void m--},v=function(){l(t,s,"workFinally",s,s,t)},w=function(e){h()||(t.defer.resolve(e),l(t,s,"workResolve",s,e,s,t),v())},E=function(e){h()||(i>t._errNo++?s._addItem(t,c,!0,!1):(t.defer.reject(e),l(t,s,"workReject",s,e,s,t)),v())};0!=m||j||(j=!0,a.call(s,"queueStart",s,s));u(function(){return t.fn.apply(p||null,t.regs)}).then(w,E).then(function(){d.length>0?o():0==m&&j&&(j=!1,a.call(s,"queueEnd",s,s))});return m+=1,void(n>0&&(e=setTimeout(function(){E("timeout")},n)))}return!0}function o(){for(;!n(););}var s=this,h={queueStart:null,queueEnd:null,workAdd:null,workResolve:null,workReject:null,workFinally:null,retry:0,retryIsJump:!1,timeout:0},d=[],y=r(e),m=0,j=!1,g=0;if(this._options=h,this.onError=w,i.isObject(t))for(var E in t)h.hasOwnProperty(E)&&(h[E]=t[E]);this.getMax=function(){return y},this.setMax=function(e){try{y=r(e),!g&&m&&s.start()}catch(e){v.call(s,e)}},this.getLength=function(){return d.length},this.getRunCount=function(){return m},this.isStart=function(){return!!j},this._addItem=function(e,t,r,n){if(!(e instanceof c))throw new TypeError('"unit" is not QueueUnit');t?d.unshift(e):d.push(e),n||p.call(s,e),r?s.start():j&&o()},this.start=function(){g=0,o()},this.stop=function(){g=1},this.clear=function(e){for(;d.length;){var t=d.shift();t.defer.reject(e)}}}function c(e,t,r){function n(e){for(var t=0;t1&&(this._options[e]=arguments[1]))},push:function(){var e=this,t=h.apply(e,arguments);return e._addItem(t,!1),t.defer.promise},unshift:function(){var e=this,t=h.apply(e,arguments);return e._addItem(t,!0),t.defer.promise},go:function(){var e=this,t=h.apply(e,arguments);return e._addItem(t,!1,!0),t.defer.promise},jump:function(){var e=this,t=h.apply(e,arguments);return e._addItem(t,!0,!0),t.defer.promise},add:function(e,t){var r,n,o,u,s,c=this,f=1;if(!i.isFunction(e))throw new TypeError("Queues only support function, '"+e+"' is not function");return r=function(){var t=j.defer();return e(t.resolve,t.reject),t.promise},n=[r],i.isObject(t)&&(n.push(t),f++),o=!!arguments[f],u=!!arguments[f+1],s=u?c.unshift.apply(c,n):c.push.apply(c,n),o&&c.start(),s},addArray:function(e,t,r){for(var n=[],o=this,i=0;i