Browse Source

Add auto refresh every minute

master
Kenneth Bruen 9 months ago
parent
commit
276f51fc5f
Signed by: kbruen
GPG Key ID: C1980A470C3EE5B1
  1. 6
      src/departure.zig
  2. 2
      src/state.zig

6
src/departure.zig

@ -15,7 +15,10 @@ fn fetchThread(state: *AppState) !void {
defer curl.deinit(); defer curl.deinit();
while (state.departure_screen_state.fetch_thread != null) { while (state.departure_screen_state.fetch_thread != null) {
const fetch_anyway = state.departure_screen_state.should_refresh; var fetch_anyway = state.departure_screen_state.should_refresh;
if (state.departure_screen_state.last_refresh_time + 60000 < std.time.milliTimestamp()) {
fetch_anyway = true;
}
if (!fetch_anyway and std.mem.eql(u8, station_id_buf.slice(), state.departure_screen_state.station_id.items) and include_tram == state.departure_screen_state.include_tram) { if (!fetch_anyway and std.mem.eql(u8, station_id_buf.slice(), state.departure_screen_state.station_id.items) and include_tram == state.departure_screen_state.include_tram) {
std.time.sleep(100 * 1000); std.time.sleep(100 * 1000);
continue; continue;
@ -64,6 +67,7 @@ fn fetchThread(state: *AppState) !void {
} }
state.departure_screen_state.fetch_result = parsed; state.departure_screen_state.fetch_result = parsed;
state.departure_screen_state.should_refresh = false; state.departure_screen_state.should_refresh = false;
state.departure_screen_state.last_refresh_time = std.time.milliTimestamp();
} }
if (state.departure_screen_state.fetch_result) |old_result| { if (state.departure_screen_state.fetch_result) |old_result| {
old_result.deinit(); old_result.deinit();

2
src/state.zig

@ -29,7 +29,7 @@ pub const DepartureScreenState = struct {
platform: std.ArrayListUnmanaged(u8), platform: std.ArrayListUnmanaged(u8),
departure_date: std.time.Instant, departure_date: std.time.Instant,
fetch_thread: ?std.Thread = null, fetch_thread: ?std.Thread = null,
last_refresh_time: std.time.Instant = std.mem.zeroInit(std.time.Instant, .{}), last_refresh_time: i64 = 0,
fetch_result: ?std.json.Parsed(std.json.Value) = null, fetch_result: ?std.json.Parsed(std.json.Value) = null,
should_refresh: bool = false, should_refresh: bool = false,
max_next_trains: c_int = 5, max_next_trains: c_int = 5,

Loading…
Cancel
Save