Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Cancel pending Style requests before making a new one
Browse files Browse the repository at this point in the history
Calling setStyleURL() multiple times in a short period
of time will enqueue requests on the FileSource. The
order they get replied is not guaranteed to be the same
as the order we make the requests. The side effect was
the last style set not always being the one rendered.

Now we cancel the current request if we make a new one
(and there was no point on keeping it around anyway).
  • Loading branch information
tmpsantos committed Jul 1, 2015
1 parent 176e42c commit 295ea08
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/mbgl/map/map_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ MapContext::~MapContext() {
void MapContext::cleanup() {
view.notify();

if (styleRequest) {
FileSource* fs = util::ThreadContext::getFileSource();
fs->cancel(styleRequest);
styleRequest = nullptr;
}

// Explicit resets currently necessary because these abandon resources that need to be
// cleaned up by glObjectStore.performCleanup();
style.reset();
Expand Down Expand Up @@ -85,6 +91,12 @@ void MapContext::triggerUpdate(const TransformState& state, const Update u) {
}

void MapContext::setStyleURL(const std::string& url) {
FileSource* fs = util::ThreadContext::getFileSource();

if (styleRequest) {
fs->cancel(styleRequest);
}

styleURL = url;
styleJSON.clear();

Expand All @@ -94,8 +106,9 @@ void MapContext::setStyleURL(const std::string& url) {
base = styleURL.substr(0, pos + 1);
}

FileSource* fs = util::ThreadContext::getFileSource();
fs->request({ Resource::Kind::Style, styleURL }, util::RunLoop::current.get()->get(), [this, base](const Response &res) {
styleRequest = fs->request({ Resource::Kind::Style, styleURL }, util::RunLoop::current.get()->get(), [this, base](const Response &res) {
styleRequest = nullptr;

if (res.status == Response::Successful) {
loadStyleJSON(res.data, base);
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/mbgl/map/map_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ class MapContext : public Style::Observer {
std::string styleURL;
std::string styleJSON;

Request* styleRequest = nullptr;

StillImageCallback callback;
size_t sourceCacheSize;
TransformState transformState;
Expand Down

0 comments on commit 295ea08

Please sign in to comment.