diff options
author | dec05eba <dec05eba@protonmail.com> | 2020-10-31 17:02:09 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-10-31 17:02:09 +0100 |
commit | 71424d0fe48bf4cf62626f1e36e2e5f861cb018d (patch) | |
tree | 86242495743517e43e6f8cf94181c77b3b244289 /include | |
parent | d638a6092bd6291c983490ba3f966162c7ca06c2 (diff) |
Matrix: cancel tasks when leaving chat page. This fixes delay in chat page close sometimes
Diffstat (limited to 'include')
-rw-r--r-- | include/AsyncTask.hpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/include/AsyncTask.hpp b/include/AsyncTask.hpp new file mode 100644 index 0000000..0d9c453 --- /dev/null +++ b/include/AsyncTask.hpp @@ -0,0 +1,65 @@ +#pragma once + +#include "Program.hpp" +#include <thread> +#include <future> + +namespace QuickMedia { + template <typename T> + class AsyncTask { + public: + using CallbackFunc = std::function<T()>; + + AsyncTask() = default; + + AsyncTask(CallbackFunc callback_func) { + std::promise<T> promise; + future = promise.get_future(); + thread = std::thread(&AsyncTask::thread_handler, this, std::move(promise), std::move(callback_func)); + } + + AsyncTask& operator=(CallbackFunc callback_func) { + cancel(); + std::promise<T> promise; + future = promise.get_future(); + thread = std::thread(&AsyncTask::thread_handler, this, std::move(promise), std::move(callback_func)); + return *this; + } + + ~AsyncTask() { + cancel(); + } + + bool valid() { + return future.valid(); + } + + bool ready() { + return future.valid() && future.wait_for(std::chrono::seconds(0)) == std::future_status::ready; + } + + T get() { + thread.join(); + return future.get(); + } + + void cancel() { + if(valid()) { + program_kill_in_thread(thread.get_id()); + get(); + } + } + private: + void thread_handler(std::promise<T> &&promise, CallbackFunc callback_func) { + if constexpr(std::is_same<T, void>::value) { + callback_func(); + promise.set_value(); + } else { + promise.set_value(callback_func()); + } + } + private: + std::thread thread; + std::future<T> future; + }; +}
\ No newline at end of file |