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