25 lines
796 B
TypeScript
25 lines
796 B
TypeScript
import * as plugins from './lik.plugins.js'
|
|
|
|
/**
|
|
* allows for avoiding race condition
|
|
*/
|
|
export class AsyncExecutionStack {
|
|
public currentExecutions: Promise<any>[] = [];
|
|
public async getExclusiveExecutionSlot(funcArg: () => Promise<any>, 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();
|
|
}
|
|
}
|
|
};
|
|
} |