Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AbstractTimer: Fix double count up/down #246

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/Services/AbstractTimer.vala
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ public abstract class AbstractTimer : Object {
return time_usec > 0;
}
}

/**
* Function executed when ${@link AbstractTimer.to_string} is called.
*/
public ToStringFunc? to_string_func = null;

private const uint INTERVAL_MSEC = 1000;
private const uint TIMER_NOT_STARTED = -1;

// The time this timer holds
protected TimeSpan time_usec;
private uint timeout;
private bool timeout_remove_flag;
private uint timeout = TIMER_NOT_STARTED;

protected AbstractTimer () {
}
Expand All @@ -49,19 +50,24 @@ public abstract class AbstractTimer : Object {
*/
public void start () {
// Already started
if (timeout_remove_flag == Source.CONTINUE) {
if (timeout != TIMER_NOT_STARTED) {
return;
}

timeout_remove_flag = Source.CONTINUE;
timeout = Timeout.add (INTERVAL_MSEC, on_timeout_cb);
}

/**
* Stop the timer.
*/
public void stop () {
timeout_remove_flag = Source.REMOVE;
// Not started
if (timeout == TIMER_NOT_STARTED) {
return;
}

Source.remove (timeout);
timeout = TIMER_NOT_STARTED;
}

/**
Expand All @@ -75,11 +81,7 @@ public abstract class AbstractTimer : Object {
}

private bool on_timeout_cb () {
if (timeout_remove_flag == Source.REMOVE) {
return timeout_remove_flag;
}

timeout_remove_flag = on_timeout ();
bool timeout_remove_flag = on_timeout ();

ticked ();
return timeout_remove_flag;
Expand Down