Draw the 3D world again after its tab is shown

- Make the block art, mesh workers and saved world atlas anew when a hidden tab comes back, instead of handing back disposed ones
This commit is contained in:
piexel committed 2026-11-25 23:00:05 +01:00
1 parent 2607682020
commit fb083a4e25
3 files changed
+38 -11

No files matched your search

@@ -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<SavedTerrainColoring>('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
@@ -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<MapTerrainColoring>('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<TerrainStyle<MapTile>>(
() => ({
surfaceOf: (tile, place) => new MapTerrain(tile, place, coloring, world.fluidHeight, art.atlas ?? undefined),
@@ -0,0 +1,24 @@
import { useEffect, useMemo, useReducer, type DependencyList } from 'react'
const DISPOSED = new WeakSet<object>()
/**
* 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<T extends object>(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
}