UNPKG

asva-executors

Version:

Helper classes for your async flow control

2 lines (1 loc) 6.98 kB
import{__extends,__awaiter,__generator,__spreadArray}from"tslib";var Executor=function(){function e(t){this.runCount=0,this.wasLastRunFine=!1,this.wasRun=!1,this.wasRunFine=!1,this.wasRunBad=!1,this.command=t}return e.createAndRun=function(t){t=new e(t);return t.run(),t},Object.defineProperty(e.prototype,"isRunning",{get:function(){return!!this.runCount},enumerable:!1,configurable:!0}),e.prototype.run=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];this.beforeRun();var n=this.command.apply(this,t);return this.afterRun(n),n},e.prototype.beforeRun=function(){this.runCount++},e.prototype.afterRun=function(t){var e=this;t.then(function(){e.runCount--,e.setRunResultFlags(!0)}),t.catch(function(){e.runCount--,e.setRunResultFlags(!1)})},e.prototype.setRunResultFlags=function(t){this.wasRun=!0,(this.wasLastRunFine=t)&&(this.wasRunFine=!0),t||(this.wasRunBad=!0)},e}(),CacheExecutor=function(n){function t(){var t=null!==n&&n.apply(this,arguments)||this;return t.cachedPromise=null,t}return __extends(t,n),t.prototype.run=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return this.cachedPromise||(this.cachedPromise=n.prototype.run.apply(this,t)),this.cachedPromise},t.prototype.runFresh=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return this.cachedPromise=n.prototype.run.apply(this,t),this.cachedPromise},t.prototype.cleanCache=function(){this.cachedPromise=null},t}(Executor),LadderExecutor=function(r){function t(){var t=null!==r&&r.apply(this,arguments)||this;return t.trigger=null,t}return __extends(t,r),t.prototype.run=function(){for(var e=this,n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];if(this.isRunning)return new Promise(function(t){e.trigger=function(){return e.run.apply(e,n).then(t)}});var i=r.prototype.run.apply(this,n);return i.then(function(){e.trigger&&e.trigger(),e.trigger=null}),i},t}(Executor),DebounceLoader=function(){function t(t,e){void 0===e&&(e=3e3),this.identityCheck={},this.isWaiting=!1,this.executor=new Executor(t),this.timeout=e}return Object.defineProperty(t.prototype,"isRunning",{get:function(){return this.executor.isRunning},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"isActive",{get:function(){return this.isRunning||this.isWaiting},enumerable:!1,configurable:!0}),t.prototype.run=function(){for(var e=this,n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];this.isWaiting=!0;var i=this.registerIdentity();setTimeout(function(){var t;e.checkIdentity(i)&&(e.isWaiting=!1,(t=e.executor).run.apply(t,n))},this.timeout)},t.prototype.reset=function(){this.isWaiting=!1,this.identityCheck={}},t.prototype.registerIdentity=function(){return this.identityCheck={},this.identityCheck},t.prototype.checkIdentity=function(t){return this.identityCheck===t},t}(),RepeatLoader=function(){function t(t,e){this.time=100,this.id=null,this.command=t,this.time=e}return t.prototype.start=function(){this.id||(this.id=setInterval(this.command,this.time))},t.prototype.stop=function(){this.id&&clearInterval(this.id),this.id=null},t}(),InfiniteLoader=function(){function t(n,t){var i=this;void 0===t&&(t=20),this.items=[],this.isFinished=!1,this.isRefreshing=!1,this.perStep=20,this.pointer=0,this.isFresh=!0,this.executor=new LadderExecutor(function(e){return void 0===e&&(e=!1),__awaiter(i,void 0,void 0,function(){return __generator(this,function(t){switch(t.label){case 0:return[4,this.runRequest(n,e)];case 1:return[2,t.sent()]}})})}),this.perStep=t}return Object.defineProperty(t.prototype,"isRunning",{get:function(){return this.executor.isRunning},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"isEmpty",{get:function(){return!this.executor.isRunning&&(this.isFinished&&0===this.items.length)},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"isFull",{get:function(){return!this.isFresh&&(!this.executor.isRunning&&!!this.items.length)},enumerable:!1,configurable:!0}),t.prototype.next=function(){this.executor.run()},t.prototype.refresh=function(){this.executor.run(!0)},t.prototype.runRequest=function(n,i){return void 0===i&&(i=!1),__awaiter(this,void 0,Promise,function(){var e;return __generator(this,function(t){switch(t.label){case 0:(i=0===this.pointer?!0:i)&&(this.pointer=0,this.isFresh=!0,this.isRefreshing=!0,this.isFinished=!1),t.label=1;case 1:return t.trys.push([1,3,4,5]),[4,n(this.pointer,this.perStep)];case 2:return(e=t.sent(),Array.isArray(e))?(e.length<this.perStep&&(this.isFinished=!0),this.applyNew(e),this.pointer=this.pointer+this.perStep,this.isFresh=!1,[3,5]):(console.warn("InfiniteLoader function must return array"),[2]);case 3:throw e=t.sent(),this.isFinished=!0,e;case 4:return this.isRefreshing=!1,[7];case 5:return[2]}})})},t.prototype.applyNew=function(t){this.isFresh?this.items=t:this.items=__spreadArray(__spreadArray([],this.items),t)},t}(),AsyncHelpers=function(){function t(){}return t.sleep=function(e){return new Promise(function(t){return setTimeout(t,e)})},t}(),RetrierExecutor=function(){function r(t,e,n){void 0===e&&(e=100),void 0===n&&(n=1e3),this.iterationCount=0,this.command=t,this.timeout=e,this.maxTime=n}return r.waitUntil=function(e,n,i){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(t){switch(t.label){case 0:return[4,new r(e,n,i).run()];case 1:return t.sent(),[2]}})})},Object.defineProperty(r.prototype,"timeElapsed",{get:function(){return this.timeout*this.iterationCount},enumerable:!1,configurable:!0}),r.prototype.takesTooLong=function(){return this.timeElapsed>this.maxTime},r.prototype.run=function(){return __awaiter(this,void 0,Promise,function(){var e;return __generator(this,function(t){switch(t.label){case 0:this.iterationCount=0,e=this.command(),t.label=1;case 1:return e?[3,3]:[4,AsyncHelpers.sleep(this.timeout)];case 2:if(t.sent(),this.iterationCount++,this.takesTooLong())throw new Error("Max time ("+this.maxTime+") elapsed.");return e=this.command(),[3,1];case 3:return[2]}})})},r}();function range(n,t){return Array(t-n).fill(0).map(function(t,e){return n+e})}var PointerRequestFactory=function(){function t(){}return t.getPointerRequest=function(i){return void 0===i&&(i=100),function(e,n){return new Promise(function(t){setTimeout(function(){t(range(e,e+n))},i)})}},t}(),InfiniteLoaderFactory=function(){function t(){}return t.createNumberedList=function(t){t=PointerRequestFactory.getPointerRequest(t=void 0===t?300:t);return new InfiniteLoader(t,10)},t}(),StatefulExecutor=function(r){function n(t,e){void 0===e&&(e=null);t=r.call(this,t)||this;return t.state=e,t}return __extends(n,r),n.createAndRun=function(t,e){e=new n(t,e=void 0===e?null:e);return e.run(),e},n.prototype.run=function(){for(var e=this,t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];var i=r.prototype.run.apply(this,t);return i.then(function(t){e.state=t}),i},n}(Executor);export{CacheExecutor,DebounceLoader,Executor,InfiniteLoader,InfiniteLoaderFactory,LadderExecutor,RepeatLoader,RetrierExecutor,StatefulExecutor};