2017-04-07 12:10:10 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-04-07 12:10:10 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-04-07 12:10:10 +00:00
|
|
|
*/
|
|
|
|
#include "base/timer.h"
|
|
|
|
|
|
|
|
namespace base {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
QObject *TimersAdjuster() {
|
|
|
|
static QObject adjuster;
|
|
|
|
return &adjuster;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-04-24 19:09:20 +00:00
|
|
|
Timer::Timer(
|
|
|
|
not_null<QThread*> thread,
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void()> callback)
|
2018-04-24 19:09:20 +00:00
|
|
|
: Timer(std::move(callback)) {
|
|
|
|
moveToThread(thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
Timer::Timer(Fn<void()> callback)
|
2018-04-24 19:09:20 +00:00
|
|
|
: QObject(nullptr)
|
2017-04-07 12:10:10 +00:00
|
|
|
, _callback(std::move(callback))
|
|
|
|
, _type(Qt::PreciseTimer)
|
2017-04-08 11:25:54 +00:00
|
|
|
, _adjusted(false) {
|
|
|
|
setRepeat(Repeat::Interval);
|
2018-04-24 19:09:20 +00:00
|
|
|
connect(
|
|
|
|
TimersAdjuster(),
|
|
|
|
&QObject::destroyed,
|
|
|
|
this,
|
|
|
|
[this] { adjust(); },
|
|
|
|
Qt::QueuedConnection);
|
2017-04-07 12:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::start(TimeMs timeout, Qt::TimerType type, Repeat repeat) {
|
|
|
|
cancel();
|
|
|
|
|
|
|
|
_type = type;
|
2017-04-08 11:25:54 +00:00
|
|
|
setRepeat(repeat);
|
2017-04-07 12:10:10 +00:00
|
|
|
_adjusted = false;
|
|
|
|
setTimeout(timeout);
|
|
|
|
_timerId = startTimer(_timeout, _type);
|
|
|
|
if (_timerId) {
|
|
|
|
_next = getms(true) + _timeout;
|
|
|
|
} else {
|
|
|
|
_next = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::cancel() {
|
|
|
|
if (isActive()) {
|
|
|
|
killTimer(base::take(_timerId));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeMs Timer::remainingTime() const {
|
|
|
|
if (!isActive()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
auto now = getms(true);
|
|
|
|
return (_next > now) ? (_next - now) : TimeMs(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::Adjust() {
|
|
|
|
QObject emitter;
|
2018-04-24 19:09:20 +00:00
|
|
|
connect(
|
|
|
|
&emitter,
|
|
|
|
&QObject::destroyed,
|
|
|
|
TimersAdjuster(),
|
|
|
|
&QObject::destroyed);
|
2017-04-07 12:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::adjust() {
|
|
|
|
auto remaining = remainingTime();
|
|
|
|
if (remaining >= 0) {
|
|
|
|
cancel();
|
|
|
|
_timerId = startTimer(remaining, _type);
|
|
|
|
_adjusted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::setTimeout(TimeMs timeout) {
|
|
|
|
Expects(timeout >= 0 && timeout <= std::numeric_limits<int>::max());
|
2018-04-24 19:09:20 +00:00
|
|
|
|
2017-04-07 12:10:10 +00:00
|
|
|
_timeout = static_cast<unsigned int>(timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Timer::timeout() const {
|
|
|
|
return _timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::timerEvent(QTimerEvent *e) {
|
2017-04-08 11:25:54 +00:00
|
|
|
if (repeat() == Repeat::Interval) {
|
2017-04-07 12:10:10 +00:00
|
|
|
if (_adjusted) {
|
2017-04-08 11:25:54 +00:00
|
|
|
start(_timeout, _type, repeat());
|
2017-04-07 12:10:10 +00:00
|
|
|
} else {
|
|
|
|
_next = getms(true) + _timeout;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_callback) {
|
|
|
|
_callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:09:20 +00:00
|
|
|
int DelayedCallTimer::call(
|
|
|
|
TimeMs timeout,
|
2018-06-04 15:35:11 +00:00
|
|
|
FnMut<void()> callback,
|
2018-04-24 19:09:20 +00:00
|
|
|
Qt::TimerType type) {
|
2017-04-07 12:10:10 +00:00
|
|
|
Expects(timeout >= 0);
|
2018-04-24 19:09:20 +00:00
|
|
|
|
2017-04-07 12:10:10 +00:00
|
|
|
if (!callback) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
auto timerId = startTimer(static_cast<int>(timeout), type);
|
|
|
|
if (timerId) {
|
|
|
|
_callbacks.emplace(timerId, std::move(callback));
|
|
|
|
}
|
|
|
|
return timerId;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DelayedCallTimer::cancel(int callId) {
|
|
|
|
if (callId) {
|
|
|
|
killTimer(callId);
|
2018-04-24 19:09:20 +00:00
|
|
|
_callbacks.remove(callId);
|
2017-04-07 12:10:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DelayedCallTimer::timerEvent(QTimerEvent *e) {
|
|
|
|
auto timerId = e->timerId();
|
|
|
|
killTimer(timerId);
|
|
|
|
|
|
|
|
auto it = _callbacks.find(timerId);
|
|
|
|
if (it != _callbacks.end()) {
|
|
|
|
auto callback = std::move(it->second);
|
|
|
|
_callbacks.erase(it);
|
|
|
|
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace base
|