mapbox / mapbox/mapbox-gl-js

setStyle(URL) is a race condition - subsequent addLayer() and addSource() possibly lost

Open
#10,534 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

mapbox-gl-js version: 1.13.0

Steps to Trigger Behavior
// first initialise map with say 5 layers
map.setStyle('mystyle.json'); // style containing 10 layers)
console.log(map.loaded(), map.isStyleLoaded(), map.getStyle().layers.length);
map.addLayer({ type: 'background', id: 'background' });

setTimeout(()=> console.log(map.getLayer('background')), 5000);

Expected Behavior

false, false, 10 (maybe - unclear what isStyleLoaded should do exactly)

Then print out the background layer we added

Actual Behavior

true, true, 5
undefined

Super frustrating, like #8691, #9779, #2268.

After calling setStyle() (with a JSON) it's difficult/impossible to know when the new style has actually taken effect. If you immediately add new layers, there is no error, but they apparently happen before the actual style change takes place, so they get removed.

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 by reproducing the issue with the provided setStyle(), addLayer(), and addSource() sequence, then trace the style-loading lifecycle and the loaded() and isStyleLoaded() entry points. Done means additions made after setStyle() are retained and the loading state accurately reflects when the new style is ready.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
frontend, web-dev
Issue type
Bug
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.