Files
formbricks-formbricks/packages/js/src/methodQueue.ts
Anshuman Pandey 48859facf4 fix: formbricks init error (#2633)
Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
2024-05-22 12:07:17 +00:00

39 lines
740 B
TypeScript

// Simple queue for formbricks methods
export class MethodQueue {
private queue: (() => Promise<void>)[] = [];
private isExecuting = false;
add = (method: () => Promise<void>) => {
this.queue.push(method);
this.run();
};
private runNext = async () => {
if (this.isExecuting) return;
const method = this.queue.shift();
if (method) {
this.isExecuting = true;
try {
await method();
} finally {
this.isExecuting = false;
if (this.queue.length > 0) {
this.runNext();
}
}
}
};
run = async () => {
if (!this.isExecuting && this.queue.length > 0) {
await this.runNext();
}
};
clear = () => {
this.queue = [];
};
}