Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

perf: the running rest is saved when it changes, not on every second of its countdown

Duarte Santosa2aa6abdca61
diff --git a/frontend/src/store/useUI.js b/frontend/src/store/useUI.js
index c16e82b6e..812a92072 100644
--- a/frontend/src/store/useUI.js
+++ b/frontend/src/store/useUI.js
@@ -464,12 +464,20 @@ export const useUI = create((set, get) => ({
// A rest that ended meanwhile, or one left with no session running, is dropped.
export const REST_KEY = 'gym_rest'
const restStore = () => { try { return typeof localStorage === 'undefined' ? null : localStorage } catch { return null } }
+// Written when the rest itself changes, not on each second's tick: a running rest comes back from
+// its `endsAt`, and its `left` (kept in the copy for older builds) is read only while paused. The
+// bar's tick used to rewrite the key every second of every rest.
+let savedRestKey = null
const saveRest = tm => {
const ss = restStore()
if (!ss) return
try {
- if (!tm || tm.ready) ss.removeItem(REST_KEY)
- else ss.setItem(REST_KEY, JSON.stringify({ endsAt: tm.endsAt, total: tm.total, forIdx: tm.forIdx ?? null, forSet: tm.forSet ?? null, kind: tm.kind || null, paused: !!tm.paused, left: tm.left }))
+ if (!tm || tm.ready) { savedRestKey = null; ss.removeItem(REST_KEY); return }
+ const v = { endsAt: tm.endsAt, total: tm.total, forIdx: tm.forIdx ?? null, forSet: tm.forSet ?? null, kind: tm.kind || null, paused: !!tm.paused, left: tm.left }
+ const key = JSON.stringify({ ...v, left: v.paused ? v.left : null })
+ if (key === savedRestKey && ss.getItem(REST_KEY) !== null) return
+ ss.setItem(REST_KEY, JSON.stringify(v))
+ savedRestKey = key
} catch { /* the rest just does not outlive a reload */ }
}
export function restoreRest(now = Date.now()) {
diff --git a/frontend/src/store/useUI.rest-reload.test.js b/frontend/src/store/useUI.rest-reload.test.js
index 6cacd1fdd..fe34e14d6 100644
--- a/frontend/src/store/useUI.rest-reload.test.js
+++ b/frontend/src/store/useUI.rest-reload.test.js
@@ -40,6 +40,29 @@ describe('the rest timer across a reload', () => {
expect(saved()).toBeNull()
})
+ // Battery round 2: the bar's tick used to rewrite the key every second of every rest.
+ it('writes the rest when it changes, not on every second of the countdown', () => {
+ const setItem = vi.spyOn(localStorage, 'setItem')
+ const restWrites = () => setItem.mock.calls.filter(([k]) => k === REST_KEY).length
+ useUI.getState().startRest(90, 2)
+ expect(restWrites()).toBe(1)
+ vi.advanceTimersByTime(10_000)
+ expect(useUI.getState().timer.left).toBeLessThan(90)
+ expect(restWrites()).toBe(1)
+ // +15 s moves the end: written; pause holds `left`: written, and a reload restores it
+ useUI.getState().addRest(15)
+ const afterAdd = restWrites()
+ useUI.getState().pauseRest()
+ expect(restWrites()).toBe(afterAdd + 1)
+ expect(saved()).toMatchObject({ paused: true, left: useUI.getState().timer.left })
+ // the key taken away under it (cleared storage): the next change writes it again
+ useUI.getState().resumeRest()
+ localStorage.removeItem(REST_KEY)
+ vi.advanceTimersByTime(1000)
+ expect(saved()).toMatchObject({ paused: false, endsAt: useUI.getState().timer.endsAt })
+ setItem.mockRestore()
+ })
+
it('comes back at boot with the time left, books nothing new, and chimes once at the end', () => {
const endsAt = Date.now() + 40_000
localStorage.setItem(REST_KEY, JSON.stringify({ endsAt, total: 90, forIdx: 1, kind: null, paused: false, left: 50 }))