promise-queue-plus.es6.min.js 9.2 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 r(e,t,n){function o(u,s){if(!t[u]){if(!e[u]){var f="function"==typeof require&&require;if(!s&&f)return f(u,!0);if(i)return i(u,!0);throw new Error("Cannot find module '"+u+"'")}var c=t[u]={exports:{}};e[u][0].call(c.exports,function(r){var t=e[u][1][r];return o(t?t:r)},c,c.exports,r,e,t,n)}return t[u].exports}for(var i="function"==typeof require&&require,u=0;u<n.length;u++)o(n[u]);return o}({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!');var t="undefined"!=typeof window?window:self;t[r]=e()}}("PromiseQueuePlus",function(){return r("../src/queue")(Promise)})},{"../src/queue":4}],2:[function(r,e,t){"use strict";function n(r,e,t){function n(r){if(i(t)){for(var e=!1,n=0;n<t.length;n++)if(t[n]==r){e=!0;break}if(!e)return!1}return!d||!s(h[r])}function o(r,e){return u(e)||(e=+e>0?+e:0),function(t){var n,o=h.defer(),s=0,c=function(i){var f=t[i];h.resolve(f).then(function(t){"undefined"!=typeof e?n.push(t):n[i]=t,(0==--s||!r&&e&&n.length>=e)&&o.resolve(n)},function(r){u(e)?o.reject(r):0==--s&&o.resolve(n)}),s++};if(i(t)){n=[],0==t.length&&o.resolve(n);for(var a=0;a<t.length;a++)c(a)}else if(r&&f(t)){var l=0;n={};for(var a in t)c(a),l++;0==l&&o.resolve(n)}else o.reject(new TypeError("参数错误"));return o.promise}}function a(r){for(var e=h.defer(),t=0;t<r.length;t++)!function(){var n=t,o=r[n];h.resolve(o).then(function(r){e.resolve(r)},function(r){e.reject(r)})}();return e.promise}function l(r){return function(e,t){return e?r.reject(e):void r.resolve(t)}}function p(r){var e=this===h?null:this,t=h.defer(),n=c(arguments,1);return n.push(l(t)),r.apply(e,n),t.promise}var h,d;if(e?(d=!0,h=e):h=r,h.Promise||r==e||(h.Promise=r),s(r)&&s(r.prototype.then))h.defer=function(){var e,t,n=new r(function(r,n){e=r,t=n});return{promise:n,resolve:e,reject:t}};else if(s(r.defer))h.defer=function(){return r.defer()};else{if(!s(r.deferred))throw new TypeError("此类不支持扩展!");h.defer=function(){return r.deferred()}}return n("delay")&&(h.delay=function(r,e){var t=h.defer();return setTimeout(function(){t.resolve(e)},r),t.promise}),n("resolve")&&(h.resolve=function(r){var e=h.defer();return e.resolve(r),e.promise}),n("reject")&&(h.reject=function(r){var e=h.defer();return e.reject(r),e.promise}),n("all")&&(h.all=o()),n("allMap")&&(h.allMap=o(!0)),n("some")&&(h.some=function(r,e){return e=+e>=0?+e:0,o(!1,e)(r)}),n("map")&&(h.map=function(r,e,t){function n(){if(f<c.length){var t=c[f],s=h.resolve(e(r[t],t,r)).then(function(r){return n(),r},o.reject);i.push(s),u--,f++}else h.all(i).then(o.resolve,o.reject)}var o=h.defer(),i=[],u=t?+t.concurrency:0;if(0==u||u!=u){for(var s in r)i.push(e(r[s],s,r));return h.all(i).then(o.resolve,o.reject),o.promise}var f=0,c=function(){var e=[];for(var t in r)e.push(t);return e}();do n();while(u>0&&f<c.length);return o.promise}),n("race")&&(h.race=a),n("any")&&(h.any=a),n("nfcall")&&(h.nfcall=p),n("nfapply")&&(h.nfapply=function(r,e){var t=this===h?null:this,n=h.defer();if(!i(e))throw TypeError('"args" is not Array');return e.push(l(n)),r.apply(t,e),n.promise}),h.denodeify=function(r){var e=this===h?null:this;return function(){return p.apply(e,[].concat([r],c(arguments)))}},h}var o=r("./utils"),i=o.isArray,u=o.isEmpty,s=o.isFunction,f=o.isPlainObject,c=o.arg2arr;e.exports=n},{"./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"undefined"==typeof r||null===r},t.arg2arr=function(r,e,t){return Array.prototype.slice.call(r,e,t)}},{}],4:[function(r,e,t){"use strict";function n(r){function e(r){w=s.Q=s.Promise=o(r,{})}function t(r){var e=+r>>0;if(e>0)return e;throw new Error('The "max" value is invalid')}function u(r){try{return w.resolve(r())}catch(r){return w.reject(r)}}function s(r,e){function n(){if(m<y&&!j&&v.length>0){var r,e=v.shift(),t=0,n=+c("timeout",e,s),i=c("retry",e,s),f=c("retryIsJump",e,s),p=e._options.self,h=function(){return r&&clearTimeout(r),r=0,!!t++||void m--},d=function(){l(e,s,"workFinally",s,s,e)},g=function(r){h()||(e.defer.resolve(r),l(e,s,"workResolve",s,r,s,e),d())},E=function(r){h()||(i>e._errNo++?s._addItem(e,f,!0,!1):(e.defer.reject(r),l(e,s,"workReject",s,r,s,e)),d())};0!=m||w||(w=!0,a.call(s,"queueStart",s,s));u(function(){return e.fn.apply(p||null,e.regs)}).then(g,E).then(function(){v.length>0?o():0==m&&w&&(w=!1,a.call(s,"queueEnd",s,s))});return m+=1,void(n>0&&(r=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},v=[],y=t(r),m=0,w=!1,j=0;if(this._options=h,this.onError=g,i.isObject(e))for(var E in e)h.hasOwnProperty(E)&&(h[E]=e[E]);this.getMax=function(){return y},this.setMax=function(r){try{y=t(r),!j&&m&&s.start()}catch(r){d.call(s,r)}},this.getLength=function(){return v.length},this.getRunCount=function(){return m},this.isStart=function(){return!!w},this._addItem=function(r,e,t,n){if(!(r instanceof f))throw new TypeError('"unit" is not QueueUnit');e?v.unshift(r):v.push(r),n||p.call(s,r),t?s.start():w&&o()},this.start=function(){j=0,o()},this.stop=function(){j=1},this.clear=function(r){for(;v.length;){var e=v.shift();e.defer.reject(r)}}}function f(r,e,t){function n(r){for(var e=0;e<u.length;e++)if(r===u[e])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(r))throw new TypeError("Queues only support function, '"+r+"' is not function");this.fn=r,this._errNo=0,this.defer=w.defer(),i.isArray(e)&&(this.regs=e,s++),this._options=o;var f=arguments[s];if(i.isObject(f))for(var c in f)n(c)&&(o[c]=f[c])}function c(r,e,t){return r in e._options?e._options[r]:t._options[r]}function a(r,e){var t=this._options[r],n=i.arg2arr(arguments,2);if(!i.isFunction(t))return!!t;try{return t.apply(e,n)}catch(r){d.call(e,r)}}function l(r,e){var t=r._options.queueEventTrigger,n=i.arg2arr(arguments,2);a.apply(r,n)!==!1&&t&&a.apply(e,n)}function p(r){a.call(this,"workAdd",this,r,this)}function h(r,e,t){return new f(r,e,t)}function d(r){i.isFunction(this.onError)&&this.onError.call(this,r)}function v(r,e,t,n){function o(o){var i=n?[].concat([r[o]],[o],[r]):[].concat(r[o]);s[o]=[e,i,t]}var u=i.isArray(r),s=u?[]:{};if(u)for(var f=0;f<r.length;f++)o(f);else for(var c in r)o(c);return s}function y(r,e,t){var n,o=2;return i.isObject(t)&&(n=t,o++),{con:n,start:arguments[o],jump:arguments[++o]}}function m(r,e){var t,n=this.queue,o=this.map,i=this.each,u=y.apply(null,arguments);return t=v(r,e,u.con,i),o?n.addProps(t,u.start,u.jump):n.addArray(t,u.start,u.jump)}var w;e(r);var g=function(r){try{console.error(r)}catch(r){}};return s.prototype={option:function(r){return 1==arguments.length?this._options[r]:void(arguments.length>1&&(this._options[r]=arguments[1]))},push:function(){var r=this,e=h.apply(r,arguments);return r._addItem(e,!1),e.defer.promise},unshift:function(){var r=this,e=h.apply(r,arguments);return r._addItem(e,!0),e.defer.promise},go:function(){var r=this,e=h.apply(r,arguments);return r._addItem(e,!1,!0),e.defer.promise},jump:function(){var r=this,e=h.apply(r,arguments);return r._addItem(e,!0,!0),e.defer.promise},add:function(r,e){var t,n,o,u,s,f=this,c=1;if(!i.isFunction(r))throw new TypeError("Queues only support function, '"+r+"' is not function");return t=function(){var e=w.defer();return r(e.resolve,e.reject),e.promise},n=[t],i.isObject(e)&&(n.push(e),c++),o=!!arguments[c],u=!!arguments[c+1],s=u?f.unshift.apply(f,n):f.push.apply(f,n),o&&f.start(),s},addArray:function(r,e,t){for(var n=[],o=this,i=0;i<r.length;i++)+function(){var e=i,u=r[e],s=t?o.unshift.apply(o,u):o.push.apply(o,u);n.push(s)}();var u=w.defer();return w.all(n).then(function(r){u.resolve(r)},function(r){u.reject(r)}),e&&o.start(),u.promise},addProps:function(r,e,t){var n={},o=this;for(var i in r)+function(){var e=i,u=r[e],s=t?o.unshift.apply(o,u):o.push.apply(o,u);n[e]=s}();var u=w.defer();return w.allMap(n).then(function(r){u.resolve(r)},function(r){u.reject(r)}),e&&o.start(),u.promise},addLikeArray:function(r,e,t){return m.apply({queue:this},arguments)},addLikeProps:function(r,e,t){return m.apply({queue:this,map:!0},arguments)},addLikeArrayEach:function(r,e,t){return m.apply({queue:this,each:!0},arguments)},addLikePropsEach:function(r,e,t){return m.apply({queue:this,each:!0,map:!0},arguments)}},s.use=e,s.createUse=n,s}var o=r("extend-promise/src/extendClass"),i=r("./utils");e.exports=n},{"./utils":5,"extend-promise/src/extendClass":2}],5:[function(r,e,t){"use strict";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)}},{}]},{},[1]);