Code-first video & image, rendered natively on WebGPU.
fix(compositions): dispatch layout nodes on `kind` so deep programs parse fast
okanasl0ffad4247224
diff --git a/packages/compositions/src/program/schema.test.ts b/packages/compositions/src/program/schema.test.tsindex a6348904..ca675645 100644--- a/packages/compositions/src/program/schema.test.ts+++ b/packages/compositions/src/program/schema.test.ts@@ -186,6 +186,54 @@ describe("CompositorProgramSchema (document v2)", () => {const parsedBad = CompositorProgramSchema.safeParse(badVolumeConfig);expect(parsedBad.success).toBe(false);});++ // A plain union over the node kinds re-parsed every subtree once per+ // member, so cost grew ~3x per nesting level; a depth-14 product demo+ // (~100 KB) never finished. Both outcomes must stay linear.+ describe("deep layout trees", () => {+ const deepTree = (depth: number, leaf: LayoutNode): LayoutNode =>+ depth === 0+ ? leaf+ : ({+ id: `box-${depth}`,+ kind: "box",+ children: [+ deepTree(depth - 1, leaf),+ { id: `label-${depth}`, kind: "text", text: "label" },+ { id: `mark-${depth}`, kind: "shape", shapeType: "rect" },+ ],+ } as LayoutNode);+ const program = (leaf: LayoutNode) => ({+ width: 1920,+ height: 1080,+ backgroundColor: "#000000",+ layout: [deepTree(16, leaf)],+ });+ const timed = (config: unknown) => {+ const start = performance.now();+ const result = CompositorProgramSchema.safeParse(config);+ return { result, ms: performance.now() - start };+ };++ it("parses a 16-deep tree quickly", () => {+ const { result, ms } = timed(+ program({ id: "leaf", kind: "text", text: "deep" } as LayoutNode),+ );+ expect(result.success).toBe(true);+ expect(ms).toBeLessThan(1000);+ });++ it("rejects a bad leaf in a 16-deep tree quickly, with its path", () => {+ const { result, ms } = timed(+ program({ id: "leaf", kind: "shape", fill: "#fff" } as LayoutNode),+ );+ expect(result.success).toBe(false);+ expect(ms).toBeLessThan(1000);+ const issue = result.error?.issues[0];+ expect(issue?.code).toBe("unrecognized_keys");+ expect(issue?.path.filter((k) => k === "children")).toHaveLength(16);+ });+ });});describe("validateLayoutProgram (E-code pre-parse scan)", () => {diff --git a/packages/compositions/src/program/schema.ts b/packages/compositions/src/program/schema.tsindex 498e744c..e61ae851 100644--- a/packages/compositions/src/program/schema.ts+++ b/packages/compositions/src/program/schema.ts@@ -1754,8 +1754,15 @@ export const Model3DNodeSchema: z.ZodType<Model3DNode> = z}).strict() as unknown as z.ZodType<Model3DNode>;+/**+ * Dispatch on `kind`, not a plain union: a plain union tries every member,+ * and each member parses `children` before failing, so parse time grows+ * ~3x per nesting level (a depth-14 tree took minutes). The node schemas+ * are typed `ZodType<…>` for their recursive types, but each is a strict+ * object with a `kind` literal, which is all `discriminatedUnion` reads.+ */export const LayoutNodeSchema = z.lazy(() =>- z.union([+ z.discriminatedUnion("kind", [FlexNodeSchema,BlockNodeSchema,BoxNodeSchema,@@ -1765,7 +1772,7 @@ export const LayoutNodeSchema = z.lazy(() =>CameraNodeSchema,LightNodeSchema,Model3DNodeSchema,- ]),+ ] as unknown as [z.ZodObject, ...z.ZodObject[]]),) as unknown as z.ZodType<LayoutNode>;// ── the program document ──────────────────────────────────────────────diff --git a/packages/framefields/src/builder-schema-conformance.test.ts b/packages/framefields/src/builder-schema-conformance.test.tsindex a1c0fca2..0142d361 100644--- a/packages/framefields/src/builder-schema-conformance.test.ts+++ b/packages/framefields/src/builder-schema-conformance.test.ts@@ -20,6 +20,12 @@ const builders = {text: () => Layer.text("Hello", { align: "start" }),richText: () => Layer.text(["Hello ", { text: "world", fontWeight: 700 }]),shape: () => Layer.shape("circle", { width: 100, height: 100 }),+ shapeFillAlias: () =>+ Layer.shape("rect", {+ width: 100,+ height: 100,+ fill: "linear-gradient(180deg, #3ecf8e55, #3ecf8e00)",+ }),flex: () => Layer.flex({ children: [Layer.text("a")] }),box: () => Layer.box({ width: 100, height: 100 }),camera: () => Layer.camera({ z: -800 }),diff --git a/packages/framefields/src/index.ts b/packages/framefields/src/index.tsindex 61c6be22..4f53d4c5 100644--- a/packages/framefields/src/index.ts+++ b/packages/framefields/src/index.ts@@ -2689,16 +2689,18 @@ export const Layer = {/** Alias of `fillColor`: a color or a CSS gradient string. */fill?: string;} = {},- ): AnimatableNode<ShapeNode> =>- withAnimation({- id: options.id ?? autoId("shape"),+ ): AnimatableNode<ShapeNode> => {+ // `fill` and `fillColor` are the same thing; either may hold a+ // color or a CSS gradient. Only `fillColor` reaches the node.+ const { fill, ...rest } = options;+ return withAnimation({+ id: rest.id ?? autoId("shape"),kind: "shape",- shapeType: options.shapeType ?? shapeType,- ...options,- // `fill` and `fillColor` are the same thing; either may hold a- // color or a CSS gradient.- fillColor: options.fillColor ?? options.fill ?? "#3b82f6",- }),+ shapeType: rest.shapeType ?? shapeType,+ ...rest,+ fillColor: rest.fillColor ?? fill ?? "#3b82f6",+ });+ },flex: (options: Partial<FlexNode> = {}): AnimatableNode<FlexNode> =>withAnimation({id: options.id ?? autoId("flex"),