UNPKG

asyncc

Version:
2 lines (1 loc) 5.19 kB
const t="object"==typeof process&&"function"==typeof process.nextTick?process.nextTick:"function"==typeof setImmediate?setImmediate:function(t){setTimeout(t,0)};class e extends Error{constructor(t,e,n){super(t),Object.assign(this,{name:"AsynccError",message:t,errors:e,errpos:n,stack:this.stack||(new Error).stack})}}function n(...t){return 1===t.length&&Array.isArray(t[0])&&(t=t[0]),function(e,n){let i=0;!function e(s,r){const o=t[i++];s||!o?n&&n(s,r):o(r,e)}(null,e)}}function i(...t){return 1===t.length&&Array.isArray(t[0])&&(t=t[0]),function(e,n){let i=0;!function e(s,r){let o=t[i++];try{if(s){for(;o&&3!==o.length;)o=t[i++];o&&o(s,r,e)}else{for(;o&&o.length>2;)o=t[i++];o&&o(r,e)}}catch(t){e(t,r)}o||n&&n(s,r)}(null,e)}}function s(e,n,i){let s=0;function r(e,r){e||n(s)?i&&i(e,r):t((()=>{o()}))}function o(){e(r,s++)}o()}function r(t,e,n){s(t,(t=>!e(t)),n)}function o(t,n,i,s={},r){"function"==typeof s&&(r=s,s={}),t=Math.abs(t||n);const o=[],u=new Array(n).fill(),h=new Array(n).fill();let c=0,f=n,l=0;if(0!==f)for(s.timeout&&setTimeout((()=>{f&&a("err_timeout")}),s.timeout),t=t<n?t:n;c<t;)i(c++,_);else a();function a(t){if(l++)return;let n=null;(o.length||t)&&(n=new e(t||"err",u,o)),r&&r(n,h)}function _(t,e,r){h[t]=r,u[t]=e,e&&(o.push(t),s.bail)?a("err_bail"):(f--,c<n?i(c++,_):f||a())}}function u(t,e,n,i,s){o(t,e.length,(function(t,i){n(e[t],((e,n)=>{i(t,e,n)}),t)}),i,s)}function h(t,e,n,i){u(0,t,e,n,i)}function c(e,n,i){const s=e.length,r=[];let o=0;function u(e,n){r.push(n),e||s===o?i&&i(e,r):o<s&&t((()=>{h()}))}function h(){n(e[o],u,o++)}0!==s?h():i(null,[])}function f(t){this._tasks=[],this.result=t,this.error=void 0,this._lock=!1}function l(t){return new f(t)}function a(t,e,n,i){o(t,e.length,(function(t,n){e[t](((e,i)=>{n(t,e,i)}))}),n,i)}function _(t,e,n){a(0,t,e,n)}function p(){this.reset()}function m(t,e){this._task=t,this._concurrency=Math.abs(e||1),this._worker=0,this._paused=!1,this._items=new p}function y(t,e){return new m(t,e)}function g(e,n){n=n||{};let i=t,s=0,r=e;return"number"!=typeof e&&(r=e.times,s=e.lag||0),r=r||n.times||0,s&&(i=setTimeout),{times:r,lag:s,fn:i}}function k(t,e,n){let i=0;const{times:s,lag:r,fn:o}=g(t,{times:2});function u(t,e){!t||i>=s?n&&n(t,e):o((()=>{h()}),r)}function h(){e(u,i++)}h()}function d(e,n){const i=e.length,s=[];let r=0;function o(){e[r++](u)}function u(e,u){s.push(u),e||i===r?n&&n(e,s):r<i&&t((()=>{o()}))}0!==i?o():n(null,[])}function w(t,e,n){let i=0;const{times:s,lag:r,fn:o}=g(t);function u(t,e){t||s>0&&i>=s?n&&n(t,e):o((()=>{h()}),r)}function h(){e(u,i++)}s?h():n&&n()}function b(e,n,i){let s=0;function r(n,r){n||e(s)?i&&i(n,r):t((()=>{o()}))}function o(){n(r,s++)}e(s)?i&&i():o()}function A(t,e,n){b((e=>!t(e)),e,n)}f.prototype={_run:function(){if(this._lock)return;this._lock=!0;let t=this._tasks.shift();const e=this.error?["catch","end"]:["then","end"];for(;t&&!~e.indexOf(t.type);)t=this._tasks.shift();if(t){const e=(t,e)=>{this.error=t,this.result=e||this.result,this._lock=!1,this._run()},n=t.fn;if("end"===t.type)n(this.error,this.result);else try{"catch"===t.type?n(this.error,this.result,e):n(this.result,e)}catch(t){e(t)}}else this._lock=!1},then:function(t){return this._tasks.push({type:"then",fn:t}),this._run(),this},catch:function(t){return this._tasks.push({type:"catch",fn:t}),this._run(),this},end:function(t){this._tasks.push({type:"end",fn:t}),this._run()}},p.prototype={get length(){return this.items.length},shift(){return(this.items.shift()||{}).item},push(t,e){const n=this.items;if("number"!=typeof e)e=1/0,n.push({prio:e,item:t});else{let i;e=Math.abs(e);for(let s=0;s<n.length;s++)if(e<n[s].prio){n.splice(s,0,{prio:e,item:t}),i=!0;break}i||n.push({prio:e,item:t})}return this},unshift(t){return this.items.unshift({prio:0,item:t}),this},reset(){this.items=[]}},m.prototype={_run(){const{_items:e,_drain:n}=this;if(this._worker-=1,0===e.length)this._worker<=0&&(this._worker=0,n&&n());else{this._worker+=1;const[n,i]=e.shift();this._task(n,((e,n)=>{i&&i(e,n),t((()=>{this._run()}))}))}},_start(){for(;!this._paused&&this._worker<Math.min(this._concurrency,this._items.length);)this._worker+=1,this._run();return this},get paused(){return this._paused},get idle(){return!this.length&&0===this._worker},get length(){return this._items.length},pause(){return this._paused=!0,this},resume(){return this._paused=!1,this._start()},reset(){return this._items.reset(),this},running(){return this._worker},push(t,e,n){return this.concat([t],e,n)},concat(t,e,n){return"number"==typeof e&&(n=e,e=void 0),t.forEach((t=>{this._items.push([t,e],n)})),this._start()},unshift(t,e){return this._items.unshift([t,e]),this._start()},drain(t){return this._drain=t,this}};var x={_setImmediate:t,AsynccError:e,compose:n,connect:i,doUntil:s,doWhilst:r,each:h,eachLimit:u,eachSeries:c,noPromise:l,NoPromise:f,parallel:_,parallelLimit:a,queue:y,Queue:m,retry:k,series:d,times:w,until:b,whilst:A};export{e as AsynccError,f as NoPromise,m as Queue,t as _setImmediate,n as compose,i as connect,x as default,s as doUntil,r as doWhilst,h as each,u as eachLimit,c as eachSeries,l as noPromise,_ as parallel,a as parallelLimit,y as queue,k as retry,d as series,w as times,b as until,A as whilst};