-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.min.js
1 lines (1 loc) · 5.38 KB
/
main.min.js
1
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.promiseButler=t():e.promiseButler=t()}(self,(()=>(()=>{"use strict";var e,t={d:(e,s)=>{for(var o in s)t.o(s,o)&&!t.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:s[o]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},s={};t.r(s),t.d(s,{getModeObject:()=>h}),function(e){e.SEQUENTIAL="SEQUENTIAL",e.BATCHED="BATCHED",e.PIPELINING="PIPELINING",e.PARALLEL="PARALLEL"}(e||(e={}));var o=function(e,t,s,o){return new(s||(s=Promise))((function(i,r){function n(e){try{h(o.next(e))}catch(e){r(e)}}function l(e){try{h(o.throw(e))}catch(e){r(e)}}function h(e){var t;e.done?i(e.value):(t=e.value,t instanceof s?t:new s((function(e){e(t)}))).then(n,l)}h((o=o.apply(e,t||[])).next())}))};class i{constructor({batchSize:e,debugMode:t,batchWiseCallback:s}){this.requestsArr=[],this.globalPromiseStore={promise:Promise.resolve(),pending:!1},this.BATCH_SIZE=6,this.batchWiseCallback=(...e)=>{},this.promiseResolvedStore={resolve:e=>{}},this.requestCounter=0,this.debugMode=!1,this.debugMode=null!=t?t:this.debugMode,this.BATCH_SIZE=null!=e?e:this.BATCH_SIZE,this.batchWiseCallback=null!=s?s:this.batchWiseCallback,this.log("Input params are: ",arguments)}log(...e){this.debugMode&&console.log(...e)}processPromise(e,t){return o(this,void 0,void 0,(function*(){if(this.globalPromiseStore.pending)return yield this.globalPromiseStore.promise,this.processPromise(e,t);this.log("Adding the promise in the array! ",t),this.requestsArr.push(e().then((e=>this.promiseResolvedStore[t]=e)).catch((e=>this.promiseResolvedStore[t]=e)).then((()=>{var e,s;return null===(s=null===(e=this.promiseResolvedStore)||void 0===e?void 0:e.resolve)||void 0===s?void 0:s.call(e,t)}))),this.requestsArr.length==this.BATCH_SIZE&&(this.globalPromiseStore={promise:Promise.all(this.requestsArr).catch((e=>{this.log("There was an error while processing a batch: ",e)})).then((()=>{this.log("A batch got completed!"),this.batchWiseCallback(this.requestsArr),this.requestsArr=[],this.globalPromiseStore.pending=!1})),pending:!0})}))}dispatch(e){return o(this,void 0,void 0,(function*(){return new Promise((t=>{this.promiseResolvedStore.resolve=s=>{this.log("The promise at index",s," is complete!"),++this.requestCounter==e.length&&(this.log("All of the promises are resolved!"),delete this.promiseResolvedStore.resolve,t(Object.values(this.promiseResolvedStore)))};for(let t=0;t<e.length;t++)this.processPromise(e[t],t)}))}))}}var r=function(e,t,s,o){return new(s||(s=Promise))((function(i,r){function n(e){try{h(o.next(e))}catch(e){r(e)}}function l(e){try{h(o.throw(e))}catch(e){r(e)}}function h(e){var t;e.done?i(e.value):(t=e.value,t instanceof s?t:new s((function(e){e(t)}))).then(n,l)}h((o=o.apply(e,t||[])).next())}))};class n{constructor({debugMode:e,slotSize:t}){this.requestSlots={},this.SLOT_SIZE=6,this.requestCounter=0,this.globalPromiseStore={resolve:e=>{},reject:()=>{},resolvedPromises:{}},this.promiseRequestStore={},this.debugMode=!1,this.debugMode=null!=e?e:this.debugMode,this.SLOT_SIZE=null!=t?t:this.SLOT_SIZE}log(...e){this.debugMode&&console.log(...e)}executePromise(e,t){return r(this,void 0,void 0,(function*(){const{value:s,done:o}=e.next();if(o||!s)return Promise.resolve();const{promiseCallback:i,index:n}=s,l=`${t}.${++this.requestCounter}`;return this.promiseRequestStore[l]=!1,this.log("Assigning the promise callback at index: ",n,", to slot: ",t),this.requestSlots[t].then((()=>{const e=i();return this.log("Outcome of the promise of index: ",n," ",e),e})).catch((e=>(this.log("promise failed in slot: ",t),e))).then((s=>r(this,void 0,void 0,(function*(){this.log("promise at index: ",n," is complete!"),delete this.promiseRequestStore[l],this.globalPromiseStore.resolvedPromises[n]=s,yield this.executePromise(e,t),0===Object.keys(this.promiseRequestStore).length&&(this.globalPromiseStore.resolve(Object.values(this.globalPromiseStore.resolvedPromises)),this.promiseRequestStore={done:!0})}))))}))}dispatch(e){return r(this,void 0,void 0,(function*(){return new Promise(((t,s)=>{const o=function*(){for(let t=0;t<e.length;t++)yield{promiseCallback:e[t],index:t}}();this.globalPromiseStore={resolve:t,reject:s,resolvedPromises:{}};for(let e=0;e<this.SLOT_SIZE;e++)this.requestSlots[e]=Promise.resolve(),this.executePromise(o,e)}))}))}}class l{constructor({debugMode:e}){var t;this.requestPromise=Promise.resolve(),this.debugMode=!1,this.debugMode=null!==(t=this.debugMode)&&void 0!==t?t:e}log(...e){this.debugMode&&console.log(...e)}dispatch(e){const t={},s=this;return new Promise((o=>{e.forEach(((e,o)=>{s.requestPromise=s.requestPromise.then((()=>e().then((e=>{s.log("The promise of index: ",o," is successfull!"),t[o]=e})).catch((e=>{s.log("The promise of index: ",o," is failed!"),t[o]=e}))))})),s.requestPromise=this.requestPromise.then((()=>{s.log("The promise is complete!"),o(Object.values(t))}))}))}}const h=(t={debugMode:!1})=>({[e.SEQUENTIAL]:()=>{const e=new l(t);return e.dispatch.bind(e)},[e.BATCHED]:(e=6,s=((...e)=>{}))=>{const o=new i(Object.assign(Object.assign({},t),{batchSize:e,batchWiseCallback:s}));return o.dispatch.bind(o)},[e.PIPELINING]:(e=6)=>{const s=new n(Object.assign(Object.assign({},t),{slotSize:e}));return s.dispatch.bind(s)}});return s})()));