mapbox / mapbox/mapbox-gl-js

isStyleLoaded doesn't consistently tell us if addLayer can be executed

Open
#10,884 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I'm trying to get started with a Mapbox GL JS project, and the first issue I ran into is that addLayer will throw an exception if the style has not loaded. To work around this, I tried the suggested solution of using isStyleLoaded and adding a listener to the 'load' event to add the layer if this fails.

However, there is a problem with this when adding multiple layers at once after the load event is fired. Once the first layer is added, it seems that something is added to the queue that causes isStyleLoaded to fail. However, the load event will not be fired again. However, the addLayer method will work if you try it, as it uses a private _checkLoaded function that only checks the _loaded flag (and not the cache checked by the public loaded function used by isStyleLoaded).

While it is possible to work around this by using the idle event, it doesn't seem ideal to wait for this after each addLayer call, particularly since this seems unnecessary. Is there a supported way to check if addLayer can be executed, and if not could one be added in a future update? Having to use idle or check a private flag seems like a hackish workaround, particularly when I also need another set of listeners (styledataloading/styledata) to handle style changes unless we use the undocumented style.data.

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 with the public isStyleLoaded and addLayer paths, including the private _checkLoaded behavior described in the issue. Reproduce multiple addLayer calls after the load event and compare the load, idle, styledataloading, and styledata events. Done means the supported readiness behavior is consistent and documented or covered by tests.

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
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.