Skip to content

feat: Add dataDiff prop for incremental GeoJSON updates via updateData()#162

Open
samtalki wants to merge 3 commits into
MIERUNE:mainfrom
samtalki:feat/geojson-update-data
Open

feat: Add dataDiff prop for incremental GeoJSON updates via updateData()#162
samtalki wants to merge 3 commits into
MIERUNE:mainfrom
samtalki:feat/geojson-update-data

Conversation

@samtalki

Copy link
Copy Markdown

Summary

Adds a dataDiff prop to GeoJSONSource (and RawSource) that calls MapLibre's GeoJSONSource.updateData(diff) instead of setData(). This resolves the TODO comment at RawSource.svelte line 95:

// TODO: support diffrential update ? (updateData)

Motivation

For applications rendering large GeoJSON datasets (thousands of features), every data prop change currently triggers a full setData() — which serializes the entire FeatureCollection to a web worker, re-runs geojson-vt tiling from scratch, and re-uploads all visible tiles to the GPU.

MapLibre GL JS v4+ provides updateData(diff) which accepts incremental diffs. When only feature properties change (not geometry), this skips the full re-tile entirely — only updating affected features in-place and reloading intersecting tiles.

Usage

<script>
  import { GeoJSONSource } from 'svelte-maplibre-gl';
  import type { GeoJSONSourceDiff } from 'maplibre-gl';

  let data = $state(initialGeoJSON);
  let dataDiff = $state<GeoJSONSourceDiff | undefined>(undefined);

  function updateColors() {
    // Incremental: only update properties, skip re-tiling
    dataDiff = {
      update: features.map(f => ({
        id: f.id,
        addOrUpdateProperties: [
          { key: 'color', value: computeColor(f) }
        ]
      }))
    };
  }
</script>

<GeoJSONSource {data} {dataDiff}>
  <CircleLayer paint={{ 'circle-color': ['get', 'color'] }} />
</GeoJSONSource>
  • data → calls setData() (full replace) — use for structural changes
  • dataDiff → calls updateData() (incremental) — use for property-only changes
  • Both props can coexist: initial load via data, subsequent updates via dataDiff

Changes

  • RawSource.svelte: Added dataDiff to Props type, new $effect that calls source.updateData(dataDiff) (same pattern as existing setData effect)
  • GeoJSONSource.svelte: Added dataDiff to Props interface, passes through to RawSource

Checklist

  • pnpm check:all — 0 errors, 0 warnings (11/11 packages)
  • pnpm build:packages — all packages build
  • pnpm test:unit -- --run — all tests pass
  • Non-breaking: new optional prop, existing behavior unchanged
  • Changeset included (minor version bump)

Resolves the TODO at RawSource.svelte line 95. Adds a `dataDiff` prop to
GeoJSONSource that calls MapLibre's updateData(diff) instead of setData().
Property-only changes skip the full geojson-vt re-tile.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings March 30, 2026 05:18
@changeset-bot

changeset-bot Bot commented Mar 30, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3a8bd54

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

💥 An error occurred when fetching the changed packages and changesets in this PR
Some errors occurred when validating the changesets config:
The package or glob expression "@svelte-maplibre-gl/docs" is specified in the `ignore` option but it is not found in the project. You may have misspelled the package name or provided an invalid glob expression. Note that glob expressions must be defined according to https://www.npmjs.com/package/micromatch.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds an optional dataDiff prop to enable incremental GeoJSON updates via MapLibre’s GeoJSONSource.updateData() for better performance on large datasets.

Changes:

  • Extend RawSource and GeoJSONSource props with dataDiff?: GeoJSONSourceDiff
  • Add a new reactive effect in RawSource that applies updateData(dataDiff) after initial mount
  • Add a changeset to publish a minor version with the new API

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
svelte-maplibre-gl/src/lib/sources/RawSource.svelte Adds dataDiff prop and a new $effect that calls GeoJSONSource.updateData()
svelte-maplibre-gl/src/lib/sources/GeoJSONSource.svelte Wires dataDiff through to RawSource
.changeset/geojson-update-data.md Announces new dataDiff prop and minor bump

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread svelte-maplibre-gl/src/lib/sources/RawSource.svelte
Comment thread svelte-maplibre-gl/src/lib/sources/RawSource.svelte Outdated
Comment thread svelte-maplibre-gl/src/lib/sources/RawSource.svelte
@pkg-pr-new

pkg-pr-new Bot commented Mar 30, 2026

Copy link
Copy Markdown

Open in StackBlitz

svelte-maplibre-gl

npm i https://pkg.pr.new/MIERUNE/svelte-maplibre-gl@162

@svelte-maplibre-gl/contour

npm i https://pkg.pr.new/MIERUNE/svelte-maplibre-gl/@svelte-maplibre-gl/contour@162

@svelte-maplibre-gl/deckgl

npm i https://pkg.pr.new/MIERUNE/svelte-maplibre-gl/@svelte-maplibre-gl/deckgl@162

@svelte-maplibre-gl/pmtiles

npm i https://pkg.pr.new/MIERUNE/svelte-maplibre-gl/@svelte-maplibre-gl/pmtiles@162

@svelte-maplibre-gl/terradraw

npm i https://pkg.pr.new/MIERUNE/svelte-maplibre-gl/@svelte-maplibre-gl/terradraw@162

commit: 3a8bd54

Address review feedback:
- Single $effect tracks both data and dataDiff to prevent race conditions
  when both change in the same tick (data takes priority, diff is skipped)
- Runtime guard: throws clear error if updateData() is not available
  (MapLibre GL JS < v4)
- prevData tracking ensures diff-only updates don't re-trigger setData()

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Comment thread svelte-maplibre-gl/src/lib/sources/RawSource.svelte Outdated
Comment thread svelte-maplibre-gl/src/lib/sources/RawSource.svelte Outdated
Reverts the merged single-effect approach. Separate effects are simpler
and preserve compatibility with in-place $state mutations on data (where
reference equality fails but Svelte's proxy still triggers the effect).

The setData effect is unchanged from the original library code.
The updateData effect is the new addition for dataDiff.

When both fire in the same tick, MapLibre's internal pending update queue
handles ordering correctly.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@ciscorn

ciscorn commented May 10, 2026

Copy link
Copy Markdown
Member

@samtalki Thanks for the PR! It'd be nice to leverage Svelte's reactivity for updateData, but I'm not sure a reactive prop is the right fit. A diff feels more like a one-shot command than declarative state, which makes it tricky to decide when to re-apply it and how it should interact with data updates. Still figuring out what the right shape would be.

@samtalki

Copy link
Copy Markdown
Author

Totally understand -- sorry for any inconvenience, I'm still learning frontend. Please let me know if there's anything I can do to improve this! @ciscorn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants