uv_task_runner.cc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/app/uv_task_runner.h"
  5. #include <utility>
  6. #include "base/location.h"
  7. #include "base/stl_util.h"
  8. #include "base/time/time.h"
  9. namespace electron {
  10. UvTaskRunner::UvTaskRunner(uv_loop_t* loop) : loop_{loop} {}
  11. UvTaskRunner::~UvTaskRunner() = default;
  12. bool UvTaskRunner::PostDelayedTask(const base::Location& from_here,
  13. base::OnceClosure task,
  14. base::TimeDelta delay) {
  15. auto on_timeout = [](uv_timer_t* timer) {
  16. auto& tasks = static_cast<UvTaskRunner*>(timer->data)->tasks_;
  17. if (auto iter = tasks.find(timer); iter != tasks.end())
  18. std::move(tasks.extract(iter).mapped()).Run();
  19. };
  20. auto timer = UvHandle<uv_timer_t>{};
  21. timer->data = this;
  22. uv_timer_init(loop_, timer.get());
  23. uv_timer_start(timer.get(), on_timeout, delay.InMilliseconds(), 0);
  24. tasks_.insert_or_assign(std::move(timer), std::move(task));
  25. return true;
  26. }
  27. bool UvTaskRunner::RunsTasksInCurrentSequence() const {
  28. return true;
  29. }
  30. bool UvTaskRunner::PostNonNestableDelayedTask(const base::Location& from_here,
  31. base::OnceClosure task,
  32. base::TimeDelta delay) {
  33. return PostDelayedTask(from_here, std::move(task), delay);
  34. }
  35. } // namespace electron