The ability to check if a style is loaded enough to perform some style operations #7346
Replies: 4 comments
-
|
The Is there a reason you need to perform some action between _loaded being true and |
Beta Was this translation helpful? Give feedback.
-
|
I believe that calling setData()/updateData() on a GeoJSON layer causes If we have a second, completely separate layer that is stable and safely populated, using One example would be changing the visibility of layer B whilst the GeoJSON in layer A is being updated. The style must be loaded to do this, but the pending data update on layer A doesn't need to block this. I think that maybe this is the purpose of the "style.load" event, and the intent was that this event should be tracked on my side and used as a filter before performing operations against the style, and not to use Basically, the issue is that In short, I just need to know when it's safe to call operations like |
Beta Was this translation helpful? Give feedback.
-
|
I use isStyleLoaded() after a call to setStyle(). Once isStyleLoaded() returns true, I do not gate any additional operations based on that until/unless another call to setStyle() is made. At one point, the "style.load" event only happened after the first call to setStyle(), but I don't know if that's still the case. Specifically, here's some pseudocode of what I do when making a setStyle call:
|
Beta Was this translation helpful? Give feedback.
-
|
In my case, I need to know when it’s safe to add and remove sources and layers since our maps’ contents are highly dynamic. I’ve written this: /**
* Adding and removing layers in MapLibre is not possible until the map
* instance's style data is loaded. This can be used to `await` that
* state.
*/
export const getStyleLoadedPromise = (map: MapLibre.Map): Promise<void> => {
if (map.isStyleLoaded()) {
return Promise.resolve();
}
return new Promise((resolve) => {
map.once("styledata", () => {
resolve();
});
});
};This would work if the Perhaps the meaning of style load state is a bit too overloaded? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
At present, map.isStyleLoaded() is publicly exposed to allow checking of whether the style has fully loaded.
This calls through to ...style.loaded(), which looks like this:
That is, it checks if the style is loaded AND there are no outstanding operations on it.
However, lots of operations on the style (like adding and removing layers, setLayoutProperty, etc) internally call _checkLoaded(), which looks like this:
_checkLoaded() only inspects
_loaded, which I assume means that with that flag set as loaded, it is safe to perform these operations.My issue is, how do I know whether the style is in this state, where I can safely perform these operations?
I can't rely on isStyleLoaded(), as it returns false for a whole load of other conditions that are not blocking on these operations (for example, if I call setData() and that call is yet to complete, isStyleLoaded returns false, but the style is in a perfectly good state for me to act on).
Is there a publicly exposed API to allow me to check that the style is loaded to a position where I can perform these operations?
Do I just need to wrap all these calls in a try/catch to see if I get this error thrown or (even worse), do I just root into the private state and check
_loadedmyself?I assume another option is to listen for the "style.load" event and latch when this has occurred? (as an aside, this even doesn't seem to be documented, at least, I couldn't find it in the API reference).
Beta Was this translation helpful? Give feedback.
All reactions