@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 395 B
JavaScript
export class Mutex{#e=!1;#s=[];acquire(){const e={resolved:!1};return this.#e?new Promise((s=>{this.#s.push((()=>s(this.#r.bind(this,e))))})):(this.#e=!0,Promise.resolve(this.#r.bind(this,e)))}#r(e){if(e.resolved)throw new Error("Cannot release more than once.");e.resolved=!0;const s=this.#s.shift();s?s():this.#e=!1}async run(e){const s=await this.acquire();try{return await e()}finally{s()}}}