fix(timer): detect remote pause via sentinel value in activity file
When the inactivity watchdog pauses the timer, write `"0"` to the shared activity file as a sentinel. Other instances read this in `on_activity()` and update their local `state.running` accordingly, allowing them to resume the timer without waiting for the pausing instance.
This commit is contained in:
@@ -48,6 +48,21 @@ local function touch_activity_file()
|
||||
end
|
||||
end
|
||||
|
||||
--- Write 0 to the shared file to signal that the timer has been paused.
|
||||
--- Other instances will detect this in on_activity() and resume accordingly.
|
||||
local function mark_timer_paused()
|
||||
local path = activity_file_path()
|
||||
if not path then
|
||||
return
|
||||
end
|
||||
local f = io.open(path, "w")
|
||||
if f then
|
||||
f:write("0")
|
||||
f:close()
|
||||
-- Do NOT update last_activity_time: cleanup logic must keep working correctly
|
||||
end
|
||||
end
|
||||
|
||||
--- Return true if another instance has recorded activity within `inactivity_ms`.
|
||||
local function other_instance_active()
|
||||
local path = activity_file_path()
|
||||
@@ -127,6 +142,7 @@ local function arm_inactivity_watchdog()
|
||||
local ok, err = api.stop_timer(state.token, state.timer_id)
|
||||
if ok then
|
||||
state.running = false
|
||||
mark_timer_paused() -- signal to other instances that the timer is now paused
|
||||
notify("Timer paused (inactivity)")
|
||||
else
|
||||
notify("Could not pause timer: " .. (err or ""), vim.log.levels.WARN)
|
||||
@@ -181,6 +197,24 @@ function M.on_activity()
|
||||
if not state.timer_id then
|
||||
return
|
||||
end
|
||||
|
||||
-- Sync local running state with the shared file.
|
||||
-- If another instance paused the timer it writes "0" as a sentinel.
|
||||
-- Detect it here so we can resume without waiting for the other instance.
|
||||
if state.running then
|
||||
local path = activity_file_path()
|
||||
if path then
|
||||
local f = io.open(path, "r")
|
||||
if f then
|
||||
local raw = f:read("*l")
|
||||
f:close()
|
||||
if tonumber(raw) == 0 then
|
||||
state.running = false -- remote pause detected; will resume below
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
touch_activity_file()
|
||||
|
||||
if not state.running then
|
||||
|
||||
Reference in New Issue
Block a user