Repository navigation
fix(deck.gl-geotiff): read MosaicLayer sources from layer state - #736
Merged
Merged
Conversation
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]>
kylebarron
commented
Oct 20, 2026
| // The index can be null if sources are empty | ||
| index: Flatbush | null; | ||
| // The sources the index was built from | ||
| sources: MosaicT[]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The problem was that
sourceswas read statically. So whensourceschanged, the existingMosaicLayerinstance would never see an updatedsources.Note
This PR was written by Claude (Claude Code), not by @kylebarron.
After its first render,
MosaicLayerignored changes tosources. Its tileset searched the latest spatial index but looked up the hits in the first render'ssourcesarray:update of TileLayer({id: 'mosaic-layer-<id>'}): Cannot read properties of undefined (reading 'id'). A layer that started withsources: []never loaded the sources passed to it later.[A, B, C]to[C]selected A.[A, B]to[P, Q]kept A and B selected and never fetched P or Q.usgs-topo passes a new
sourcesarray as the map moves. It showed missing maps after a pan, and drew nothing after a jump to another area.Cause:
TileLayercreates its tileset only once, so thegetSourcesandgetIndexclosures thatMosaicLayerhands it stay bound to the firstMosaicLayerinstance. Apps create a new layer instance on every render. deck.gl hands the new instance the old instance'sstateobject, but each instance has its ownprops. SogetIndex(this.state.index) returned the latest index, whilegetSources(this.props.sources) returned the first instance's array.Fix:
MosaicLayernow keepssourcesin state next to the index built from them, andgetSourcesreadsthis.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 readthis.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
idstill go wrong when one is removed or replaced.sources, and deck.gl'sTileset2Dreuses 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.sourcesdocs say shifted sources re-fetch, but they don't.Sources with an
idaren't affected. This needs a separate fix.Tests
The existing tests in
mosaic-layer.test.tscall the lifecycle methods on one bare layer instance, so they couldn't catch this. The newMosaicLayer sources across layer instancesblock renders through deck.gl'sLayerManagerwith a newMosaicLayerfor each update.updateStatewhen there's no device.renderSourcereturnsnull, so nothing draws.onViewportLoadlast reported and thatonErrorwasn't called. The tests that add sources also check which onesgetSourcefetched.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)):reading 'id'error.onViewportLoadreport A instead of C.With the change they pass, and so do
pnpm build,pnpm -r --filter="!docs" typecheck,pnpm test,pnpm publintandbiome ci ..🤖 Written by Claude Code