2015-09-03 02:28:50 +00:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2024-09-06 12:16:56 +00:00
|
|
|
#include "shell/app/uv_task_runner.h"
|
|
|
|
|
2017-06-16 20:39:08 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2021-07-02 00:51:37 +00:00
|
|
|
#include "base/location.h"
|
2022-02-25 18:17:35 +00:00
|
|
|
#include "base/time/time.h"
|
2015-09-03 02:28:50 +00:00
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
2024-09-06 12:16:56 +00:00
|
|
|
UvTaskRunner::UvTaskRunner(uv_loop_t* loop) : loop_{loop} {}
|
2015-09-03 02:28:50 +00:00
|
|
|
|
2024-09-06 12:16:56 +00:00
|
|
|
UvTaskRunner::~UvTaskRunner() = default;
|
2015-09-03 02:28:50 +00:00
|
|
|
|
2017-12-17 23:56:18 +00:00
|
|
|
bool UvTaskRunner::PostDelayedTask(const base::Location& from_here,
|
2017-06-16 20:39:08 +00:00
|
|
|
base::OnceClosure task,
|
2015-09-03 02:28:50 +00:00
|
|
|
base::TimeDelta delay) {
|
2024-09-06 12:16:56 +00:00
|
|
|
auto on_timeout = [](uv_timer_t* timer) {
|
|
|
|
auto& tasks = static_cast<UvTaskRunner*>(timer->data)->tasks_;
|
|
|
|
if (auto iter = tasks.find(timer); iter != tasks.end())
|
|
|
|
std::move(tasks.extract(iter).mapped()).Run();
|
|
|
|
};
|
|
|
|
|
|
|
|
auto timer = UvHandle<uv_timer_t>{};
|
2015-09-03 02:28:50 +00:00
|
|
|
timer->data = this;
|
2024-09-06 12:16:56 +00:00
|
|
|
uv_timer_init(loop_, timer.get());
|
|
|
|
uv_timer_start(timer.get(), on_timeout, delay.InMilliseconds(), 0);
|
|
|
|
tasks_.insert_or_assign(std::move(timer), std::move(task));
|
2015-09-03 02:28:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-07 21:14:35 +00:00
|
|
|
bool UvTaskRunner::RunsTasksInCurrentSequence() const {
|
2015-09-03 02:28:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
bool UvTaskRunner::PostNonNestableDelayedTask(const base::Location& from_here,
|
|
|
|
base::OnceClosure task,
|
|
|
|
base::TimeDelta delay) {
|
2017-06-16 20:39:08 +00:00
|
|
|
return PostDelayedTask(from_here, std::move(task), delay);
|
2015-09-03 02:28:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace electron
|