export class UpdateQueue { private tail: Promise = Promise.resolve(); private pending = 0; enqueue(operation: () => Promise): Promise { this.pending++; const result = this.tail.then(operation, operation); this.tail = result.catch(() => {}).finally(() => { this.pending--; }); return result; } async drain(): Promise { await this.tail; } get size(): number { return this.pending; } }