Flush pending state save immediately when tab backgrounds/closes
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.
This commit is contained in:
1 parent
5a7ce13ca2
commit
10fa150d09
2 files changed
+11
No files matched your search
@@ -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, {
|
||||
|
||||
@@ -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 } })(),
|
||||
|
||||
Reference in new issue
Block a user