From 276f51fc5f4775d4df6b8690d343474c78ff3d0c Mon Sep 17 00:00:00 2001 From: Dan Cojocaru Date: Sat, 17 Feb 2024 12:00:58 +0100 Subject: [PATCH] Add auto refresh every minute --- src/departure.zig | 6 +++++- src/state.zig | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/departure.zig b/src/departure.zig index 1a38561..d2bb061 100644 --- a/src/departure.zig +++ b/src/departure.zig @@ -15,7 +15,10 @@ fn fetchThread(state: *AppState) !void { defer curl.deinit(); 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) { std.time.sleep(100 * 1000); continue; @@ -64,6 +67,7 @@ fn fetchThread(state: *AppState) !void { } state.departure_screen_state.fetch_result = parsed; 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| { old_result.deinit(); diff --git a/src/state.zig b/src/state.zig index cbb542f..5be816d 100644 --- a/src/state.zig +++ b/src/state.zig @@ -29,7 +29,7 @@ pub const DepartureScreenState = struct { platform: std.ArrayListUnmanaged(u8), departure_date: std.time.Instant, 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, should_refresh: bool = false, max_next_trains: c_int = 5,