-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"load" event not firing #6076
Comments
Hmm, using |
Hmm, actually no. If the user switches layers rapidly (that is, I call my |
@stevage it's hard to diagnose your problem without an example that reproduces it, but I'd suspect if you're finding a situation where your load listener is never being invoked, there's probably a race condition where the listener isn't attached before the load event gets fired. A jsfiddle reproducing this issue could confirm that hunch or otherwise help us diagnose exactly what it is. As to your second question, it's unlikely that we'll implement a system to queue mutations for when the style is "ready" for whatever that mutation is, as doing so would force us to make assumptions about what the developer wants to do and in what order. (You alluded to this, sort of, in
…because what if for some application-specific reason you did care? :) ) |
Yeah, I know. Making repros isn't always practical, though, for various reasons. I do try.
It's funny though, I end up using this A common example for me is:
It's not safe to use
Then I'd revert to using these more specific handlers. Currently, |
Ok, I've made a JSBin which I hope demonstrates that there is either a bug, or the documentation is unclear, or...something. :) https://jsbin.com/yahirajuxe/edit?html,console,output I'm starting with the (possibly false) assumption that:
After 5 seconds, this map does the following:
The results (on my computer):
In other words: Just to be clear: this is not just academic interest. I'm having a lot of trouble with a site now in production with this scenario:
Any reliable workaround would be great. |
I've made another version, focusing on whether you can use In this version, every time you see something like Output (edited):
Notes:
My conclusions:
My best guess at a workaround at this point would involve some kind of chaining: wait, check whether the map is in a good state, and if not, wait again. Or maybe, just listen out for the "error" message, but it's probably really hard to pass state through it. |
FWIW, this
Bonus: the initial page load is faster, as we can add the data after the initial tiles have loaded, but before the first map paint is complete. |
This isn't correct.
It sounds like you're looking for #1715. |
Reading back to try to glean your concrete use case:
What you need to do here is not specifically GL JS related, but a general concurrent programming problem: how do you do something only when two asynchronous tasks are both complete? One approach is something like:
|
Thanks, but I don't believe I am looking for #1715. Use case there seem to be around notifying the user that data is still loading, or knowing when it's safe to start querying data in the map (because all the data is there). My use case is really simple: a guaranteed way to call
Yes, thanks - I don't need help with the concurrent programming bit :) The problem (as I explained in quite a lot of detail here ) is that this part of your solution:
...doesn't work. As you note, it only fires the first time a map loads, and never again. So in this scenario:
There is seemingly no way to know when it's safe to call step 4, because you can't wait for |
In this scenario, why do you need to wait for 3 to complete to do 4? |
Another guess about what you mean: you have two or more independent things that need to happen once the map is loaded. (But they don't necessarily need to be ordered with respect to each other.) var ready = false;
map.on('load', function () { ready = true; });
function onReady(cb) {
if (ready) {
cb();
} else {
map.on('load', cb);
}
}
onReady(function () { map.addLayer(...) });
onReady(function () { map.addLayer(...) }); If that's not it either, please please please provide a JSFiddle. I'm begging you! 😅 |
Heh. I can't provide an actual JSfiddle because it's a confidential site for a client, full of confidential data. So the best I can do is to provide a simulation demonstrating the issue, which is exactly what I did here and here. Here's how this app works:
Now, I was using Ok, so to answer your question:
Because: all I want is a reliable way to add layers, no matter what the current state of the map is. I guess you know that I don't need to wait for one So, I guess, I'm begging you: tell me what the body of this function should be:
My current best guess is:
but honestly I don't know if there are race conditions, or particular scenarios where it will fail. |
Having a use case where I filter 4 based on Currently I use @stevage did you notice #4222 (comment)? Maybe this helps in your use case... |
(deleted previous comment bc I think @jfirebaugh had the right response in #6076 (comment)) |
|
Ok, thank you. If you compare your solution to what I wrote in the original problem description, it boils down to the fact that The thing that confuses me most about this whole thread is why no one else seems to think this is much of a problem :) The actual version I'm now using in production:
This version (listening to 'styledata', not 'load') also seems to get to first page paint faster. This way, the extra data layer gets rendered a second or two before the basemap arrives. If I listen to 'load', the basemap paints (which takes a couple of seconds of vector tile fetching - slow internet in Australia), and then finally the data layer (which is what my users care about) paints. It might be worth improving the documentation of |
mapbox-gl-js version: 0.43.1 dev
I'm having an issue where sometimes the map's "load" event is not firing. In response to a user event, I'm adding and removing several layers at once, so to avoid a "Style is not done loading" error, I use this code:
where
map.onLoad
is:Mostly this works as expected. But in this particular case, for whatever reason, the event never fires (so the "On loaded!" message never shows, and the function never gets called). There are no console messages.
It would be pretty hard to create a repro case from this. So I'm asking:
addLayer()
please, please abstract away this monitoring of whether the style is ready or not? It's my biggest annoyance working with Mapbox-GL-JS on a daily basis. I would loveaddLayer()
to just...add the layer. Now or later, I don't care, whenever the style is ready - just don't give me an error message, and don't require me to implement two different code paths to handle a state property I'm not very interested in.The text was updated successfully, but these errors were encountered: