Skip to content
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

Wait to start loading source assets until onAdd is called #3761

Merged
merged 5 commits into from
Dec 20, 2016
Merged

Wait to start loading source assets until onAdd is called #3761

merged 5 commits into from
Dec 20, 2016

Conversation

lbud
Copy link
Contributor

@lbud lbud commented Dec 7, 2016

Fixes #3735 by taking advantage of the onAdd method to load assets/fire events. Previously we were doing those in the source constructors, and they were constructed before establishing ownership (map -> style -> source_cache -> source), meaning events fired almost immediately wouldn't bubble up to the map object — so for example, vector tile sources (loaded over network connection) would reliably trigger map.on('source.load', fn) listeners, but canvas sources which loaded very quickly (from the same page) would not.

This isn't a perfect solution, but since sources will soon undergo a pretty big overhaul… 🔨

cc @lucaswoj

Copy link
Contributor

@lucaswoj lucaswoj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what cases did calling setEventedParent in the source constructors fail?

sourceCache.onAdd(this.map);

this._changed = true;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the changes to this file significant?

@lbud
Copy link
Contributor Author

lbud commented Dec 8, 2016

@lucaswoj the way it has been working:

  • Style creates instances of SourceCaches, then sets their parent: style.js#L376-L382
    • it does this because its eventedParentData is dependent on the Source having already been created (sourceCache.serialize() and sourceCache.loaded() both just forward to sourceCache._source methods)
    • this is where the race occurs: when sources fire events in their constructors before we've set up SourceCache's parent.
      • Specifically I was seeing this with the canvas source branch, since we load so quickly (from the same page).
      • However, at a glance I can see that other events like VectorTileSource#dataloading wouldn't always bubble up to Map for the same reason. Was just able to reproduce this with dynamically adding a vector tile set (it fires source.load because it's slow enough, but never dataloading)
  • SourceCache creates instances of Sources, passing itself as a constructor so a source can be responsible for calling setEventedParent

Another minor, related change I made in this PR is to pass Style in the ImageSprite constructor, which looked like it could feasibly suffer the same race condition: https://github.com/mapbox/mapbox-gl-js/pull/3761/files#diff-6369a555a491e3e9a58fd29c3fd5a371L97

@lbud lbud mentioned this pull request Dec 8, 2016
5 tasks
Copy link
Contributor

@lucaswoj lucaswoj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I appreciate that this is blocking #3765 and want to 🚢 to keep that project moving.

Some thoughts on future refactors: (I'll tix these out seperately)

@lucaswoj
Copy link
Contributor

@lbud Are you ready to merge this PR? I'd hate for a rebase conflict to emerge 😅

@lbud
Copy link
Contributor Author

lbud commented Dec 20, 2016

Oops yes @lucaswoj — I paused on this with all my naïve optimism about #3186 😬
🚢

@lbud lbud merged commit fb17325 into master Dec 20, 2016
@lbud lbud deleted the 3735 branch December 20, 2016 19:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants