From 10fa150d091e9fb4bc3abcae1c7d4f0ac0dcbe55 Mon Sep 17 00:00:00 2001 From: Duarte Santos Date: Tue, 21 Jul 2026 12:00:49 +0200 Subject: [PATCH] Flush pending state save immediately when tab backgrounds/closes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Settings changes (like the reminder time) were saved via a 1.5s debounce; closing or switching away from the tab right after an edit could kill that pending save before it reached the server, silently losing the change. Now flushed on visibilitychange instead of relying on the timer alone. Also confirmed via a live server-side test (bypassing the client entirely) that the reminder loop itself fires correctly — trimmed the verbose per-tick debug log added for that test, kept the one for an actual match. --- api/server.js | 1 + frontend/src/store/useStore.js | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/api/server.js b/api/server.js index da11acf..d8f2d6c 100644 --- a/api/server.js +++ b/api/server.js @@ -116,6 +116,7 @@ setInterval(() => { const rid = effectiveRoutineId(S, now.date); if (!rid) continue; // rest day — nothing planned const routine = (S.routines || []).find(r => r.id === rid); + console.log('reminder firing', user.id, rid); user.lastReminder = now.date; saveDb(); sendPush(user.id, { diff --git a/frontend/src/store/useStore.js b/frontend/src/store/useStore.js index 02b364a..490004f 100644 --- a/frontend/src/store/useStore.js +++ b/frontend/src/store/useStore.js @@ -35,6 +35,16 @@ export const useStore = create((set, get) => { } } + // A setting changed right before switching away/closing the tab must not get lost mid-debounce + // (e.g. setting the reminder time then immediately backgrounding to test it). + document.addEventListener('visibilitychange', () => { + if (document.visibilityState === 'hidden' && pushTm) { + clearTimeout(pushTm) + pushTm = null + get().pushState() + } + }) + return { S: loadState(), user: (() => { try { return JSON.parse(localStorage.getItem('gym_user')) || null } catch { return null } })(),