import * as plugins from './lik.plugins.js' /** * allows for avoiding race condition */ export class AsyncExecutionStack { public currentExecutions: Promise[] = []; public async getExclusiveExecutionSlot(funcArg: () => Promise, timeoutArg: number) { const executionDeferred = plugins.smartpromise.defer(); this.currentExecutions.push(executionDeferred.promise); for (const promiseArg of this.currentExecutions) { if (promiseArg !== executionDeferred.promise) { await promiseArg; } else { if (timeoutArg) { await Promise.race([funcArg(),plugins.smartdelay.delayFor(timeoutArg)]) } else { await funcArg(); } executionDeferred.resolve(); this.currentExecutions.shift(); } } }; }