best-queue
Version:
Queue in runtime base promise
2 lines (1 loc) • 11.4 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).BQueue={})}(this,(function(t){"use strict";function e(t,e,r,n,i,o,s){try{var a=t[o](s),u=a.value}catch(t){return void r(t)}a.done?e(u):Promise.resolve(u).then(n,i)}var r={exports:{}};!function(t){var e=function(t){var e,r=Object.prototype,n=r.hasOwnProperty,i="function"==typeof Symbol?Symbol:{},o=i.iterator||"@@iterator",s=i.asyncIterator||"@@asyncIterator",a=i.toStringTag||"@@toStringTag";function u(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{u({},"")}catch(t){u=function(t,e,r){return t[e]=r}}function c(t,e,r,n){var i=Object.create((e&&e.prototype instanceof y?e:y).prototype),o=new F(n||[]);return i._invoke=function(t,e,r){var n=f;return function(i,o){if(n===p)throw Error("Generator is already running");if(n===d){if("throw"===i)throw o;return j()}for(r.method=i,r.arg=o;;){var s=r.delegate;if(s){var a=S(s,r);if(a){if(a===v)continue;return a}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if(n===f)throw n=d,r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n=p;var u=h(t,e,r);if("normal"===u.type){if(n=r.done?d:l,u.arg===v)continue;return{value:u.arg,done:r.done}}"throw"===u.type&&(n=d,r.method="throw",r.arg=u.arg)}}}(t,r,o),i}function h(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var f="suspendedStart",l="suspendedYield",p="executing",d="completed",v={};function y(){}function g(){}function m(){}var x={};u(x,o,(function(){return this}));var k=Object.getPrototypeOf,w=k&&k(k(P([])));w&&w!==r&&n.call(w,o)&&(x=w);var b=m.prototype=y.prototype=Object.create(x);function L(t){["next","throw","return"].forEach((function(e){u(t,e,(function(t){return this._invoke(e,t)}))}))}function E(t,e){function r(i,o,s,a){var u=h(t[i],t,o);if("throw"!==u.type){var c=u.arg,f=c.value;return f&&"object"==typeof f&&n.call(f,"__await")?e.resolve(f.__await).then((function(t){r("next",t,s,a)}),(function(t){r("throw",t,s,a)})):e.resolve(f).then((function(t){c.value=t,s(c)}),(function(t){return r("throw",t,s,a)}))}a(u.arg)}var i;this._invoke=function(t,n){function o(){return new e((function(e,i){r(t,n,e,i)}))}return i=i?i.then(o,o):o()}}function S(t,r){var n=t.iterator[r.method];if(n===e){if(r.delegate=null,"throw"===r.method){if(t.iterator.return&&(r.method="return",r.arg=e,S(t,r),"throw"===r.method))return v;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return v}var i=h(n,t.iterator,r.arg);if("throw"===i.type)return r.method="throw",r.arg=i.arg,r.delegate=null,v;var o=i.arg;return o?o.done?(r[t.resultName]=o.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=e),r.delegate=null,v):o:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,v)}function T(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function Q(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function F(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(T,this),this.reset(!0)}function P(t){if(t){var r=t[o];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var i=-1,s=function r(){for(;++i<t.length;)if(n.call(t,i))return r.value=t[i],r.done=!1,r;return r.value=e,r.done=!0,r};return s.next=s}}return{next:j}}function j(){return{value:e,done:!0}}return g.prototype=m,u(b,"constructor",m),u(m,"constructor",g),g.displayName=u(m,a,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===g||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,m):(t.__proto__=m,u(t,a,"GeneratorFunction")),t.prototype=Object.create(b),t},t.awrap=function(t){return{__await:t}},L(E.prototype),u(E.prototype,s,(function(){return this})),t.AsyncIterator=E,t.async=function(e,r,n,i,o){void 0===o&&(o=Promise);var s=new E(c(e,r,n,i),o);return t.isGeneratorFunction(r)?s:s.next().then((function(t){return t.done?t.value:s.next()}))},L(b),u(b,a,"Generator"),u(b,o,(function(){return this})),u(b,"toString",(function(){return"[object Generator]"})),t.keys=function(t){var e=[];for(var r in t)e.push(r);return e.reverse(),function r(){for(;e.length;){var n=e.pop();if(n in t)return r.value=n,r.done=!1,r}return r.done=!0,r}},t.values=P,F.prototype={constructor:F,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=e,this.done=!1,this.delegate=null,this.method="next",this.arg=e,this.tryEntries.forEach(Q),!t)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=e)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var r=this;function i(n,i){return a.type="throw",a.arg=t,r.next=n,i&&(r.method="next",r.arg=e),!!i}for(var o=this.tryEntries.length-1;o>=0;--o){var s=this.tryEntries[o],a=s.completion;if("root"===s.tryLoc)return i("end");if(this.prev>=s.tryLoc){var u=n.call(s,"catchLoc"),c=n.call(s,"finallyLoc");if(u&&c){if(s.catchLoc>this.prev)return i(s.catchLoc,!0);if(s.finallyLoc>this.prev)return i(s.finallyLoc)}else if(u){if(s.catchLoc>this.prev)return i(s.catchLoc,!0)}else{if(!c)throw Error("try statement without catch or finally");if(s.finallyLoc>this.prev)return i(s.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var i=this.tryEntries[r];if(this.prev>=i.tryLoc&&n.call(i,"finallyLoc")&&i.finallyLoc>this.prev){var o=i;break}}!o||"break"!==t&&"continue"!==t||o.tryLoc>e||e>o.finallyLoc||(o=null);var s=o?o.completion:{};return s.type=t,s.arg=e,o?(this.method="next",this.next=o.finallyLoc,v):this.complete(s)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),v},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),Q(r),v}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var i=n.arg;Q(r)}return i}}throw Error("illegal catch attempt")},delegateYield:function(t,r,n){return this.delegate={iterator:P(t),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=e),v}},t}(t.exports);try{regeneratorRuntime=e}catch(t){"object"==typeof globalThis?globalThis.regeneratorRuntime=e:Function("r","regeneratorRuntime = r")(e)}}(r);var n,i=r.exports;function o(t){return new Promise((function(e){setTimeout((function(){e(null)}),t)}))}!function(t){t[t.Init=0]="Init",t[t.Running=1]="Running",t[t.Pause=2]="Pause",t[t.Finish=3]="Finish",t[t.Error=4]="Error"}(n||(n={}));var s=Symbol(),a=function(){function t(t,e,r){this.hasFinishedCount=0,this.currentTaskIndex=0,this.resolveFn=function(){},this.rejectFn=function(){},this.finished=[],this.errData=s,this.taskQueue=t,this.subscriber=r,this.options=e}var r,a,u=t.prototype;return u.resume=function(){if(this.taskQueue.getState()===n.Pause){if(this.currentTaskIndex===this.taskQueue.count-1)return void(this.errData!==s?(this.rejectFn(this.errData),this.taskQueue.setState(n.Error)):(this.taskQueue.setState(n.Finish),this.resolveFn(this.finished)));this.taskQueue.setState(n.Running),this.currentTaskIndex++,this.run()}},u.pause=function(){this.taskQueue.getState()===n.Running&&this.taskQueue.setState(n.Pause)},u.run=function(){var t=this.taskQueue.count-this.currentTaskIndex,e=t>this.options.max?this.options.max:t,r=this.currentTaskIndex;this.taskQueue.setState(n.Running);for(var i=0;e>i;i++)this.currentTaskIndex=r+i,this.handleTask(this.taskQueue.tasks[this.currentTaskIndex],this.currentTaskIndex)},u.next=(r=i.mark((function t(){var e,r=this;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(e=function(){return r.taskQueue.count-1>r.currentTaskIndex&&r.taskQueue.getState()===n.Running})()){t.next=5;break}return t.next=4,o(this.options.interval);case 4:e()&&this.handleTask(this.taskQueue.tasks[++this.currentTaskIndex],this.currentTaskIndex);case 5:case"end":return t.stop()}}),t,this)})),a=function(){var t=this,n=arguments;return new Promise((function(i,o){var s=r.apply(t,n);function a(t){e(s,i,o,a,u,"next",t)}function u(t){e(s,i,o,a,u,"throw",t)}a(void 0)}))},function(){return a.apply(this,arguments)}),u.handleSingleTaskResult=function(t,e){this.hasFinishedCount++,this.finished[e]=t;var r=this.hasFinishedCount===this.taskQueue.count;[n.Pause,n.Init].includes(this.taskQueue.getState())||(r?(this.taskQueue.setState(n.Finish),this.resolveFn(this.finished)):this.next())},u.onSingleTaskSuccess=function(t,e){this.handleSingleTaskResult(t,e)},u.onSingleTaskError=function(t,e){this.options.recordError?this.handleSingleTaskResult(t instanceof Error?t:Error(""+t),e):this.taskQueue.getState()===n.Pause?this.errData=t:(this.rejectFn(t),this.taskQueue.setState(n.Error))},u.promiseExecuter=function(t,e){this.resolveFn=t,this.rejectFn=e,this.run()},u.getProgress=function(){return this.hasFinishedCount/this.taskQueue.count},u.handleTask=function(t,e){var r=this;Promise.resolve("function"==typeof t?t():t).then((function(t){r.onSingleTaskSuccess(t,e),r.subscriber.dispatch({taskStatus:"success",data:t,taskIndex:e,progress:r.getProgress()})})).catch((function(t){r.onSingleTaskError(t,e),r.subscriber.dispatch({taskStatus:"error",data:t,taskIndex:e,progress:r.getProgress()})}))},t}(),u=function(){function t(){this.tasks={},this.count=0,this.state=n.Init}var e=t.prototype;return e.enqueue=function(t){this.tasks[this.count]=t,this.count++},e.getState=function(){return this.state},e.setState=function(t){this.state=t},e.isEmptyQueue=function(){return 0===this.count},t}(),c=function(){function t(){this.currentListeners=[],this.nextListeners=[]}var e=t.prototype;return e.dispatch=function(t){(this.currentListeners=this.nextListeners).forEach((function(e){e(t)}))},e.subscribe=function(t){if("function"!=typeof t)throw Error("listener must be a function");return this.ensureCanMutateNextListeners(),this.nextListeners.push(t),function(){this.ensureCanMutateNextListeners();var e=this.nextListeners.indexOf(t);this.nextListeners.splice(e,1),this.currentListeners=null}.bind(this)},e.ensureCanMutateNextListeners=function(){this.nextListeners===this.currentListeners&&(this.nextListeners=this.currentListeners.slice())},t}();t.createQueue=function(t,e){if(void 0===e&&(e={}),!Array.isArray(t))throw new TypeError("tasks must be a array");var r,n="object"==typeof(r=e)&&null!==r?e:{},i=n.max,o=void 0===i?1:i,s=n.interval,h=void 0===s?0:s,f=n.recordError,l=void 0!==f&&f,p=new c,d=new u;t.forEach((function(t){d.enqueue(t)}));var v=new a(d,{max:1>(o>>=0)?1:o,interval:0>(h>>=0)?0:h,recordError:l},p),y={pause:v.pause.bind(v),resume:v.resume.bind(v),subscribe:p.subscribe.bind(p)};return Object.assign(d.isEmptyQueue()?Promise.resolve([]):new Promise((function(t,e){return v.promiseExecuter(t,e)})),y)},Object.defineProperty(t,"__esModule",{value:!0})}));