UNPKG

@dark-engine/core

Version:

The lightweight and powerful UI rendering engine without dependencies and written in TypeScript (Browser, Node.js, Android, iOS, Windows, Linux, macOS)

46 lines (45 loc) 1.53 kB
import { REJECTED_STATUS } from '../constants'; import { createError } from '../utils'; class Awaiter { store = new Map(); add(suspense, boundary, promise) { const key = suspense?.hook || boundary?.hook; !this.store.has(key) && this.store.set(key, [null, null, new Set()]); const data = this.store.get(key); data[0] = suspense?.hook || null; data[1] = boundary?.hook || null; data[2].add(promise); } resolve() { for (const [key, data] of this.store) { this.store.delete(key); const [suspenseHook, boundaryHook, promises] = data; let pendings = 0; if (promises.size === 0) continue; if (suspenseHook) { suspenseHook.setIsPeinding(true); suspenseHook.incrementPendings(); pendings = suspenseHook.getPendings(); suspenseHook.update(); } Promise.allSettled(promises).then(res => { const hook = boundaryHook && suspenseHook ? boundaryHook.owner.id < suspenseHook.owner.id ? boundaryHook : suspenseHook : boundaryHook || suspenseHook; if (boundaryHook) { const rejected = res.find(x => x.status === REJECTED_STATUS); rejected && boundaryHook.owner.setError(createError(rejected.reason)); } if (suspenseHook && pendings === suspenseHook.getPendings()) { suspenseHook.setIsPeinding(false); } hook.update(); }); } } } export { Awaiter }; //# sourceMappingURL=awaiter.js.map