mapbox / mapbox/mapbox-gl-js

Use (shallow) merge semantics for `map.setStyle()`

Open
#6,701 5 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

api :memo: feature :green_apple:
Dominant language
TypeScript
Stars
12.4k
Forks
2.4k
PR merge metrics
No merged PRs in 30d

Description

## Motivation

`Map#setStyle()` currently does a deep equality comparison when diffing sources: https://github.com/mapbox/mapbox-gl-js/blob/a5a8dfe5b90737dbb295eaca7f41c667ae4060a8/src/style-spec/diff.js#L153

This is unnecessarily costly for styles using inline / dynamically-created GeoJSON data. And more generally, it's very common that the caller of `map.setStyle()` knows that the sources haven't changed.

See also @averas's comment here: https://github.com/mapbox/mapbox-gl-js/pull/3643#issuecomment-275741172

## Design Alternatives

#4006
#4000

## Design

Per the title, I propose that we do a shallow merge from `map.setStyle()`. In practice, this means that a user could omit omit any top-level field (`sources`, `layers`, etc.) of the style they're passing to `map.setStyle()`, and we would just use the existing style state for that field.

### Mock-Up

```js
map.setStyle({
layers: [ ... new layers ...]
// don't worry about setting sources:, sprite:, etc.
})
```

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at map.setStyle() and inspect the source diffing logic in src/style-spec/diff.js at the linked comparison. Trace how omitted top-level style fields are handled and how source equality is checked; done means omitted fields preserve existing style state and source comparison uses shallow merge semantics rather than deep equality.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
frontend, web-dev
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.