promise-queue-plus.min.js 11 KB

123456
  1. /*!
  2. * promise-queue-plus v1.1.0
  3. * Homepage https://github.com/cnwhy/promise-queue-plus
  4. * License BSD-2-Clause
  5. */
  6. !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;u<n.length;u++)o(n[u]);return o}({1:[function(e,t,r){!function(e,t){if("function"==typeof define&&(define.amd||define.cmd))define([],t);else{if("undefined"==typeof window&&"undefined"==typeof self)throw new Error('Loading the "'+e+'" module failed!');var r="undefined"!=typeof window?window:self;r[e]=t()}}("PromiseQueuePlus",function(){return e("../src/queue")(e("easy-promise/setTimeout"))})},{"../src/queue":6,"easy-promise/setTimeout":2}],2:[function(e,t,r){t.exports=e("./src")(function(e){setTimeout(e,0)})},{"./src":3}],3:[function(e,t,r){"use strict";t.exports=function(e){function t(e,n){if(r(n))n.then(e.resolve,e.reject);else if(!n||"function"!=typeof n&&"object"!=typeof n)e.resolve(n);else{var o,i=!1;try{o=n.then,"function"==typeof o?o.call(n,function(r){i||(i=!0,t(e,r))},function(t){i||(i=!0,e.reject(t))}):e.resolve(n)}catch(t){i||(i=!0,e.reject(t))}}}function r(e){return e instanceof n}function n(e){function r(e){o.call(c,1,e)}function n(e){o.call(c,0,e)}function i(e){if(!f)return f=!0,c===e?n(new TypeError("The promise and its value refer to the same object")):void t({resolve:r,reject:n},e)}function u(e){f||(f=!0,n(e))}var c=this;this.status=-1,this._events=[];var f=!1;if(this.resolve=i,this.reject=u,e!==s&&"function"==typeof e)try{e(this.resolve,this.reject)}catch(e){this.reject(e)}}function o(e,t){var r=this;~this.status||(this.status=e,e?this.value=t:this.reason=t,i.call(r))}function i(){if(~this.status){for(var e=this,t=e._events,r=e.status?e.value:e.reason,n=e.status?0:1,o=0;o<t.length;o++)!function(t){var o=t[n],i=t[2];u(o,r,i,e.status)}(t[o]);t=[]}}function u(t,r,n,o){var i=n.resolve,u=n.reject;"function"==typeof t?e(function(){var e;try{e=t(r)}catch(e){return void u(e)}i(e)}):o?i(r):u(r)}var s=function(){};return n.defer=function(){var e=new n(s);return{promise:e,resolve:e.resolve,reject:e.reject}},n.resolve=function(e){return r(e)?e:new n(function(t,r){t(e)})},n.reject=function(e){return new n(function(t,r){r(e)})},n.prototype.toString=function(){return"[object Promise]"},n.prototype.then=function(e,t){var r=this.status,o=n.defer(),i=o.promise;return~r?r&&"function"==typeof e?u(e,this.value,i,r):r||"function"!=typeof t?r?o.resolve(this.value):o.reject(this.reason):u(t,this.reason,i,r):this._events.push([e,t,i]),i},n}},{}],4:[function(e,t,r){"use strict";function n(e,t,r){function n(e){if(i(r)){for(var t=!1,n=0;n<r.length;n++)if(r[n]==e){t=!0;break}if(!t)return!1}return!v||!s(h[e])}function o(e,t){return u(t)||(t=+t>0?+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<r.length;a++)f(a)}else if(e&&c(r)){var l=0;n={};for(var a in r)f(a),l++;0==l&&o.resolve(n)}else o.reject(new TypeError("参数错误"));return o.promise}}function a(e){for(var t=h.defer(),r=0;r<e.length;r++)!function(){var n=r,o=e[n];h.resolve(o).then(function(e){t.resolve(e)},function(e){t.reject(e)})}();return t.promise}function l(e){return function(t,r){return t?e.reject(t):void e.resolve(r)}}function p(e){var t=this===h?null:this,r=h.defer(),n=f(arguments,1);return n.push(l(r)),e.apply(t,n),r.promise}var h,v;if(t?(v=!0,h=t):h=e,h.Promise||e==t||(h.Promise=e),s(e)&&s(e.prototype.then))h.defer=function(){var t,r,n=new e(function(e,n){t=e,r=n});return{promise:n,resolve:t,reject:r}};else if(s(e.defer))h.defer=function(){return e.defer()};else{if(!s(e.deferred))throw new TypeError("此类不支持扩展!");h.defer=function(){return e.deferred()}}return n("delay")&&(h.delay=function(e,t){var r=h.defer();return setTimeout(function(){r.resolve(t)},e),r.promise}),n("resolve")&&(h.resolve=function(e){var t=h.defer();return t.resolve(e),t.promise}),n("reject")&&(h.reject=function(e){var t=h.defer();return t.reject(e),t.promise}),n("all")&&(h.all=o()),n("allMap")&&(h.allMap=o(!0)),n("some")&&(h.some=function(e,t){return t=+t>=0?+t:0,o(!1,t)(e)}),n("map")&&(h.map=function(e,t,r){function n(){if(c<f.length){var r=f[c],s=h.resolve(t(e[r],r,e)).then(function(e){return n(),e},o.reject);i.push(s),u--,c++}else h.all(i).then(o.resolve,o.reject)}var o=h.defer(),i=[],u=r?+r.concurrency:0;if(0==u||u!=u){for(var s in e)i.push(t(e[s],s,e));return h.all(i).then(o.resolve,o.reject),o.promise}var c=0,f=function(){var t=[];for(var r in e)t.push(r);return t}();do n();while(u>0&&c<f.length);return o.promise}),n("race")&&(h.race=a),n("any")&&(h.any=a),n("nfcall")&&(h.nfcall=p),n("nfapply")&&(h.nfapply=function(e,t){var r=this===h?null:this,n=h.defer();if(!i(t))throw TypeError('"args" is not Array');return t.push(l(n)),e.apply(r,t),n.promise}),h.denodeify=function(e){var t=this===h?null:this;return function(){return p.apply(t,[].concat([e],f(arguments)))}},h}var o=e("./utils"),i=o.isArray,u=o.isEmpty,s=o.isFunction,c=o.isPlainObject,f=o.arg2arr;t.exports=n},{"./utils":5}],5:[function(e,t,r){"use strict";r.isPlainObject=function(e){return null!==e&&"object"==typeof e&&!e.nodeType&&e!==e.window&&!(e.constructor&&!Object.prototype.hasOwnProperty.call(e.constructor.prototype,"isPrototypeOf"))},r.isArray=function(e){return"[object Array]"==Object.prototype.toString.call(e)},r.isFunction=function(e){return"function"==typeof e},r.isEmpty=function(e){return"undefined"==typeof e||null===e},r.arg2arr=function(e,t,r){return Array.prototype.slice.call(e,t,r)}},{}],6:[function(e,t,r){"use strict";function n(e){function t(e){j=s.Q=s.Promise=o(e,{})}function r(e){var t=+e>>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(m<y&&!g&&d.length>0){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;t<u.length;t++)if(e===u[t])return!0;return!1}var o={workResolve:!0,workReject:!0,workFinally:!0,queueEventTrigger:!0,regs:[],self:null},u=["workResolve","workReject","workFinally","queueEventTrigger","retry","retryIsJump","timeout","self"],s=1;if(!i.isFunction(e))throw new TypeError("Queues only support function, '"+e+"' is not function");this.fn=e,this._errNo=0,this.defer=j.defer(),i.isArray(t)&&(this.regs=t,s++),this._options=o;var c=arguments[s];if(i.isObject(c))for(var f in c)n(f)&&(o[f]=c[f])}function f(e,t,r){return e in t._options?t._options[e]:r._options[e]}function a(e,t){var r=this._options[e],n=i.arg2arr(arguments,2);if(!i.isFunction(r))return!!r;try{return r.apply(t,n)}catch(e){v.call(t,e)}}function l(e,t){var r=e._options.queueEventTrigger,n=i.arg2arr(arguments,2);a.apply(e,n)!==!1&&r&&a.apply(t,n)}function p(e){a.call(this,"workAdd",this,e,this)}function h(e,t,r){return new c(e,t,r)}function v(e){i.isFunction(this.onError)&&this.onError.call(this,e)}function d(e,t,r,n){function o(o){var i=n?[].concat([e[o]],[o],[e]):[].concat(e[o]);s[o]=[t,i,r]}var u=i.isArray(e),s=u?[]:{};if(u)for(var c=0;c<e.length;c++)o(c);else for(var f in e)o(f);return s}function y(e,t,r){var n,o=2;return i.isObject(r)&&(n=r,o++),{con:n,start:arguments[o],jump:arguments[++o]}}function m(e,t){var r,n=this.queue,o=this.map,i=this.each,u=y.apply(null,arguments);return r=d(e,t,u.con,i),o?n.addProps(r,u.start,u.jump):n.addArray(r,u.start,u.jump)}var j;t(e);var w=function(e){try{console.error(e)}catch(e){}};return s.prototype={option:function(e){return 1==arguments.length?this._options[e]:void(arguments.length>1&&(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<e.length;i++)+function(){var t=i,u=e[t],s=r?o.unshift.apply(o,u):o.push.apply(o,u);n.push(s)}();var u=j.defer();return j.all(n).then(function(e){u.resolve(e)},function(e){u.reject(e)}),t&&o.start(),u.promise},addProps:function(e,t,r){var n={},o=this;for(var i in e)+function(){var t=i,u=e[t],s=r?o.unshift.apply(o,u):o.push.apply(o,u);n[t]=s}();var u=j.defer();return j.allMap(n).then(function(e){u.resolve(e)},function(e){u.reject(e)}),t&&o.start(),u.promise},addLikeArray:function(e,t,r){return m.apply({queue:this},arguments)},addLikeProps:function(e,t,r){return m.apply({queue:this,map:!0},arguments)},addLikeArrayEach:function(e,t,r){return m.apply({queue:this,each:!0},arguments)},addLikePropsEach:function(e,t,r){return m.apply({queue:this,each:!0,map:!0},arguments)}},s.use=t,s.createUse=n,s}var o=e("extend-promise/src/extendClass"),i=e("./utils");t.exports=n},{"./utils":7,"extend-promise/src/extendClass":4}],7:[function(e,t,r){"use strict";r.isArray=function(e){return"[object Array]"==Object.prototype.toString.call(e)},r.isFunction=function(e){return"function"==typeof e},r.isObject=function(e){return"object"==typeof e&&null!==e},r.arg2arr=function(e,t,r){return Array.prototype.slice.call(e,t,r)}},{}]},{},[1]);