diff --git a/studio/src/renderer/src/studio/SavedWorldStudio.tsx b/studio/src/renderer/src/studio/SavedWorldStudio.tsx index 9c8ba28e1..649f64c00 100644 --- a/studio/src/renderer/src/studio/SavedWorldStudio.tsx +++ b/studio/src/renderer/src/studio/SavedWorldStudio.tsx @@ -19,6 +19,7 @@ import { SceneViewport } from './SceneViewport' import { Separator } from '@/components/ui/separator' import { HEADER_CHOICE, SidebarPanel, ViewIsland } from './Sidebar' import { useSettledEntry, useTab } from './tabContext' +import { useDisposable } from './useDisposable' import { useSpotActions } from './useSpotActions' import { useTerrainStream } from './useTerrainStream' @@ -47,13 +48,16 @@ export function SavedWorldStudio({ title, client, world, onSummary, onProgress, const [coloring, setColoring] = useState('biomes') const [readings, setReadings] = useState(0) const { start, looking } = useSavedStart(client, chosen?.id ?? null, readings, tell) - const { tileSource, atlas } = useMemo(() => { - const images = new BridgeAssets(client, () => {}) - const models = new BlockModels(images) - const pictures = new TerrainAtlas(images) - return { tileSource: savedTileSource(client, new BlockColors(models, images), new BlockLooks(models, pictures)), atlas: pictures } - }, [client]) - useEffect(() => () => atlas.dispose(), [atlas]) + const { tileSource, atlas } = useDisposable( + () => { + const images = new BridgeAssets(client, () => {}) + const models = new BlockModels(images) + const pictures = new TerrainAtlas(images) + return { tileSource: savedTileSource(client, new BlockColors(models, images), new BlockLooks(models, pictures)), atlas: pictures } + }, + (made) => made.atlas.dispose(), + [client] + ) const centerX = start?.centerX ?? 0 const centerZ = start?.centerZ ?? 0 const lowestGround = start?.lowestGround ?? 0 diff --git a/studio/src/renderer/src/studio/WorldStudio3D.tsx b/studio/src/renderer/src/studio/WorldStudio3D.tsx index 1240b7dc3..b75303826 100644 --- a/studio/src/renderer/src/studio/WorldStudio3D.tsx +++ b/studio/src/renderer/src/studio/WorldStudio3D.tsx @@ -16,6 +16,7 @@ import { Legend } from './Legend' import { PickInspector, hasPickSections } from './PickInspector' import { SceneViewport } from './SceneViewport' import { usePickInspection } from './usePickInspection' +import { useDisposable } from './useDisposable' import { useSpotActions } from './useSpotActions' import { DetailPanel, HEADER_CHOICE, SizeChoice, ViewIsland } from './Sidebar' import { useGroundInView, useLongPending, useTerrainStream } from './useTerrainStream' @@ -57,8 +58,7 @@ interface WorldStudio3DProps { export function WorldStudio3D({ title, client, world, mapArea, packRevision, onSummary, tell }: WorldStudio3DProps): React.JSX.Element { const [coloring, setColoring] = useState('biomes') const centre = useMemo(() => centreOf(mapArea), [mapArea]) - const art = useMemo(() => blockArt(client, true), [client]) - useEffect(() => () => art.dispose(), [art]) + const art = useDisposable(() => blockArt(client, true), (made) => made.dispose(), [client]) const source = useMemo(() => mapTileSource(client, TILE_COLUMNS, art, MOST_BLOCKS_PER_COLUMN), [client, art]) const focus = useMemo( () => focusAround(centre.x, centre.z, world.fluidHeight - BLOCKS_BELOW_FLUID_IN_FOCUS, world.fluidHeight + BLOCKS_ABOVE_FLUID_IN_FOCUS), @@ -69,8 +69,7 @@ export function WorldStudio3D({ title, client, world, mapArea, packRevision, onS () => ({ view: 'biomes', preview: null, heights: true, pickNoise: false, blocks: showsBlocks ? 'generated' : null, shape: showsBlocks, revision: packRevision }), [packRevision, showsBlocks] ) - const meshWorkers = useMemo(() => new TerrainMeshWorkers(), []) - useEffect(() => () => meshWorkers.dispose(), [meshWorkers]) + const meshWorkers = useDisposable(() => new TerrainMeshWorkers(), (workers) => workers.dispose(), []) const style = useMemo>( () => ({ surfaceOf: (tile, place) => new MapTerrain(tile, place, coloring, world.fluidHeight, art.atlas ?? undefined), diff --git a/studio/src/renderer/src/studio/useDisposable.ts b/studio/src/renderer/src/studio/useDisposable.ts new file mode 100644 index 000000000..b3b292175 --- /dev/null +++ b/studio/src/renderer/src/studio/useDisposable.ts @@ -0,0 +1,24 @@ +import { useEffect, useMemo, useReducer, type DependencyList } from 'react' + +const DISPOSED = new WeakSet() + +/** + * What {@code create} makes, given up with {@code dispose} when the component's effects end: when it unmounts, and while + * an Activity hides it, as it does a tab that is not shown. A memoised value outlives its effects, so once they start + * again a disposed one is made anew instead of being handed back. + */ +export function useDisposable(create: () => T, dispose: (value: T) => void, deps: DependencyList): T { + const [generation, makeAnew] = useReducer((count: number) => count + 1, 0) + const value = useMemo(create, [...deps, generation]) + useEffect(() => { + if (DISPOSED.has(value)) { + makeAnew() + return + } + return () => { + dispose(value) + DISPOSED.add(value) + } + }, [value]) + return value +}