From 295ea0856becc35fe77ee7c207f533be3b7589ee Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Wed, 1 Jul 2015 10:07:12 +0300 Subject: [PATCH] Cancel pending Style requests before making a new one 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). --- src/mbgl/map/map_context.cpp | 17 +++++++++++++++-- src/mbgl/map/map_context.hpp | 2 ++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/mbgl/map/map_context.cpp b/src/mbgl/map/map_context.cpp index 9dadd05d37f..fa06a426721 100644 --- a/src/mbgl/map/map_context.cpp +++ b/src/mbgl/map/map_context.cpp @@ -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(); @@ -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(); @@ -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 { diff --git a/src/mbgl/map/map_context.hpp b/src/mbgl/map/map_context.hpp index 715a55bf152..141a8ee9647 100644 --- a/src/mbgl/map/map_context.hpp +++ b/src/mbgl/map/map_context.hpp @@ -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;