promise-queue-plus
Version:
Promise-based queue. Support timeout, retry and so on.
6 lines • 9.58 kB
JavaScript
/*!
* promise-queue-plus v1.2.2
* Homepage https://github.com/cnwhy/promise-queue-plus
* License BSD-2-Clause
*/
!function o(i,u,s){function f(t,r){if(!u[t]){if(!i[t]){var e="function"==typeof require&&require;if(!r&&e)return e(t,!0);if(a)return a(t,!0);throw new Error("Cannot find module '"+t+"'")}var n=u[t]={exports:{}};i[t][0].call(n.exports,function(r){var e=i[t][1][r];return f(e||r)},n,n.exports,o,i,u,s)}return u[t].exports}for(var a="function"==typeof require&&require,r=0;r<s.length;r++)f(s[r]);return f}({1:[function(r,e,t){!function(r,e){if("function"==typeof define&&(define.amd||define.cmd))define([],e);else{if("undefined"==typeof window&&"undefined"==typeof self)throw new Error('Loading the "'+r+'" module failed!');("undefined"!=typeof window?window:self)[r]=e()}}("PromiseQueuePlus",function(){return r("../src/queue")(Promise)})},{"../src/queue":4}],2:[function(r,e,t){"use strict";var n=r("./utils"),a=n.isArray,l=n.isEmpty,p=n.isFunction,h=n.isPlainObject,d=n.arg2arr;e.exports=function(r,e,n){var c,o;function t(r){if(a(n)){for(var e=!1,t=0;t<n.length;t++)if(n[t]==r){e=!0;break}if(!e)return!1}return!o||!p(c[r])}if((c=e?(o=!0,e):r).Promise||r==e||(c.Promise=r),p(r)&&p(r.prototype.then))c.defer=function(){var t,n;return{promise:new r(function(r,e){t=r,n=e}),resolve:t,reject:n}};else if(p(r.defer))c.defer=function(){return r.defer()};else{if(!p(r.deferred))throw new TypeError("此类不支持扩展!");c.defer=function(){return r.deferred()}}function i(s,f){return l(f)||(f=0<+f?+f:0),function(t){var n,o=c.defer(),i=0,r=function(e){var r=t[e];c.resolve(r).then(function(r){void 0!==f?n.push(r):n[e]=r,(0==--i||!s&&f&&n.length>=f)&&o.resolve(n)},function(r){l(f)?o.reject(r):0==--i&&o.resolve(n)}),i++};if(a(t)){n=[],0==t.length&&o.resolve(n);for(var e=0;e<t.length;e++)r(e)}else if(s&&h(t)){var u=0;for(var e in n={},t)r(e),u++;0==u&&o.resolve(n)}else o.reject(new TypeError("参数错误"));return o.promise}}function u(r){for(var e,t=c.defer(),n=0;n<r.length;n++)e=r[n],c.resolve(e).then(function(r){t.resolve(r)},function(r){t.reject(r)});return t.promise}function s(t){return function(r,e){if(r)return t.reject(r);t.resolve(e)}}function f(r){var e=this===c?null:this,t=c.defer(),n=d(arguments,1);return n.push(s(t)),r.apply(e,n),t.promise}return t("delay")&&(c.delay=function(r,e){var t=c.defer();return setTimeout(function(){t.resolve(e)},r),t.promise}),t("resolve")&&(c.resolve=function(r){var e=c.defer();return e.resolve(r),e.promise}),t("reject")&&(c.reject=function(r){var e=c.defer();return e.reject(r),e.promise}),t("all")&&(c.all=i()),t("allMap")&&(c.allMap=i(!0)),t("some")&&(c.some=function(r,e){return i(!1,e=0<=+e?+e:0)(r)}),t("map")&&(c.map=function(t,n,r){var o=c.defer(),i=[],u=r?+r.concurrency:0;if(0==u||u!=u){for(var e in t)i.push(n(t[e],e,t));return c.all(i).then(o.resolve,o.reject),o.promise}var s=0,f=function(){var r=[];for(var e in t)r.push(e);return r}();function a(){if(s<f.length){var r=f[s],e=c.resolve(n(t[r],r,t)).then(function(r){return a(),r},o.reject);i.push(e),u--,s++}else c.all(i).then(o.resolve,o.reject)}for(;a(),0<u&&s<f.length;);return o.promise}),t("race")&&(c.race=u),t("any")&&(c.any=u),t("nfcall")&&(c.nfcall=f),t("nfapply")&&(c.nfapply=function(r,e){var t=this===c?null:this,n=c.defer();if(!a(e))throw TypeError('"args" is not Array');return e.push(s(n)),r.apply(t,e),n.promise}),c.denodeify=function(r){var e=this===c?null:this;return function(){return f.apply(e,[].concat([r],d(arguments)))}},c}},{"./utils":3}],3:[function(r,e,t){"use strict";t.isPlainObject=function(r){return null!==r&&"object"==typeof r&&!r.nodeType&&r!==r.window&&!(r.constructor&&!Object.prototype.hasOwnProperty.call(r.constructor.prototype,"isPrototypeOf"))},t.isArray=function(r){return"[object Array]"==Object.prototype.toString.call(r)},t.isFunction=function(r){return"function"==typeof r},t.isEmpty=function(r){return null==r},t.arg2arr=function(r,e,t){return Array.prototype.slice.call(r,e,t)}},{}],4:[function(r,e,t){var A=r("./utils");e.exports=function r(e){var c;t(e);var i=function(r){console.error(r)},m=function(r){return A.runFn2Promise(c,r)};function t(r){c=u.Q=u.Promise=A.extendPromise(r)}function u(r,e){if(!(this instanceof u))return new u(r,e);var c=this,t={queueStart:null,queueEnd:null,workAdd:null,workResolve:null,workReject:null,workFinally:null,retry:0,retryIsJump:!1,timeout:0,autoRun:!1},l=[],p=A.getPositiveInt(r),h=0,d=!1,y=0;if(this._options=t,this.onError=i,A.isObject(e))for(var n in e)t.hasOwnProperty(n)&&(t[n]=e[n]);function o(){if(h<p&&!y&&0<l.length){var r,e=l.shift(),t=0,n=+g("timeout",e,c),o=g("retry",e,c),i=g("retryIsJump",e,c),u=e._options.self,s=function(){if(r&&clearTimeout(r),r=0,t++)return!0;h--},f=function(){j(e,c,"workFinally",c,c,e)},a=function(r){s()||(o>e._errNo++?c._addItem(e,i,!0,!1):(e.defer.reject(r),j(e,c,"workReject",c,r,c,e)),f())};return 0!=h||d||(d=!0,w.call(c,"queueStart",c,c)),m(function(){return e.fn.apply(u||null,e.regs)}).then(function(r){s()||(e.defer.resolve(r),j(e,c,"workResolve",c,r,c,e),f())},a).then(function(){0<l.length?v():0==h&&d&&(d=!1,w.call(c,"queueEnd",c,c))}),h+=1,void(0<n&&(r=setTimeout(function(){a("timeout")},n)))}return!0}function v(){for(;!o(););}this.getMax=function(){return p},this.setMax=function(r){p=A.getPositiveInt(r),!y&&h&&c.start()},this.getLength=function(){return l.length},this.getRunCount=function(){return h},this.isStart=function(){return!!d},this._addItem=function(r,e,t,n){if(!(r instanceof s))throw new TypeError('"unit" is not QueueUnit');e?l.unshift(r):l.push(r),n||function(r){w.call(this,"workAdd",this,r,this)}.call(c,r),t||c._options.autoRun?c.start():d&&v()},this.start=function(){y=0,v()},this.stop=function(){y=1},this.clear=function(r){for(;l.length;)l.shift().defer.reject(r)}}function s(r,e,t){var n={workResolve:!0,workReject:!0,workFinally:!0,queueEventTrigger:!0,regs:[],self:null},o=["workResolve","workReject","workFinally","queueEventTrigger","retry","retryIsJump","timeout","self"],i=1;if(!A.isFunction(r))throw new TypeError("Queues only support function, '"+r+"' is not function");function u(r){for(var e=0;e<o.length;e++)if(r===o[e])return!0;return!1}this.fn=r,this._errNo=0,this.defer=c.defer(),A.isArray(e)&&(this.regs=e,i++),this._options=n;var s=arguments[i];if(A.isObject(s))for(var f in s)u(f)&&(n[f]=s[f])}function g(r,e,t){return r in e._options?e._options[r]:t._options[r]}function w(r,e){var t=this._options[r],n=A.arg2arr(arguments,2);if(!A.isFunction(t))return!!t;try{return t.apply(e,n)}catch(r){(function(r){A.isFunction(this.onError)&&this.onError.call(this,r)}).call(e,r)}}function j(r,e){var t=r._options.queueEventTrigger,n=A.arg2arr(arguments,2);!1!==w.apply(r,n)&&t&&w.apply(e,n)}function n(r,e,t){return new s(r,e,t)}function o(r,e){var t,n=this.queue,o=this.map,i=this.each,u=function(r,e,t){var n,o=2;return A.isObject(t)&&(n=t,o++),{con:n,start:arguments[o],jump:arguments[++o]}}.apply(null,arguments);return t=function(t,n,o,i){var r=A.isArray(t),u=r?[]:{};function e(r){var e=i?A.toArray([t[r]],[r],[t]):A.toArray(t[r]);u[r]=[n,e,o]}if(r)for(var s=0;s<t.length;s++)e(s);else for(var f in t)e(f);return u}(r,e,u.con,i),o?n.addProps(t,u.start,u.jump):n.addArray(t,u.start,u.jump)}return u.prototype={option:function(r){if(1==arguments.length)return this._options[r];1<arguments.length&&(this._options[r]=arguments[1])},push:function(){var r=n.apply(this,arguments);return this._addItem(r,!1),r.defer.promise},unshift:function(){var r=n.apply(this,arguments);return this._addItem(r,!0),r.defer.promise},go:function(){var r=n.apply(this,arguments);return this._addItem(r,!1,!0),r.defer.promise},jump:function(){var r=n.apply(this,arguments);return this._addItem(r,!0,!0),r.defer.promise},add:function(e,r){var t,n,o,i=this,u=1;if(!A.isFunction(e))throw new TypeError("Queues only support function, '"+e+"' is not function");return t=[function(){var r=c.defer();return e(r.resolve,r.reject),r.promise}],A.isObject(r)&&(t.push(r),u++),n=!!arguments[u],o=arguments[u+1]?i.unshift.apply(i,t):i.push.apply(i,t),n&&i.start(),o},addArray:function(r,e,t){for(var n,o,i,u=[],s=this,f=0;f<r.length;f++)n=f,o=A.toArray(r[n]),i=t?s.unshift.apply(s,o):s.push.apply(s,o),u.push(i);var a=c.defer();return c.all(u).then(function(r){a.resolve(r)},function(r){a.reject(r)}),e&&s.start(),a.promise},addProps:function(r,e,t){var n,o,i,u={},s=this;for(var f in r)n=f,o=A.toArray(r[n]),i=t?s.unshift.apply(s,o):s.push.apply(s,o),u[n]=i;var a=c.defer();return c.allMap(u).then(function(r){a.resolve(r)},function(r){a.reject(r)}),e&&s.start(),a.promise},addLikeArray:function(r,e,t){return o.apply({queue:this},arguments)},addLikeProps:function(r,e,t){return o.apply({queue:this,map:!0},arguments)},addLikeArrayEach:function(r,e,t){return o.apply({queue:this,each:!0},arguments)},addLikePropsEach:function(r,e,t){return o.apply({queue:this,each:!0,map:!0},arguments)}},u.use=t,u.createUse=r,u}},{"./utils":5}],5:[function(r,e,t){var n=r("extend-promise/src/extendClass");t.isArray=function(r){return"[object Array]"==Object.prototype.toString.call(r)},t.isFunction=function(r){return"function"==typeof r},t.isObject=function(r){return"object"==typeof r&&null!==r},t.arg2arr=function(r,e,t){return Array.prototype.slice.call(r,e,t)},t.toArray=function(){return Array.prototype.concat.apply([],arguments)},t.getPositiveInt=function(r){var e=+r>>0;if(1<=e)return e;throw new Error('The "max" value is invalid')},t.extendPromise=function(r){return n(r,{})},t.runFn2Promise=function(e,r){try{return e.resolve(r())}catch(r){return e.reject(r)}}},{"extend-promise/src/extendClass":2}]},{},[1]);