fix(editor): release scene singletons on Editor unmount (#214)
The spatial-grid manager, scene-store subscriptions, and the viewer outliner's Object3D arrays are module-level singletons that previously lived for the whole page lifetime. When an app unmounted the Editor (e.g. navigating away from the 3D editor and back in an SPA host) they retained references to the disposed Three.js scene graph, which caused the app to freeze on re-entry as the stale data was replayed against a fresh scene. - initSpatialGridSync now returns the unsubscribe handle from its scene-store subscription so callers can detach it. - initializeEditorRuntime becomes reversible: it wires up the spatial grid, space detection and SFX bus on mount and returns a teardown closure that detaches both store subscriptions, clears the spatial grid manager, and truncates the viewer outliner's selected/hovered arrays in place. - The Editor component's runtime-init effect now returns that teardown, so the singletons are reset cleanly every time the Editor unmounts. - initSFXBus is now idempotent via an internal guard so repeated Editor mounts do not stack duplicate mitt listeners. Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -34,8 +34,10 @@ export function resolveLevelId(node: AnyNode, nodes: Record<string, AnyNode>): s
|
||||
return 'default' // fallback for orphaned items
|
||||
}
|
||||
|
||||
// Call this once at app initialization
|
||||
export function initSpatialGridSync() {
|
||||
// Call this once at app initialization. Returns an unsubscribe function that
|
||||
// detaches the scene-store listener (useful when the editor is unmounted so
|
||||
// the spatial grid singleton does not hold stale references to old scenes).
|
||||
export function initSpatialGridSync(): () => void {
|
||||
const store = useScene
|
||||
// 1. Initial sync - process all existing nodes
|
||||
const state = store.getState()
|
||||
@@ -48,7 +50,7 @@ export function initSpatialGridSync() {
|
||||
const markDirty = (id: AnyNodeId) => store.getState().markDirty(id)
|
||||
|
||||
// Subscribe to all changes
|
||||
store.subscribe((state, prevState) => {
|
||||
const unsubscribe = store.subscribe((state, prevState) => {
|
||||
// Detect added nodes
|
||||
for (const [id, node] of Object.entries(state.nodes)) {
|
||||
if (!prevState.nodes[id as AnyNode['id']]) {
|
||||
@@ -113,6 +115,8 @@ export function initSpatialGridSync() {
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return unsubscribe
|
||||
}
|
||||
|
||||
function arraysEqual(a: number[], b: number[]): boolean {
|
||||
|
||||
Reference in New Issue
Block a user