export class ISchedule { // 进程锁 isLocked = false; private t: number | null = null; constructor(public idle: number, public start: () => Promise | void) { this.start = start; this.idle = idle; this.t = window.setInterval(() => { this._start(); }, this.idle); } private async _start() { if (this.isLocked) { return; } else { this.isLocked = true; try { // 失败不退出队列 await this.start(); } catch (e) { console.error(e); } finally { this.isLocked = false; } } } get isAvailable() { return this.t !== null; } stop() { if (this.t !== null) { window.clearInterval(this.t); this.t = null; } } } export class IQueue { data: T[] = []; lpush(...data: T[]) { this.data.unshift(...data); } rpop(n: number) { const result: T[] = []; for (let i = 0; i < n; i++) { const o = this.data.pop(); o !== undefined && result.push(o); } return result; } rpopAll() { const result: T[] = []; while (true) { const o = this.data.pop(); if (o !== undefined) { result.push(o); } else { break; } } return result; } }