# FTXUI: task_queue.cpp Source File

Loading...
Searching...
No Matches
task_queue.cpp
Go to the documentation of this file.
// 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.
#include "ftxui/component/task_queue.hpp"
namespace ftxui::task {
auto TaskQueue::PostTask(PendingTask task) -> void {
if (!task.time) {
immediate_tasks_.push(task);
return;
}
if (task.time.value() < std::chrono::steady_clock::now()) {
immediate_tasks_.push(task);
return;
}
delayed_tasks_.push(task);
}
auto TaskQueue::Get() -> MaybeTask {
// Attempt to execute a task immediately.
if (!immediate_tasks_.empty()) {
auto task = immediate_tasks_.front();
immediate_tasks_.pop();
return task.task;
}
// Move all tasks that can be executed to the immediate queue.
auto now = std::chrono::steady_clock::now();
while (!delayed_tasks_.empty() && delayed_tasks_.top().time.value() <= now) {
immediate_tasks_.push(delayed_tasks_.top());
delayed_tasks_.pop();
}
// Attempt to execute a task immediately.
if (!immediate_tasks_.empty()) {
auto task = immediate_tasks_.front();
immediate_tasks_.pop();
return task.task;
}
// If there are no tasks to execute, return the delay until the next task.
if (!delayed_tasks_.empty()) {
return delayed_tasks_.top().time.value() - now;
}
// If there are no tasks to execute, return the maximum duration.
return std::monostate{};
}
} // namespace ftxui::task
ftxui::task
Definition screen_interactive.hpp:27
ftxui::task::PendingTask
Definition task_internal.hpp:18
ftxui::task::TaskQueue::Get
auto Get() -> MaybeTask
Definition task_queue.cpp:22
ftxui::task::TaskQueue::MaybeTask
std::variant< Task, std::chrono::steady_clock::duration, std::monostate > MaybeTask
Definition task_queue.hpp:24
ftxui::task::TaskQueue::PostTask
auto PostTask(PendingTask task) -> void
Definition task_queue.cpp:8
task_queue.hpp