call_delayed.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #include "base/call_delayed.h"
  8. #include "base/timer.h"
  9. #include <QtCore/QCoreApplication>
  10. namespace base {
  11. namespace {
  12. DelayedCallTimer *GlobalTimer = nullptr;
  13. bool Finished = false;
  14. void CreateGlobalTimer() {
  15. Expects(QCoreApplication::instance() != nullptr);
  16. Expects(!GlobalTimer);
  17. const auto instance = QCoreApplication::instance();
  18. Assert(instance != nullptr);
  19. GlobalTimer = new DelayedCallTimer();
  20. instance->connect(instance, &QCoreApplication::aboutToQuit, [] {
  21. Finished = true;
  22. });
  23. instance->connect(instance, &QCoreApplication::destroyed, [] {
  24. Finished = true;
  25. delete GlobalTimer;
  26. GlobalTimer = nullptr;
  27. });
  28. }
  29. } // namespace
  30. void call_delayed(crl::time delay, FnMut<void()> &&callable) {
  31. if (Finished) {
  32. return;
  33. }
  34. if (!GlobalTimer) {
  35. CreateGlobalTimer();
  36. }
  37. GlobalTimer->call(delay, std::move(callable));
  38. }
  39. } // namespace base