Skip to content

refactor: load layer metadata through one RasterTileLayer helper - #783

Merged
kylebarron merged 11 commits into
mainfrom
kyle/load-metadata
Oct 26, 2026
Merged

kylebarron merged 11 commits into
mainfrom
kyle/load-metadata

Conversation

@kylebarron

@kylebarron kylebarron commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

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 updateState and store the result in state. 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 RasterTileLayer runs these loads, mirroring deck.gl's own shouldUpdateState, updateState and _update():

  • shouldLoadMetadata(params): subclasses override it to return true when a prop their metadata depends on has changed.
  • loadMetadata(signal): subclasses override it to load the metadata and store it in state. After its last await, it calls signal.throwIfAborted() before setState or callbacks.
  • _loadMetadata(): RasterTileLayer.updateState calls this protected method when shouldLoadMetadata returns true. It:
    • aborts the previous load;
    • sets and clears loadingMetadata;
    • raises only the latest load's errors through onError, prefixed with the layer's name, as in "Metadata loading failed in COGLayer: …";
    • aborts the load in flight when the layer is removed.

Because every instance shares one state object, setState from the instance that started a load lands where the newest instance reads. The three layers drop their own updateState, and type their state as RasterTileLayer<…>["state"] & {…}, as deck.gl's MVTLayer does with TileLayer's.

Fixes:

  • ZarrLayer: changing node or variable while 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's open, so a replaced load stops reading.
  • MultiCOGLayer:
    • It now runs super.updateState, which its own updateState skipped.
    • A replaced load's errors no longer reach 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: …".

shouldLoadMetadata and loadMetadata are new public methods on RasterTileLayer and its subclasses; nothing new is exported. Notes for authors of RasterTileLayer subclasses:

  • An override of updateState must call super.updateState, which starts the loads.
  • An override of finalizeState must call super.finalizeState, which aborts the load in flight.
  • state.loadingMetadata and state.metadataAbortController belong to the base layer, and the default _isLoadingMetadata() reads state.loadingMetadata.

Tests:

  • The race tests use real fixtures: COG files for the two COG layers, and a GeoZarr store for ZarrLayer.
  • They fail if the base doesn't abort the previous load, or if any layer's loadMetadata skips throwIfAborted().
  • The Zarr tests also fail if either open call drops the signal.
  • The base tests also check that a re-render whose shouldLoadMetadata returns false starts no load.

🤖 Written by Claude Code

kylebarron and others added 6 commits October 23, 2026 15:24
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]>
@github-actions github-actions Bot added the fix label Oct 23, 2026
Comment on lines +151 to +158
/** 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;
};

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.

I'm more tempted by a Source class and subclass approach rather than just these type defs

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.

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

Comment on lines -173 to -178
/** 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;

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.

👍 it was weird for the layer to own its own abort controller

}
});
this._loadMetadata({
load: (signal) => this._parseGeoTIFF(signal),

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.

Hmm, instead of passing this down as a callback, I feel like overriding _loadMetadata would be cleaner.

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.

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), like shouldUpdateState;
  • loadMetadata(signal), like updateState.

RasterTileLayer.updateState calls the protected _loadMetadata() bookkeeping, like deck.gl's _update(), when shouldLoadMetadata returns true.

🤖 Written by Claude Code

Comment on lines +220 to +229
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);

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.

Much cleaner

* Call `onGeoTIFFLoad`, infer the default render pipeline, and store the
* parsed GeoTIFF in state.
*/
_applyGeoTIFF({

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.

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?

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.

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

Comment thread packages/deck.gl-zarr/src/zarr-layer.ts Outdated
Comment on lines +200 to +204
this._loadMetadata({
load: (signal) => this._parseZarr(signal),
apply: (layer, parsed) => layer.setState(parsed),
errorContext: "loading Zarr metadata",
});

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.

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?

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.

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

@kylebarron kylebarron changed the title fix: load layer metadata through one RasterTileLayer helper refactor: load layer metadata through one RasterTileLayer helper Oct 23, 2026
@github-actions github-actions Bot added refactor and removed fix labels Oct 23, 2026
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");

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.

Instead of just passing a string, this should be like

{errorMsg: "loading GeoTIFF"}

@kylebarron kylebarron Oct 26, 2026 •

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.

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.

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.

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 static layerName, 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() around updateState(). Subclasses override the public shouldLoadMetadata and loadMetadata, and never call the protected _loadMetadata(); RasterTileLayer.updateState does.

🤖 Written by Claude Code

signal?: AbortSignal;
},
): Promise<OpenedCogSource[] | null> {
): Promise<OpenedCogSource[]> {

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.

Much cleaner

* `errorContext`, such as `"loading GeoTIFF"`. Removing the layer aborts the
* load in flight.
*/
protected _reloadMetadata(errorContext: string): void {

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.

Yeah this should definitely be in an object as a named parameter

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.

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

Comment thread packages/deck.gl-zarr/src/zarr-layer.ts Resolved
kylebarron and others added 3 commits October 26, 2026 09:15
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]>
… 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]>

@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.

overall lgtm

} as typeof RasterTileLayer.defaultProps;

declare state: {
declare state: RasterTileLayer<DataT, COGLayerProps<DataT>>["state"] & {

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.

👍 I like this typing

Comment on lines +177 to +184
override shouldLoadMetadata({
props,
oldProps,
changeFlags,
}: UpdateParameters<this>): boolean {
return (
Boolean(changeFlags.dataChanged) || props.geotiff !== oldProps.geotiff
);

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.

👍 This is really clean.

We don't use the data prop, so do we even have to check dataChanged?

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.

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). So props.geotiff !== oldProps.geotiff is already true.
  • Later updates: dataChanged only reports changes to the data prop (or a dataComparator), 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";

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.

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?

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.

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]>

@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! Let's merge

@kylebarron
kylebarron merged commit 34123dd into main Oct 26, 2026
4 checks passed
@kylebarron
kylebarron deleted the kyle/load-metadata branch October 26, 2026 13:50
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