Files
NapCatQQ/src/common/utils/AsyncQueue.ts
2024-05-01 16:03:53 +08:00

34 lines
670 B
TypeScript

import { sleep } from '@/common/utils/helper';
type AsyncQueueTask = (() => void) | Promise<void> ;
export class AsyncQueue {
private tasks: (AsyncQueueTask)[] = [];
public addTask(task: AsyncQueueTask) {
this.tasks.push(task);
if (this.tasks.length === 1) {
this.runQueue().then().catch(()=>{});
}
}
private async runQueue() {
while (this.tasks.length > 0) {
const task = this.tasks[0];
try {
if (task instanceof Promise) {
await task;
}
else{
task();
}
} catch (e) {
console.error(e);
}
this.tasks.shift();
await sleep(100);
}
}
}