Extract Periodic class from connection's Worker

This commit is contained in:
Camden Dixie O'Brien 2025-03-02 14:03:33 +00:00
parent 76aca12fec
commit 94df48db7b
3 changed files with 33 additions and 16 deletions

View File

@ -84,29 +84,18 @@ namespace StudySystemClient {
} }
} }
private class Worker { private class Worker : Periodic {
private uint TASK_PERIOD_MS = 10; private const uint TASK_PERIOD_MS = 10;
private SessionManager session_manager; private SessionManager session_manager;
private bool exit;
private Thread<void> thread;
public Worker(SessionManager session_manager) { public Worker(SessionManager session_manager) {
base(TASK_PERIOD_MS);
this.session_manager = session_manager; this.session_manager = session_manager;
exit = false;
thread = new Thread<void>("connection_worker", body);
} }
~Worker() { protected override void task() {
exit = true; session_manager.task();
thread.join();
}
private void body() {
while (!exit) {
session_manager.task();
Thread.usleep(1000 * TASK_PERIOD_MS);
}
} }
} }
} }

View File

@ -17,6 +17,7 @@ lib = library(
'connection.vala', 'connection.vala',
'der.vala', 'der.vala',
'main_window.vala', 'main_window.vala',
'periodic.vala',
'request.vala', 'request.vala',
'response.vala', 'response.vala',
'session_manager.vala', 'session_manager.vala',

27
client/src/periodic.vala Normal file
View File

@ -0,0 +1,27 @@
namespace StudySystemClient {
public abstract class Periodic {
private uint period_ms;
private bool exit;
private Thread<void> thread;
protected Periodic(uint period_ms) {
this.period_ms = period_ms;
exit = false;
thread = new Thread<void>("Periodic task", body);
}
~Periodic() {
exit = true;
thread.join();
}
protected abstract void task();
private void body() {
while (!exit) {
task();
Thread.usleep(1000 * period_ms);
}
}
}
}