Simplify RunLoop

Instead of transforming between return value and callback, pass
the wrapped callback on to the invoked function as the last argument.

This eliminates the need for multiple invokeWithArgument overloads
and permits invoked functions to be asynchronous.
This commit is contained in:
John Firebaugh
2015-06-11 19:03:19 -04:00
parent 3ab682e627
commit fc64e97bce
8 changed files with 149 additions and 216 deletions

View File

@@ -131,10 +131,10 @@ std::unique_ptr<WorkRequest> SQLiteCache::get(const Resource &resource, Callback
// Will try to load the URL from the SQLite database and call the callback when done.
// Note that the callback is probably going to invoked from another thread, so the caller
// must make sure that it can run in that thread.
return thread->invokeWithResult(&Impl::get, std::move(callback), resource);
return thread->invokeWithCallback(&Impl::get, callback, resource);
}
std::unique_ptr<Response> SQLiteCache::Impl::get(const Resource &resource) {
void SQLiteCache::Impl::get(const Resource &resource, Callback callback) {
try {
// This is called in the SQLite event loop.
if (!db) {
@@ -167,14 +167,14 @@ std::unique_ptr<Response> SQLiteCache::Impl::get(const Resource &resource) {
if (getStmt->get<int>(5)) { // == compressed
response->data = util::decompress(response->data);
}
return std::move(response);
callback(std::move(response));
} else {
// There is no data.
return nullptr;
callback(nullptr);
}
} catch (mapbox::sqlite::Exception& ex) {
Log::Error(Event::Database, ex.code, ex.what());
return nullptr;
callback(nullptr);
}
}