export type Block = { count: number; unblock: () => void; getPromise: () => Promise; }; export class Blocker { #blocks: Map = new Map(); block(key: string): void { if (!this.#blocks.has(key)) { const promise = new Promise((resolve) => { this.#blocks.set(key, { count: 0, // Incremented to 1 further below unblock() { this.count--; if (this.count === 0) { resolve(); } }, getPromise: () => promise, // :d }); }); } this.#blocks.get(key)!.count++; } unblock(key: string): void { if (this.#blocks.has(key)) { this.#blocks.get(key)!.unblock(); } } async waitToBeUnblocked(key: string): Promise { if (!this.#blocks.has(key)) { return; } await this.#blocks.get(key)!.getPromise(); } }