mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 22:27:35 +00:00
LibThreading+Everywhere: Support returning error from BackgroundAction
This patch allows returning an `Error` from the `on_complete` callback in `BackgroundAction`. It also adds a custom callback to manage errors returned during its execution.
This commit is contained in:
parent
664117564a
commit
2693745336
6 changed files with 38 additions and 21 deletions
|
@ -670,7 +670,7 @@ bool FileSystemModel::fetch_thumbnail_for(Node const& node)
|
|||
return render_thumbnail(path);
|
||||
},
|
||||
|
||||
[this, path, weak_this](auto thumbnail_or_error) {
|
||||
[this, path, weak_this](auto thumbnail_or_error) -> ErrorOr<void> {
|
||||
if (thumbnail_or_error.is_error()) {
|
||||
s_thumbnail_cache.set(path, nullptr);
|
||||
dbgln("Failed to load thumbnail for {}: {}", path, thumbnail_or_error.error());
|
||||
|
@ -681,7 +681,7 @@ bool FileSystemModel::fetch_thumbnail_for(Node const& node)
|
|||
// The model was destroyed, no need to update
|
||||
// progress or call any event handlers.
|
||||
if (weak_this.is_null())
|
||||
return;
|
||||
return {};
|
||||
|
||||
m_thumbnail_progress++;
|
||||
if (on_thumbnail_progress)
|
||||
|
@ -692,6 +692,7 @@ bool FileSystemModel::fetch_thumbnail_for(Node const& node)
|
|||
}
|
||||
|
||||
did_update(UpdateFlag::DontInvalidateIndices);
|
||||
return {};
|
||||
});
|
||||
|
||||
return false;
|
||||
|
|
|
@ -52,16 +52,21 @@ public:
|
|||
virtual ~BackgroundAction() = default;
|
||||
|
||||
private:
|
||||
BackgroundAction(Function<Result(BackgroundAction&)> action, Function<void(Result)> on_complete)
|
||||
BackgroundAction(Function<Result(BackgroundAction&)> action, Function<ErrorOr<void>(Result)> on_complete, Optional<Function<void(Error)>> on_error = {})
|
||||
: Core::Object(&background_thread())
|
||||
, m_action(move(action))
|
||||
, m_on_complete(move(on_complete))
|
||||
{
|
||||
if (on_error.has_value())
|
||||
m_on_error = on_error.release_value();
|
||||
|
||||
enqueue_work([this, origin_event_loop = &Core::EventLoop::current()] {
|
||||
m_result = m_action(*this);
|
||||
if (m_on_complete) {
|
||||
origin_event_loop->deferred_invoke([this] {
|
||||
m_on_complete(m_result.release_value());
|
||||
auto maybe_error = m_on_complete(m_result.release_value());
|
||||
if (maybe_error.is_error())
|
||||
m_on_error(maybe_error.release_error());
|
||||
remove_from_parent();
|
||||
});
|
||||
origin_event_loop->wake();
|
||||
|
@ -73,7 +78,10 @@ private:
|
|||
|
||||
bool m_cancelled { false };
|
||||
Function<Result(BackgroundAction&)> m_action;
|
||||
Function<void(Result)> m_on_complete;
|
||||
Function<ErrorOr<void>(Result)> m_on_complete;
|
||||
Function<void(Error)> m_on_error = [](Error error) {
|
||||
dbgln("Error occurred while running a BackgroundAction: {}", error);
|
||||
};
|
||||
Optional<Result> m_result;
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue