gate-executor
Version:
A work queue that can be gated, stopping to wait for sub-queues to complete.
1 lines • 4.88 kB
JavaScript
!function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).GateExecutor=t()}}((function(){var t,e,n,r,i=(t=function(t,e){(function(t,n){(function(){var r=o.nextTick,i=(Function.prototype.apply,Array.prototype.slice),l={},u=0;function c(t,e){this._id=t,this._clearFn=e}c.prototype.unref=c.prototype.ref=function(){},c.prototype.close=function(){this._clearFn.call(window,this._id)},e.setImmediate="function"==typeof t?t:function(t){var n=u++,o=!(arguments.length<2)&&i.call(arguments,1);return l[n]=!0,r((function(){l[n]&&(o?t.apply(null,o):t.call(null),e.clearImmediate(n))})),n},e.clearImmediate="function"==typeof n?n:function(t){delete l[t]}}).call(this)}).call(this,i({}).setImmediate,i({}).clearImmediate)},function(n){return e||t(e={exports:{},parent:n},e.exports),e.exports}),o={},l=o={};function u(){throw new Error("setTimeout has not been defined")}function c(){throw new Error("clearTimeout has not been defined")}function a(t){if(n===setTimeout)return setTimeout(t,0);if((n===u||!n)&&setTimeout)return n=setTimeout,setTimeout(t,0);try{return n(t,0)}catch(e){try{return n.call(null,t,0)}catch(e){return n.call(this,t,0)}}}!function(){try{n="function"==typeof setTimeout?setTimeout:u}catch(e){n=u}try{r="function"==typeof clearTimeout?clearTimeout:c}catch(e){r=c}}();var s,f=[],h=!1,d=-1;function p(){h&&s&&(h=!1,s.length?f=s.concat(f):d=-1,f.length&&m())}function m(){if(!h){var t=a(p);h=!0;for(var n=f.length;n;){for(s=f,f=[];++d<n;)s&&s[d].run();d=-1,n=f.length}s=null,h=!1,function(t){if(r===clearTimeout)return clearTimeout(t);if((r===c||!r)&&clearTimeout)return r=clearTimeout,clearTimeout(t);try{r(t)}catch(e){try{return r.call(null,t)}catch(e){return r.call(this,t)}}}(t)}}function g(t,e){this.fun=t,this.array=e}function y(){}l.nextTick=function(t){var e=new Array(arguments.length-1);if(arguments.length>1)for(var n=1;n<arguments.length;n++)e[n-1]=arguments[n];f.push(new g(t,e)),1!==f.length||h||a(m)},g.prototype.run=function(){this.fun.apply(null,this.array)},l.title="browser",l.browser=!0,l.env={},l.argv=[],l.version="",l.versions={},l.on=y,l.addListener=y,l.once=y,l.off=y,l.removeListener=y,l.removeAllListeners=y,l.emit=y,l.prependListener=y,l.prependOnceListener=y,l.listeners=function(t){return[]},l.binding=function(t){throw new Error("process.binding is not supported")},l.cwd=function(){return"/"},l.chdir=function(t){throw new Error("process.chdir is not supported")},l.umask=function(){return 0};var w={};return function(t){(function(){"use strict";function e(e){return(e=e||{}).interval=null==e.interval?111:e.interval,e.timeout=null==e.timeout?2222:e.timeout,function e(n,r){let i={};i.id=++r,i.options=n;let o=[],l={lookup:{},history:[]},u=[],c={work_counter:0,gate:!1,running:!1,clear:null,firstclear:null,tm_in:null,hw_tmc:0,hw_hst:0};function a(){if(!c.running)return;i.isclear()||c.tm_in||(c.tm_in=setInterval(f,n.interval));let t=!1;do{t=!1;let e=null;c.gate||(e=o.shift()),e&&(l.lookup[e.id]=e,l.history.push(e),c.hw_hst=l.history.length>c.hw_hst?l.history.length:c.hw_hst,c.gate=e.gate,e.start=Date.now(),e.callback=s(e),u.push(e),c.hw_tmc=u.length>c.hw_tmc?u.length:c.hw_tmc,e.fn(e.callback),t=!0)}while(t)}function s(e){return function(){if(!e.done){for(e.end=Date.now(),e.done=!0,delete l.lookup[e.id];l.history[0]&&l.history[0].done;)l.history.shift();for(;u[0]&&u[0].done;)u.shift();if(e.gate&&(c.gate=!1),0===o.length&&0===l.history.length){if(clearInterval(c.tm_in),c.tm_in=null,c.firstclear){let t=c.firstclear;c.firstclear=null,t()}c.clear&&c.clear()}t(a)}}}function f(){let t=Date.now(),e=null;for(let n=0;n<u.length;++n)!(e=u[n]).gate&&!e.done&&e.tm<t-e.start&&(e.ontm&&e.ontm(e.tm,e.start,t),e.callback())}return i.start=function(e){return t((function(){c.running=!0,e&&(c.firstclear=e),a()})),i},i.pause=function(){c.running=!1},i.clear=function(t){return c.clear=t,i},i.isclear=function(){return 0===o.length&&0===l.history.length},i.add=function(e){return c.work_counter+=1,e.id=e.id||""+c.work_counter,e.ge=i.id,e.tm=null==e.tm?n.timeout:e.tm,e.dn=e.dn||e.fn.name||""+Date.now(),e.ctxt={},o.push(e),c.running&&t(a),i},i.gate=function(){let t=e(n,r);return i.add({gate:t,fn:function(e){t.start(e)}}),t},i.state=function(){let t=[];for(let e=0;e<l.history.length;++e){let n=l.history[e];n.done||t.push({s:"a",ge:n.ge,dn:n.dn,id:n.id})}for(let e=0;e<o.length;++e){let n=o[e];n.gate?t.push(n.gate.state()):t.push({s:"w",ge:n.ge,dn:n.dn,id:n.id})}return t.internal={qlen:o.length,hlen:l.history.length,klen:Object.keys(l.lookup).length,tlen:f.length,hw_hst:c.hw_hst,hw_tmc:c.hw_tmc},t},i}(e,0)}Object.defineProperty(w,"__esModule",{value:!0}),w.default=e,null!=typeof w&&(w=e)}).call(this)}.call(this,i({}).setImmediate),w}));