Skip to content

fix(deck.gl-geotiff): read MosaicLayer sources from layer state - #736

Merged
kylebarron merged 3 commits into
mainfrom
kyle/mosaic-sources-in-state
Oct 20, 2026
Merged

kylebarron merged 3 commits into
mainfrom
kyle/mosaic-sources-in-state

Conversation

@kylebarron

@kylebarron kylebarron commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

The problem was that sources was read statically. So when sources changed, the existing MosaicLayer instance would never see an updated sources.


Note

This PR was written by Claude (Claude Code), not by @kylebarron.

After its first render, MosaicLayer ignored changes to sources. Its tileset searched the latest spatial index but looked up the hits in the first render's sources array:

  • Added sources: they never loaded, and deck.gl reported update of TileLayer({id: 'mosaic-layer-<id>'}): Cannot read properties of undefined (reading 'id'). A layer that started with sources: [] never loaded the sources passed to it later.
  • Removed sources: they stayed selected. Going from [A, B, C] to [C] selected A.
  • Replaced sources: replacing every source kept the old ones. Going from [A, B] to [P, Q] kept A and B selected and never fetched P or Q.

usgs-topo passes a new sources array as the map moves. It showed missing maps after a pan, and drew nothing after a jump to another area.

Cause: TileLayer creates its tileset only once, so the getSources and getIndex closures that MosaicLayer hands it stay bound to the first MosaicLayer instance. Apps create a new layer instance on every render. deck.gl hands the new instance the old instance's state object, but each instance has its own props. So getIndex (this.state.index) returned the latest index, while getSources (this.props.sources) returned the first instance's array.

Fix: MosaicLayer now keeps sources in state next to the index built from them, and getSources reads this.state.sources. The comment above the closures used to say that deck.gl reuses the layer instance across prop updates. It now explains why both closures read this.state.

#673 included the same fix but was closed without merging. #688's description documented this bug under "Related bug, not fixed here".

Not covered

Sources without an id still go wrong when one is removed or replaced.

  • Their tile id defaults to their position in sources, and deck.gl's Tileset2D reuses the cached tile with that id. So a source that moves into a removed source's position shows the removed source's tile.
  • [A, B, C] to [C] still shows A, and [A, B] to [P, Q] keeps A and B and never fetches P or Q.
  • The sources docs say shifted sources re-fetch, but they don't.

Sources with an id aren't affected. This needs a separate fix.

Tests

The existing tests in mosaic-layer.test.ts call the lifecycle methods on one bare layer instance, so they couldn't catch this. The new MosaicLayer sources across layer instances block renders through deck.gl's LayerManager with a new MosaicLayer for each update.

  • It passes a stub device, because deck.gl ignores errors thrown in updateState when there's no device. renderSource returns null, so nothing draws.
  • Each test checks what onViewportLoad last reported and that onError wasn't called. The tests that add sources also check which ones getSource fetched.

The tests:

  • loads sources added after the first render: [A] to [A, B, C]
  • loads sources passed after an empty first render: [] to [A, B]
  • drops sources removed after the first render: [A, B, C] to [C]
  • loads the new sources when every source is replaced: [A, B] to [P, Q]

Without the change to mosaic-layer.ts, all four fail (Tests 4 failed | 7 passed (11)):

  • The first two get the reading 'id' error.
  • The third sees onViewportLoad report A instead of C.
  • The fourth never fetches P or Q.

With the change they pass, and so do pnpm build, pnpm -r --filter="!docs" typecheck, pnpm test, pnpm publint and biome ci ..

🤖 Written by Claude Code

kylebarron and others added 3 commits October 20, 2026 17:48
TileLayer creates its tileset once, so the `getSources` and `getIndex`
closures that MosaicLayer hands it stay bound to the first MosaicLayer
instance. deck.gl gives each new layer instance the same `state` object,
but each instance keeps its own `props`. So the tileset saw the latest
spatial index but the first render's `sources`, and looked index hits up
in that stale array.

After `sources` changed, added sources never loaded and deck.gl raised
"Cannot read properties of undefined (reading 'id')", removed sources
stayed selected, and replacement sources never loaded.

MosaicLayer now keeps `sources` in state next to the index built from
them, and the tileset reads them from there.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…y first render

Apps that load their sources asynchronously usually render MosaicLayer
with `sources: []` first. On main, the sources passed next never loaded,
and deck.gl raised "Cannot read properties of undefined (reading 'id')",
because the tileset still read the first, empty `sources` array.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The comment above the tileset closures sits in `renderTileLayer`, where
"Each render makes a new instance" reads as if `renderLayers` made the new
MosaicLayer. It makes a new TileLayer; the app makes the new MosaicLayer.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the fix label Oct 20, 2026

@kylebarron kylebarron left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

// The index can be null if sources are empty
index: Flatbush | null;
// The sources the index was built from
sources: MosaicT[];

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@kylebarron
kylebarron merged commit 550a969 into main Oct 20, 2026
4 checks passed
@kylebarron
kylebarron deleted the kyle/mosaic-sources-in-state branch October 20, 2026 22:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant