Refactor mountNode to build fiber tree alongside host instances
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import type { UNode, UElement, URoot, ComponentFn, UComponent } from "../core/schema.js";
|
||||
import { isURoot, isUPrimitive } from "../core/schema.js";
|
||||
import { Context } from "../core/context.js";
|
||||
import type { Fiber } from "./fiber.js";
|
||||
|
||||
export interface HostConfig<TTag extends string, Instance, RootCtx> {
|
||||
name: string;
|
||||
@@ -34,6 +35,7 @@ export interface Root<TTag extends string, Instance, RootCtx> {
|
||||
ctx: RootCtx;
|
||||
container: unknown;
|
||||
context: Context;
|
||||
rootFiber: Fiber<Instance> | null;
|
||||
render(node: UNode): void;
|
||||
unmount(): void;
|
||||
}
|
||||
@@ -47,46 +49,70 @@ export function createRoot<TTag extends string, Instance, RootCtx>(
|
||||
const ctx = host.createRootContext(container, options, context);
|
||||
const rootContext = context ?? new Context();
|
||||
|
||||
function mountNode(node: UNode, parentInst?: Instance): Instance | undefined {
|
||||
function mountNode(node: UNode, parentFiber: Fiber<Instance> | null): Fiber<Instance> | undefined {
|
||||
if (node == null || node === false) return undefined;
|
||||
|
||||
if (isUPrimitive(node)) {
|
||||
const text = node === null ? "" : String(node);
|
||||
const parentInst = parentFiber?.instance;
|
||||
const t = host.createTextInstance(text, ctx, parentInst);
|
||||
if (parentInst) host.appendChild(parentInst, t, ctx);
|
||||
host.emit?.("instance.create", `text_${Date.now()}`, { kind: "text", value: text });
|
||||
return t;
|
||||
const fiber: Fiber<Instance> = {
|
||||
instance: t,
|
||||
tag: "#text",
|
||||
props: { text },
|
||||
key: undefined,
|
||||
children: [],
|
||||
parent: parentFiber,
|
||||
effect: null,
|
||||
signalDisposers: [],
|
||||
prevProps: null,
|
||||
};
|
||||
if (parentFiber) parentFiber.children.push(fiber);
|
||||
return fiber;
|
||||
}
|
||||
|
||||
if (isURoot(node)) {
|
||||
for (const child of node.children) {
|
||||
mountNode(child, parentInst);
|
||||
mountNode(child, parentFiber);
|
||||
}
|
||||
return parentInst;
|
||||
return parentFiber ?? undefined;
|
||||
}
|
||||
|
||||
// node must be a UElement here
|
||||
const el = node as UElement;
|
||||
|
||||
// Function component — type is a function (runtime-only, before resolution)
|
||||
if (typeof el.type === "function") {
|
||||
const component = el.type as ComponentFn;
|
||||
const out = component({ ...el.props, children: el.children });
|
||||
host.emit?.("component.invoke", `comp_${Date.now()}`, { type: (component as unknown as UComponent).displayName ?? "anonymous" });
|
||||
return mountNode(out, parentInst);
|
||||
return mountNode(out, parentFiber);
|
||||
}
|
||||
|
||||
// Intrinsic element
|
||||
const tag = el.type as TTag;
|
||||
const parentInst = parentFiber?.instance;
|
||||
const inst = host.createInstance(tag, el.props as Record<string, unknown>, ctx, parentInst);
|
||||
host.emit?.("instance.create", `${tag}_${Date.now()}`, { kind: "element", tag, props: el.props });
|
||||
|
||||
const fiber: Fiber<Instance> = {
|
||||
instance: inst,
|
||||
tag,
|
||||
props: el.props as Record<string, unknown>,
|
||||
key: el.key,
|
||||
children: [],
|
||||
parent: parentFiber,
|
||||
effect: null,
|
||||
signalDisposers: [],
|
||||
prevProps: null,
|
||||
};
|
||||
|
||||
for (const child of el.children) {
|
||||
mountNode(child, inst);
|
||||
mountNode(child, fiber);
|
||||
}
|
||||
|
||||
if (parentInst) host.appendChild(parentInst, inst, ctx);
|
||||
return inst;
|
||||
if (parentFiber) parentFiber.children.push(fiber);
|
||||
return fiber;
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -94,11 +120,24 @@ export function createRoot<TTag extends string, Instance, RootCtx>(
|
||||
ctx,
|
||||
container,
|
||||
context: rootContext,
|
||||
rootFiber: null,
|
||||
render(node: UNode) {
|
||||
const root: Fiber<Instance> = {
|
||||
instance: undefined as unknown as Instance,
|
||||
tag: "#root",
|
||||
props: {},
|
||||
key: undefined,
|
||||
children: [],
|
||||
parent: null,
|
||||
effect: null,
|
||||
signalDisposers: [],
|
||||
prevProps: null,
|
||||
};
|
||||
const payloadChildren = isURoot(node) ? (node as URoot).children : [node];
|
||||
for (const child of payloadChildren) {
|
||||
mountNode(child, undefined);
|
||||
mountNode(child, root);
|
||||
}
|
||||
this.rootFiber = root;
|
||||
host.finalizeRoot?.(ctx);
|
||||
host.emit?.("root.render", `root_${Date.now()}`, { childCount: payloadChildren.length });
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user