Ensure that Callback is called only once

Also, make Callback movable but uncopyable.
This commit is contained in:
Eelco Dolstra
2019-09-03 13:45:35 +02:00
parent 8c4ea7a451
commit 7348653ff4
5 changed files with 36 additions and 17 deletions
+7 -5
View File
@@ -137,17 +137,19 @@ protected:
auto request(makeRequest(path));
auto callbackPtr = std::make_shared<decltype(callback)>(std::move(callback));
getDownloader()->enqueueDownload(request,
{[callback, this](std::future<DownloadResult> result) {
{[callbackPtr, this](std::future<DownloadResult> result) {
try {
callback(result.get().data);
(*callbackPtr)(result.get().data);
} catch (DownloadError & e) {
if (e.error == Downloader::NotFound || e.error == Downloader::Forbidden)
return callback(std::shared_ptr<std::string>());
return (*callbackPtr)(std::shared_ptr<std::string>());
maybeDisable();
callback.rethrow();
callbackPtr->rethrow();
} catch (...) {
callback.rethrow();
callbackPtr->rethrow();
}
}});
}