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)

157 lines (156 loc) 3.72 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); exports.getHook = exports.Hook = exports.Fiber = void 0; const constants_1 = require('../constants'); const view_1 = require('../view'); const use_effect_1 = require('../use-effect'); const utils_1 = require('../utils'); const component_1 = require('../component'); class Fiber { id = 0; cc = 0; cec = 0; idx = 0; eidx = 0; mask = 0; el = null; tag = null; parent = null; child = null; next = null; alt = null; inst = null; hook = null; constructor(idx = 0, hook = null) { this.id = Fiber.incrementId(); this.idx = idx; this.hook = hook; } mutate(fiber) { for (const key in fiber) { this[key] = fiber[key]; } return this; } markHost(mask) { this.mask |= mask; this.parent && !(this.parent.mask & mask) && this.parent.markHost(mask); } increment(count = 1) { if (!this.parent) return; this.parent.cec += count; if (!this.parent.el && !this.parent.hook?.getIsWip()) { this.parent.increment(count); } } setError(err) { if (this.hook?.hasCatch()) { this.hook.catch(err); (0, utils_1.logError)(err); } else if (this.parent) { this.parent.setError(err); } else { throw err; } } static incrementId() { return ++Fiber.nextId; } static setNextId(id) { Fiber.nextId = id; } static nextId = 0; } exports.Fiber = Fiber; class Hook { idx = 0; values = []; owner = null; mask = 0; providers = null; cleanups = null; catch = null; pendings = 0; update = null; __getMask(mask) { return Boolean(this.mask & mask); } __mark(mask, x) { x ? (this.mask |= mask) : (this.mask &= ~mask); } getIsWip() { return this.__getMask(constants_1.IS_WIP_HOOK_MASK); } setIsWip(x) { this.__mark(constants_1.IS_WIP_HOOK_MASK, x); } getIsPortal() { return this.__getMask(constants_1.IS_PORTAL_HOOK_MASK); } setIsPortal(x) { this.__mark(constants_1.IS_PORTAL_HOOK_MASK, x); } getIsSuspense() { return this.__getMask(constants_1.IS_SUSPENSE_HOOK_MASK); } setIsSuspense(x) { this.__mark(constants_1.IS_SUSPENSE_HOOK_MASK, x); } getIsBoundary() { return this.__getMask(constants_1.IS_BOUNDARY_HOOK_MASK); } setIsBoundary(x) { this.__mark(constants_1.IS_BOUNDARY_HOOK_MASK, x); } getIsPending() { return this.__getMask(constants_1.IS_PENDING_HOOK_MASK); } setIsPeinding(x) { this.__mark(constants_1.IS_PENDING_HOOK_MASK, x); } getProviders() { return this.providers; } setProviders(x) { this.providers = x; } hasCatch() { return (0, utils_1.detectIsFunction)(this.catch); } setCatch(x) { this.catch = x; } setUpdate(x) { this.update = x; } createCleanup(key, create) { this.owner.markHost(constants_1.CLEANUP_HOST_MASK); if (!this.cleanups) this.cleanups = new Map(); this.cleanups.get(key)?.(); this.cleanups.set(key, create()); } incrementPendings() { this.pendings++; } getPendings() { return this.pendings; } drop() { const { values, owner } = this; if (values.length > 0 && owner.mask & constants_1.EFFECT_HOST_MASK) { (0, use_effect_1.dropEffects)(this); } if (this.cleanups) { for (const [_, cleanup] of this.cleanups) cleanup(); this.cleanups.clear(); } } } exports.Hook = Hook; function getHook(alt, prevInst, nextInst) { if (alt && (0, view_1.detectAreSameComponentTypesWithSameKeys)(prevInst, nextInst)) return alt.hook; if ((0, component_1.detectIsComponent)(nextInst)) return new Hook(); return null; } exports.getHook = getHook; //# sourceMappingURL=fiber.js.map