15 : Interval_ { interval }
17 LastInvocation_.start ();
19 Timer_.setTimerType (type);
20 Timer_.setSingleShot (
true);
21 Timer_.callOnTimeout ([
this]
23 LastInvocation_.restart ();
25 if (Queue_.size () > 1)
26 StartTimer (Interval_);
27 if (!Queue_.isEmpty ())
28 Queue_.takeFirst () ();
42 Throttle::Awaiter::Awaiter (
Throttle& throttle)
43 : Throttle_ { throttle }
47 Throttle::Awaiter::Awaiter (Awaiter&& other) noexcept
48 : Throttle_ { other.Throttle_ }
49 , Handle_ { std::exchange (other.Handle_, {}) }
53 Throttle::Awaiter::~Awaiter ()
56 Throttle_.Queue_.removeOne (Handle_);
59 bool Throttle::Awaiter::await_ready ()
const
61 const bool allowed = std::chrono::milliseconds { Throttle_.LastInvocation_.elapsed () } >= Throttle_.Interval_ && Throttle_.Queue_.isEmpty ();
63 Throttle_.LastInvocation_.restart ();
67 void Throttle::Awaiter::await_suspend (std::coroutine_handle<> handle)
69 if (Throttle_.Queue_.isEmpty ())
70 Throttle_.StartTimer (Throttle_.Interval_ - std::chrono::milliseconds { Throttle_.LastInvocation_.elapsed () });
72 Throttle_.Queue_ << handle;
76 void Throttle::Awaiter::await_resume ()
81 Throttle::Awaiter Throttle::operator
co_await ()
83 return Awaiter { *
this };
86 void Throttle::StartTimer (std::chrono::milliseconds timeout)
88 BackoffFactor_ = std::max (0, BackoffFactor_ - 1);
89 Timer_.start (timeout * (BackoffFactor_ + 1));
std::chrono::milliseconds GetInterval() const
Throttle(std::chrono::milliseconds, Qt::TimerType=Qt::TimerType::CoarseTimer)