fix(mpd): use timers with second granularity where possible
Reuse already armed timer in Disconnected state.
This commit is contained in:
parent
653c24cee1
commit
bb60d41842
|
@ -148,6 +148,7 @@ class Stopped : public State {
|
|||
class Disconnected : public State {
|
||||
Context* const ctx_;
|
||||
sigc::connection timer_connection_;
|
||||
int last_interval_;
|
||||
|
||||
public:
|
||||
Disconnected(Context* const ctx) : ctx_{ctx} {}
|
||||
|
@ -162,7 +163,7 @@ class Disconnected : public State {
|
|||
Disconnected(Disconnected const&) = delete;
|
||||
Disconnected& operator=(Disconnected const&) = delete;
|
||||
|
||||
void arm_timer(int interval) noexcept;
|
||||
bool arm_timer(int interval) noexcept;
|
||||
void disarm_timer() noexcept;
|
||||
|
||||
bool on_timer();
|
||||
|
|
|
@ -53,10 +53,10 @@ auto waybar::modules::MPD::update() -> void {
|
|||
|
||||
void waybar::modules::MPD::queryMPD() {
|
||||
if (connection_ != nullptr) {
|
||||
spdlog::debug("{}: fetching state information", module_name_);
|
||||
spdlog::trace("{}: fetching state information", module_name_);
|
||||
try {
|
||||
fetchState();
|
||||
spdlog::debug("{}: fetch complete", module_name_);
|
||||
spdlog::trace("{}: fetch complete", module_name_);
|
||||
} catch (std::exception const& e) {
|
||||
spdlog::error("{}: {}", module_name_, e.what());
|
||||
state_ = MPD_STATE_UNKNOWN;
|
||||
|
|
|
@ -119,7 +119,7 @@ bool Idle::on_io(Glib::IOCondition const&) {
|
|||
|
||||
void Playing::entry() noexcept {
|
||||
sigc::slot<bool> timer_slot = sigc::mem_fun(*this, &Playing::on_timer);
|
||||
timer_connection_ = Glib::signal_timeout().connect(timer_slot, /* milliseconds */ 1'000);
|
||||
timer_connection_ = Glib::signal_timeout().connect_seconds(timer_slot, 1);
|
||||
spdlog::debug("mpd: Playing: enabled 1 second periodic timer.");
|
||||
}
|
||||
|
||||
|
@ -327,14 +327,20 @@ void Stopped::pause() {
|
|||
|
||||
void Stopped::update() noexcept { ctx_->do_update(); }
|
||||
|
||||
void Disconnected::arm_timer(int interval) noexcept {
|
||||
bool Disconnected::arm_timer(int interval) noexcept {
|
||||
// check if it's necessary to modify the timer
|
||||
if (timer_connection_ && last_interval_ == interval) {
|
||||
return true;
|
||||
}
|
||||
// unregister timer, if present
|
||||
disarm_timer();
|
||||
|
||||
// register timer
|
||||
last_interval_ = interval;
|
||||
sigc::slot<bool> timer_slot = sigc::mem_fun(*this, &Disconnected::on_timer);
|
||||
timer_connection_ = Glib::signal_timeout().connect(timer_slot, interval);
|
||||
spdlog::debug("mpd: Disconnected: enabled interval timer.");
|
||||
timer_connection_ = Glib::signal_timeout().connect_seconds(timer_slot, interval);
|
||||
spdlog::debug("mpd: Disconnected: enabled {}s interval timer.", interval);
|
||||
return false;
|
||||
}
|
||||
|
||||
void Disconnected::disarm_timer() noexcept {
|
||||
|
@ -347,7 +353,7 @@ void Disconnected::disarm_timer() noexcept {
|
|||
|
||||
void Disconnected::entry() noexcept {
|
||||
ctx_->emit();
|
||||
arm_timer(1'000);
|
||||
arm_timer(1 /* second */);
|
||||
}
|
||||
|
||||
void Disconnected::exit() noexcept { disarm_timer(); }
|
||||
|
@ -376,9 +382,7 @@ bool Disconnected::on_timer() {
|
|||
spdlog::warn("mpd: Disconnected: error: {}", e.what());
|
||||
}
|
||||
|
||||
arm_timer(ctx_->interval() * 1'000);
|
||||
|
||||
return false;
|
||||
return arm_timer(ctx_->interval());
|
||||
}
|
||||
|
||||
void Disconnected::update() noexcept { ctx_->do_update(); }
|
||||
|
|
Loading…
Reference in New Issue