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)

49 lines (48 loc) 1.66 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); exports.Awaiter = void 0; const constants_1 = require('../constants'); const utils_1 = require('../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 === constants_1.REJECTED_STATUS); rejected && boundaryHook.owner.setError((0, utils_1.createError)(rejected.reason)); } if (suspenseHook && pendings === suspenseHook.getPendings()) { suspenseHook.setIsPeinding(false); } hook.update(); }); } } } exports.Awaiter = Awaiter; //# sourceMappingURL=awaiter.js.map