From 13ef14208ef33bda2b91c3dd8a3e21ed19cc6740 Mon Sep 17 00:00:00 2001 From: konstantin-paulus Date: Wed, 8 Jul 2026 08:41:19 +0200 Subject: [PATCH] Refactor rendering functions to include dispose logic for cleanup in `handleMount` and `handleNodeInsert`. Update `renderProject` to return a dispose function, ensuring proper resource management during project rendering. Introduce `DocumentContext` for improved context handling in the Solid renderer. --- apps/web/src/context/electron/mount.ts | 10 ++++++-- packages/solid/src/renderer.ts | 33 ++++++++++++++++---------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/apps/web/src/context/electron/mount.ts b/apps/web/src/context/electron/mount.ts index 77a23e0..4c8a202 100644 --- a/apps/web/src/context/electron/mount.ts +++ b/apps/web/src/context/electron/mount.ts @@ -22,6 +22,7 @@ export function handleMount(engine: Accessor) { const e = engine(); const world = e.world; const c = world.components; + let dispose = () => {}; try { // Evaluate — top-level code (including top-level await) runs here. @@ -33,7 +34,7 @@ export function handleMount(engine: Accessor) { try { world.history.startTransaction("Render project"); - renderProject(project, document); + dispose = renderProject(project, document); world.history.commitTransaction(); } catch (error) { world.history.rollbackTransaction(); @@ -52,6 +53,8 @@ export function handleMount(engine: Accessor) { } } catch (error) { return { status: "rejected", error: error instanceof Error ? error.message : String(error) }; + } finally { + dispose?.(); } return { status: "fulfilled" }; @@ -69,6 +72,7 @@ export function handleNodeInsert(engine: Accessor) { return async ({ code, parentId, index }: NodeInsertRequest): Promise => { const e = engine(); const world = e.world; + let dispose = () => {}; try { const parentEid = resolveEntityEid(world, parentId); @@ -80,7 +84,7 @@ export function handleNodeInsert(engine: Accessor) { try { world.history.startTransaction("Insert nodes"); - renderProject(project, document); + dispose = renderProject(project, document); if (index !== undefined && document.rootEid !== null) { reorderEntity(world, document.rootEid, index); } @@ -98,6 +102,8 @@ export function handleNodeInsert(engine: Accessor) { } } catch (error) { return { status: "rejected", error: error instanceof Error ? error.message : String(error) }; + } finally { + dispose?.(); } return { status: "fulfilled" }; diff --git a/packages/solid/src/renderer.ts b/packages/solid/src/renderer.ts index c347973..7117005 100644 --- a/packages/solid/src/renderer.ts +++ b/packages/solid/src/renderer.ts @@ -2,21 +2,25 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +import { createContext, useContext } from "solid-js"; import { createRenderer } from "solid-js/universal"; +import type { JSX } from "solid-js"; import type { ProjectDocument } from "./document"; // Compiled project modules call the static runtime exports below // (babel-preset-solid in `universal` mode with a fixed moduleName), so the -// renderer must be a module-level singleton. Its host operations forward to -// the document of the active `renderProject` call. +// renderer must be a module-level singleton. +const DocumentContext = createContext(null); + let currentDocument: ProjectDocument | null = null; function doc(): ProjectDocument { - if (currentDocument === null) { + const document = useContext(DocumentContext) ?? currentDocument; + if (document === null) { throw new Error("No active mount — elements can only be created while a project renders"); } - return currentDocument; + return document; } const renderer = createRenderer({ @@ -69,19 +73,24 @@ export const { /** * Host entry point: renders a project component directly into `document`. - * Mounting is synchronous — the document is fully written when this returns — - * and the returned dispose function ends the reactive root. + * Mounting is synchronous — the document is fully written when this returns. */ -export function renderProject(project: () => unknown, document: ProjectDocument) { +export function renderProject(project: () => unknown, document: ProjectDocument): () => void { const previous = currentDocument; currentDocument = document as ProjectDocument; - let dispose: () => void = () => { }; try { - dispose = render(() => createComponent(project, {}), document.stage); - } catch (error) { - dispose?.(); + return render( + () => + createComponent(DocumentContext.Provider, { + value: document as ProjectDocument, + get children() { + return createComponent(project, {}) as JSX.Element; + }, + }), + document.stage, + ); + } finally { currentDocument = previous; - throw error; } }