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:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ public:
|
||||
Impl(uv_loop_t*, const std::string &path = ":memory:");
|
||||
~Impl();
|
||||
|
||||
std::unique_ptr<Response> get(const Resource&);
|
||||
void get(const Resource&, Callback);
|
||||
void put(const Resource& resource, std::shared_ptr<const Response> response);
|
||||
void refresh(const Resource& resource, int64_t expires);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user