@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 707 B
JavaScript
export class ResolverBase{#e=new Map;async waitFor(e){const s=this.getForId(e);return s||this.getOrCreatePromise(e)}tryGet(e,s){const t=this.getForId(e);if(!t){const t=()=>{};return this.getOrCreatePromise(e).catch(t).then((e=>{e&&s(e)})),null}return t}clear(){this.stopListening();for(const[e,{reject:s}]of this.#e.entries())s(new Error(`Object with ${e} never resolved.`));this.#e.clear()}getOrCreatePromise(e){const s=this.#e.get(e);if(s)return s.promise;let t=()=>{},r=()=>{};const o=new Promise(((e,s)=>{t=e,r=s}));return this.#e.set(e,{promise:o,resolve:t,reject:r}),this.startListening(),o}onResolve(e,s){const t=this.#e.get(e);this.#e.delete(e),0===this.#e.size&&this.stopListening(),t?.resolve(s)}}