Handle React cleanup
Some checks failed
Lint / lint (push) Failing after 10s

This commit is contained in:
Julien Valverdé
2026-01-02 00:43:28 +01:00
parent 3b2961e557
commit f245b61ab3
2 changed files with 21 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
/** biome-ignore-all lint/complexity/noBannedTypes: {} is the "empty props" type in React */
import type { Node, NodePathMap } from "godot"
import { Callable, type Node, type NodePathMap } from "godot"
import * as React from "react"
import * as Reconciler from "./Reconciler.js"
@@ -21,10 +21,11 @@ export const renderComponent: {
component: React.FC<{}>,
props?: Record<string, unknown>,
): void => {
const reconciler = Reconciler.make()
if (!(container as any)._reactReconciler)
(container as any)._reactReconciler = Reconciler.make()
if (!(container as any)._rootContainer)
(container as any)._rootContainer = (reconciler as any).createContainer(
if (!(container as any)._reactContainer) {
(container as any)._reactContainer = (container as any)._reactReconciler.createContainer(
container,
ConcurrentRoot,
null,
@@ -34,9 +35,23 @@ export const renderComponent: {
console.error,
)
reconciler.updateContainer(
const cleanup = Callable.create(container, function(this) {
(container as any)._reactReconciler.updateContainer(
null,
(container as any)._reactContainer,
null,
)
delete (container as any)._reactReconciler
delete (container as any)._reactContainer
container.tree_exiting.disconnect(cleanup)
})
container.tree_exiting.connect(cleanup)
}
(container as any)._reactReconciler.updateContainer(
React.createElement(component, props),
(container as any)._rootContainer,
(container as any)._reactContainer,
null,
)
}