Keep observers encapsulated

This commit is contained in:
John Firebaugh
2015-04-16 17:11:49 -07:00
parent f4adef1889
commit 2a8ddc9f27
3 changed files with 13 additions and 14 deletions

View File

@@ -184,22 +184,14 @@ void DefaultFileSource::Impl::abort(const Environment& env) {
}
void DefaultFileSource::Impl::notify(SharedRequestBase *sharedRequest,
const std::set<Request *> &observers,
std::shared_ptr<const Response> response, FileCache::Hint hint) {
// First, remove the request, since it might be destructed at any point now.
assert(find(sharedRequest->resource) == sharedRequest);
pending.erase(sharedRequest->resource);
if (response) {
if (cache) {
// Store response in database
cache->put(sharedRequest->resource, response, hint);
}
// Notify all observers.
for (auto req : observers) {
req->notify(response);
}
if (response && cache) {
// Store response in database
cache->put(sharedRequest->resource, response, hint);
}
}

View File

@@ -14,7 +14,7 @@ class DefaultFileSource::Impl {
public:
Impl(FileCache *cache, const std::string &root = "");
void notify(SharedRequestBase *sharedRequest, const std::set<Request *> &observers,
void notify(SharedRequestBase *sharedRequest,
std::shared_ptr<const Response> response, FileCache::Hint hint);
SharedRequestBase *find(const Resource &resource);

View File

@@ -35,7 +35,14 @@ public:
void notify(std::shared_ptr<const Response> response, FileCache::Hint hint) {
MBGL_VERIFY_THREAD(tid);
source.notify(this, observers, response, hint);
source.notify(this, response, hint);
if (response) {
// Notify all observers.
for (auto req : observers) {
req->notify(response);
}
}
}
void subscribe(Request *request) {
@@ -52,7 +59,7 @@ public:
if (abandoned()) {
// There are no observers anymore. We are initiating cancelation.
// First, remove this SharedRequestBase from the source.
source.notify(this, observers, nullptr, FileCache::Hint::No);
source.notify(this, nullptr, FileCache::Hint::No);
// Then, initiate cancelation of this request
cancel();