Skip to content

Commit

Permalink
fix(executor): timed task
Browse files Browse the repository at this point in the history
  • Loading branch information
Serein207 committed Aug 11, 2024
1 parent df65ec3 commit 9c9f6e5
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Controller/AsyncExecutor.hh
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ private:
BOOST_ASIO_COMPLETION_TOKEN_FOR(
typename net::detail::awaitable_signature<net::result_of_t<TaskFunc()>>::type)
CompletionCallback>
requires std::is_invocable_v<CompletionCallback>
void asyncExecuteByTimerHelper(TaskFunc&& func, CompletionCallback&& callback, int taskId) {
_timers[taskId]->async_wait([=,
func = std::forward<TaskFunc>(func),
Expand All @@ -167,6 +168,37 @@ private:
});
}

template<typename TaskFunc,
BOOST_ASIO_COMPLETION_TOKEN_FOR(
typename net::detail::awaitable_signature<net::result_of_t<TaskFunc()>>::type)
CompletionCallback>
void asyncExecuteByTimerHelper(TaskFunc&& func, CompletionCallback&& callback, int taskId) {
_timers[taskId]->async_wait([=,
func = std::forward<TaskFunc>(func),
callback = std::forward<CompletionCallback>(callback),
this](const boost::system::error_code& ec) {
if (!ec) {
net::co_spawn(_ioc, func(), [callback](std::exception_ptr e, auto value) {
if (!e) {
slint::invoke_from_event_loop(
[callback = std::move(callback), value = std::move(value)]() {
callback(std::move(value));
});
return;
}
try {
std::rethrow_exception(e);
} catch (std::exception& ex) {
spdlog::error(ex.what());
}
});
asyncExecuteByTimerHelper(std::move(func), std::move(callback), taskId);
} else {
spdlog::error(ec.what());
}
});
}

private:
net::io_context _ioc;
std::thread _iocThread;
Expand Down

0 comments on commit 9c9f6e5

Please sign in to comment.