2025-08-16 18:40:50 +02:00
|
|
|
// Copyright 2024 Arthur Sonzogni. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by the MIT license that can be found in
|
|
|
|
|
// the LICENSE file.
|
|
|
|
|
#ifndef TASK_RUNNER_HPP
|
|
|
|
|
#define TASK_RUNNER_HPP
|
|
|
|
|
|
|
|
|
|
#include "ftxui/component/task_internal.hpp"
|
|
|
|
|
#include "ftxui/component/task_queue.hpp"
|
|
|
|
|
|
|
|
|
|
namespace ftxui::task {
|
|
|
|
|
|
|
|
|
|
class TaskRunner {
|
|
|
|
|
public:
|
|
|
|
|
TaskRunner();
|
|
|
|
|
~TaskRunner();
|
|
|
|
|
|
|
|
|
|
// Returns the task runner for the current thread.
|
|
|
|
|
static auto Current() -> TaskRunner*;
|
|
|
|
|
|
|
|
|
|
/// Schedules a task to be executed immediately.
|
|
|
|
|
auto PostTask(Task task) -> void;
|
|
|
|
|
|
|
|
|
|
/// Schedules a task to be executed after a certain duration.
|
2025-09-21 10:30:52 +02:00
|
|
|
auto PostDelayedTask(Task task,
|
|
|
|
|
std::chrono::steady_clock::duration duration) -> void;
|
2025-08-16 18:40:50 +02:00
|
|
|
|
|
|
|
|
/// Runs the tasks in the queue, return the delay until the next delayed task
|
|
|
|
|
/// can be executed.
|
|
|
|
|
auto RunUntilIdle() -> std::optional<std::chrono::steady_clock::duration>;
|
|
|
|
|
|
|
|
|
|
// Runs the tasks in the queue, blocking until all tasks are executed.
|
|
|
|
|
auto Run() -> void;
|
|
|
|
|
|
2025-08-17 11:18:25 +02:00
|
|
|
bool HasImmediateTasks() const { return queue_.HasImmediateTasks(); }
|
2025-08-16 18:40:50 +02:00
|
|
|
|
|
|
|
|
size_t ExecutedTasks() const { return executed_tasks_; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
TaskRunner* previous_task_runner_ = nullptr;
|
|
|
|
|
TaskQueue queue_;
|
|
|
|
|
size_t executed_tasks_ = 0;
|
|
|
|
|
};
|
|
|
|
|
|
2025-08-17 11:18:25 +02:00
|
|
|
} // namespace ftxui::task
|
2025-08-16 18:40:50 +02:00
|
|
|
|
|
|
|
|
#endif // TASK_RUNNER_HPP
|