Repository navigation
refactor: load layer metadata through one RasterTileLayer helper - #783
Conversation
RasterTileLayer subclasses load their metadata from updateState and copy the result into state. deck.gl hands that state to every newer instance of the layer, so a load started by an earlier render can land after a newer one. #770 fixed this for COGLayer and MultiCOGLayer by hand; ZarrLayer still has it. _loadMetadata does that bookkeeping once. Each load gets its own AbortController, and starting a load aborts the previous one. Only the latest load applies its result, raises its errors, and clears loadingMetadata. Removing the layer aborts the load in flight. The result is applied to getCurrentLayer(), the newest instance, so callbacks see the current props instead of those of the render that started the load. Co-Authored-By: Claude Opus 5.5 <[email protected]>
COGLayer's parse splits in two. _parseGeoTIFF(signal) opens the file, resolves its CRS and builds the tile grid. _applyGeoTIFF(parsed) calls onGeoTIFFLoad, infers the default render pipeline and stores the result. RasterTileLayer's _loadMetadata runs both, so COGLayer drops its own AbortController, finalizeState and loadingMetadata bookkeeping. onGeoTIFFLoad now comes from the newest render's props. Before, a render that changed only the callback while the file was opening still got the old callback. RasterTileLayer's state type gains an index signature, the shape of deck.gl's own SharedLayerState. Without it, a subclass whose state shares no field with the base's all-optional fields fails TypeScript's weak type check. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…etadata The same split as COGLayer. _parseAllSources(signal) opens the sources and builds the grouped tile grid, and _applyAllSources(parsed) stores them and calls onGeoTIFFLoad. MultiCOGLayer drops its own AbortController, finalizeState and loadingMetadata bookkeeping, and onGeoTIFFLoad now comes from the newest render's props. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…variable changes ZarrLayer had the race that #770 fixed in the COG layers. When `node` or `variable` changed while the previous store was still loading, the old load could finish last and overwrite the new store's state, or finish first and mark the layer loaded early. It now loads through RasterTileLayer's _loadMetadata, and _parseZarr returns what it parsed instead of calling setState. zarrita's `open` takes no AbortSignal, so a replaced load still runs to the end; only its result is dropped. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The design for moving dataset state (opened files, projection, tile grid) out of the layers into app-created sources, landing in five stages. This branch is stage 1. Co-Authored-By: Claude Opus 5.5 <[email protected]>
zarrita's `open` takes an AbortSignal, so ZarrLayer now passes the load's signal to both of its `open` calls. A load replaced by a newer `node` or `variable`, or by removing the layer, stops reading instead of running to the end. The previous commit's comment said zarrita took no signal; it does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
| /** A GeoTIFF opened by {@link COGLayer._parseGeoTIFF}, ready to apply. */ | ||
| export type ParsedGeoTIFF = { | ||
| geotiff: GeoTIFF; | ||
| sourceProjection: ProjectionDefinition; | ||
| /** Converts between the GeoTIFF's CRS and WGS84. */ | ||
| converter4326: Converter; | ||
| tilesetDescriptor: RasterTilesetDescriptor; | ||
| }; |
There was a problem hiding this comment.
I'm more tempted by a Source class and subclass approach rather than just these type defs
There was a problem hiding this comment.
Note
This reply was written by Claude (Claude Code), not by @kylebarron.
The Parsed* types are gone as of 9ba5844. Each layer's loadMetadata now stores what it loads in state itself, so there's no intermediate object to type.
The Source class idea belongs to stage 2. The spec now lists a generic RasterSource base class, with a subclass per format (COGSource, ZarrSource), as open question 5.
🤖 Written by Claude Code
| /** Aborts the in-flight header read when the `geotiff` prop changes or the | ||
| * layer is removed | ||
| */ | ||
| abortController?: AbortController; | ||
| /** True while `_parseGeoTIFF` is in flight. */ | ||
| loadingMetadata?: boolean; |
There was a problem hiding this comment.
👍 it was weird for the layer to own its own abort controller
| } | ||
| }); | ||
| this._loadMetadata({ | ||
| load: (signal) => this._parseGeoTIFF(signal), |
There was a problem hiding this comment.
Hmm, instead of passing this down as a callback, I feel like overriding _loadMetadata would be cleaner.
There was a problem hiding this comment.
Note
This reply was written by Claude (Claude Code), not by @kylebarron.
Done. Subclasses no longer pass callbacks. As of adbf2af they override two public methods that mirror deck.gl's lifecycle:
shouldLoadMetadata(params), likeshouldUpdateState;loadMetadata(signal), likeupdateState.
RasterTileLayer.updateState calls the protected _loadMetadata() bookkeeping, like deck.gl's _update(), when shouldLoadMetadata returns true.
🤖 Written by Claude Code
| async _parseGeoTIFF(signal: AbortSignal): Promise<ParsedGeoTIFF> { | ||
| const geotiff = await fetchGeoTIFF(this.props.geotiff, { | ||
| concurrencyLimiter: this.props.concurrencyLimiter, | ||
| signal, | ||
| }); | ||
| const crs = geotiff.crs; | ||
| const sourceProjection = | ||
| typeof crs === "number" | ||
| ? await this.props.epsgResolver!(crs) | ||
| : parseWkt(crs); |
| * Call `onGeoTIFFLoad`, infer the default render pipeline, and store the | ||
| * parsed GeoTIFF in state. | ||
| */ | ||
| _applyGeoTIFF({ |
There was a problem hiding this comment.
I'm confused about "apply". The name is confusing. We now split the act of loading the geotiff and setting it in the layer state?
There was a problem hiding this comment.
Note
This reply was written by Claude (Claude Code), not by @kylebarron.
That split is gone as of 9ba5844. It existed so that a synchronous apply step could run on the newest layer instance (getCurrentLayer()), which made callbacks such as onGeoTIFFLoad come from the newest render's props.
We decided that wasn't worth an extra concept. loadMetadata now loads and calls setState itself, and callbacks come from the props of the render that started the load, as before this PR.
🤖 Written by Claude Code
| this._loadMetadata({ | ||
| load: (signal) => this._parseZarr(signal), | ||
| apply: (layer, parsed) => layer.setState(parsed), | ||
| errorContext: "loading Zarr metadata", | ||
| }); |
There was a problem hiding this comment.
If we override _loadMetadata in this layer subclass, then the _loadMetadata can set this.state directly and that'll point to the this of ZarrLayer, right?
There was a problem hiding this comment.
Note
This reply was written by Claude (Claude Code), not by @kylebarron.
Yes. deck.gl's _transferState hands every new instance of a layer the same state object, and deliberately keeps the old instance's reference ("to support async actions"). So setState from the instance that started a load lands where the newest instance reads.
loadMetadata relies on this. The one rule is to call signal.throwIfAborted() after the last await, so a replaced load never writes.
🤖 Written by Claude Code
Per review, RasterTileLayer no longer takes `load` and `apply` callbacks. Subclasses override a protected async _loadMetadata(signal) that loads and stores their metadata, and start it from updateState with _reloadMetadata(errorContext). The base keeps the bookkeeping: aborting the previous load, owning loadingMetadata, raising only the latest load's errors, and aborting on finalize. deck.gl hands every instance the same state object, so setState from the instance that started a load lands where the newest instance reads. The one rule for overrides is to call signal.throwIfAborted() after the last await, before setState or callbacks. This drops the routing of onGeoTIFFLoad and onError to the newest render, the Parsed* types, and the getCurrentLayer() test stubs. openCogSources no longer catches aborts, since a replaced load's errors are ignored. ZarrLayer's race tests now load a real GeoZarr store. Co-Authored-By: Claude Opus 5.5 <[email protected]>
| this.setState({ loadingMetadata: false }); | ||
| } | ||
| }); | ||
| this._reloadMetadata("loading GeoTIFF"); |
There was a problem hiding this comment.
Instead of just passing a string, this should be like
{errorMsg: "loading GeoTIFF"}
There was a problem hiding this comment.
Why does this call this._reloadMetadata instead of this._loadMetadata? I don't see why it would be important to have two different methods for this.
There was a problem hiding this comment.
Note
This reply was written by Claude (Claude Code), not by @kylebarron.
Both points are resolved.
- Named option: it landed as
{ errorMsg }in 6d2f90a and was renamed to{ errorContext }in 7197704. Then it went away: the base now starts loads itself, so nothing passes it. Since 92f082d, the prefix comes from the layer's staticlayerName, as in "Metadata loading failed in COGLayer: …". - Two methods: the bookkeeping has to wrap the subclass's code, so it can't live in the method subclasses override. As of adbf2af this follows deck.gl's own split of
_update()aroundupdateState(). Subclasses override the publicshouldLoadMetadataandloadMetadata, and never call the protected_loadMetadata();RasterTileLayer.updateStatedoes.
🤖 Written by Claude Code
| signal?: AbortSignal; | ||
| }, | ||
| ): Promise<OpenedCogSource[] | null> { | ||
| ): Promise<OpenedCogSource[]> { |
| * `errorContext`, such as `"loading GeoTIFF"`. Removing the layer aborts the | ||
| * load in flight. | ||
| */ | ||
| protected _reloadMetadata(errorContext: string): void { |
There was a problem hiding this comment.
Yeah this should definitely be in an object as a named parameter
There was a problem hiding this comment.
Note
This reply was written by Claude (Claude Code), not by @kylebarron.
Superseded: _reloadMetadata no longer exists. The named option shipped in 6d2f90a, but adbf2af moved the load trigger into the base (shouldLoadMetadata), so no caller passes an error prefix anymore. Since 92f082d, the prefix comes from the layer's name.
🤖 Written by Claude Code
Per review, `_reloadMetadata` takes `{ errorMsg }` instead of a bare
string. Also adds a blank line before each override's abort check.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Per review. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… loads
Per review, subclasses no longer call a reload method. They override two
public methods, like deck.gl's shouldUpdateState and updateState:
shouldLoadMetadata(params) says when a prop the metadata depends on has
changed, and loadMetadata(signal) loads and stores it.
RasterTileLayer.updateState calls shouldLoadMetadata, and a protected
_loadMetadata(), like deck.gl's _update(), does the bookkeeping. Each
layer's error prefix is now a metadataErrorContext field.
The three layers drop their own updateState, which also gives
MultiCOGLayer the super.updateState call it skipped. Each layer resets
its stale state at the top of loadMetadata, which still runs
synchronously.
Subclass state is typed as RasterTileLayer<...>["state"] & {...}, as
deck.gl's MVTLayer does with TileLayer's. That replaces the index
signature on the base state type. Tests spy on the public loadMetadata
directly.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
| } as typeof RasterTileLayer.defaultProps; | ||
|
|
||
| declare state: { | ||
| declare state: RasterTileLayer<DataT, COGLayerProps<DataT>>["state"] & { |
| override shouldLoadMetadata({ | ||
| props, | ||
| oldProps, | ||
| changeFlags, | ||
| }: UpdateParameters<this>): boolean { | ||
| return ( | ||
| Boolean(changeFlags.dataChanged) || props.geotiff !== oldProps.geotiff | ||
| ); |
There was a problem hiding this comment.
👍 This is really clean.
We don't use the data prop, so do we even have to check dataChanged?
There was a problem hiding this comment.
Note
This reply was written by Claude (Claude Code), not by @kylebarron.
Right, it isn't needed. It was removed in 92f082d.
- First update: deck.gl sets
dataChanged: 'init', but it also diffs the props against empty old props (getOldProps()returns an empty object). Soprops.geotiff !== oldProps.geotiffis alreadytrue. - Later updates:
dataChangedonly reports changes to thedataprop (or adataComparator), which these layers don't use.
The test helpers used to run the first update with oldProps equal to props, which is why the check looked necessary. They now pass empty old props, as deck.gl does.
🤖 Written by Claude Code
| this.state.abortController?.abort(); | ||
| super.finalizeState(context); | ||
| } | ||
| protected override readonly metadataErrorContext = "loading GeoTIFF"; |
There was a problem hiding this comment.
Is there another way to do this than adding a new string static? Can't we access the name of the layer? And then say "Metadata loading failed in COGLayer" where we use the name of the layer?
There was a problem hiding this comment.
Note
This reply was written by Claude (Claude Code), not by @kylebarron.
Done in 92f082d; the field is gone. _loadMetadata() reads the class's static layerName, the way deck.gl's own Layer.toString() does, and raises "Metadata loading failed in COGLayer: …". MultiCOGLayer and ZarrLayer do the same.
🤖 Written by Claude Code
Per review: - Metadata load errors are prefixed with the layer's name, as in "Metadata loading failed in COGLayer: ...". The name comes from the class's static layerName, the way deck.gl's toString() reads it. That removes the metadataErrorContext fields. - shouldLoadMetadata no longer checks changeFlags.dataChanged. On a layer's first update, deck.gl diffs its props against empty old props, so `props.geotiff !== oldProps.geotiff` is already true. After that, dataChanged only reports the `data` prop, which these layers don't use. The test helpers now run the first update with empty old props, as deck.gl does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Note
This PR was written by Claude (Claude Code), not by @kylebarron.
Stage 1 of the Sources design, which this PR adds as
dev-docs/specs/2026-10-22-sources-design.md.COGLayer, MultiCOGLayer and ZarrLayer each load their metadata from
updateStateand store the result instate. deck.gl hands that state object to every newer instance of the layer, so a load started by an earlier render can land after a newer one. #770 fixed this for the two COG layers by hand; ZarrLayer still had it.Now
RasterTileLayerruns these loads, mirroring deck.gl's ownshouldUpdateState,updateStateand_update():shouldLoadMetadata(params): subclasses override it to returntruewhen a prop their metadata depends on has changed.loadMetadata(signal): subclasses override it to load the metadata and store it in state. After its lastawait, it callssignal.throwIfAborted()beforesetStateor callbacks._loadMetadata():RasterTileLayer.updateStatecalls this protected method whenshouldLoadMetadatareturnstrue. It:loadingMetadata;onError, prefixed with the layer's name, as in "Metadata loading failed in COGLayer: …";Because every instance shares one
stateobject,setStatefrom the instance that started a load lands where the newest instance reads. The three layers drop their ownupdateState, and type their state asRasterTileLayer<…>["state"] & {…}, as deck.gl's MVTLayer does with TileLayer's.Fixes:
nodeorvariablewhile the previous store is still loading no longer lets the old load overwrite the new one or mark the layer loaded early. ZarrLayer also passes the signal to zarrita'sopen, so a replaced load stops reading.super.updateState, which its ownupdateStateskipped.onError, even when they're thrown while building its tile grid.Metadata errors now name the layer: "loading GeoTIFF: …", "loading COG sources: …" and "loading Zarr metadata: …" become "Metadata loading failed in COGLayer: …", "… in MultiCOGLayer: …" and "… in ZarrLayer: …".
shouldLoadMetadataandloadMetadataare new public methods onRasterTileLayerand its subclasses; nothing new is exported. Notes for authors ofRasterTileLayersubclasses:updateStatemust callsuper.updateState, which starts the loads.finalizeStatemust callsuper.finalizeState, which aborts the load in flight.state.loadingMetadataandstate.metadataAbortControllerbelong to the base layer, and the default_isLoadingMetadata()readsstate.loadingMetadata.Tests:
loadMetadataskipsthrowIfAborted().opencall drops the signal.shouldLoadMetadatareturnsfalsestarts no load.🤖 Written by Claude Code