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.

Rest timer survives the app being killed mid-rest

Duarte Santos4ca7e1ab6ec1
diff --git a/frontend/src/store/useUI.js b/frontend/src/store/useUI.js
index f303ff252..db5446975 100644
--- a/frontend/src/store/useUI.js
+++ b/frontend/src/store/useUI.js
@@ -352,14 +352,17 @@ export const useUI = create((set, get) => ({
}))
// A reload in the middle of a rest (pull to refresh, a WebView restart, an update) used to drop
-// the countdown while its alarm still rang later. The rest lives on in sessionStorage for this
-// tab and comes back at boot while its end is still ahead, held if it was paused. Its end was
-// booked when it started (the server's push, the Android alarm), and that booking survives the
-// reload, so it is not booked again: the alert fires once, and the bar's own tick at zero calls
-// the push off as it always does. A rest that ended meanwhile, or one left with no session
-// running, is dropped.
+// the countdown while its alarm still rang later. The rest lives on in localStorage and comes
+// back at boot while its end is still ahead, held if it was paused. Not sessionStorage: that
+// dies with the process, and Android killing the app mid-rest (or iOS dropping the home-screen
+// app) took the bar with it. On the web its end was booked when it started (the server's push,
+// per device, which survives the reload), so it is not booked again: the alert fires once, and
+// the bar's own tick at zero calls the push off as it always does. In the app the countdown
+// notification lives in the app's process and may be gone with it, so the end is booked again
+// there: the alarm has a fixed id and the new booking replaces the old one, it never adds a second.
+// 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 sessionStorage === 'undefined' ? null : sessionStorage } catch { return null } }
+const restStore = () => { try { return typeof localStorage === 'undefined' ? null : localStorage } catch { return null } }
const saveRest = tm => {
const ss = restStore()
if (!ss) return
@@ -379,10 +382,12 @@ export function restoreRest(now = Date.now()) {
const base = { total, forIdx: saved.forIdx ?? undefined, ...(saved.kind === 'switch' ? { kind: 'switch' } : {}) }
if (saved.paused) {
useUI.setState({ timer: { ...base, left: Math.round(saved.left), endsAt: saved.endsAt, paused: true } })
+ if (MOBILE) holdRestAlert(Math.round(saved.left), total)
return true
}
pageHiddenAt = typeof document !== 'undefined' && document.hidden ? now : null
useUI.setState({ timer: { ...base, left: Math.max(1, Math.round((saved.endsAt - now) / 1000)), endsAt: saved.endsAt } })
+ if (MOBILE) bookRestEnd(saved.endsAt, total, base.kind)
runRest(useUI.setState, useUI.getState)
return true
}
diff --git a/frontend/src/store/useUI.rest-kill.test.js b/frontend/src/store/useUI.rest-kill.test.js
new file mode 100644
index 000000000..58d81c962
--- /dev/null
+++ b/frontend/src/store/useUI.rest-kill.test.js
@@ -0,0 +1,60 @@
+// @vitest-environment happy-dom
+// Android kills the app mid-rest (kill -9, low memory): sessionStorage dies with the process and
+// so does the countdown notification, which lives in it. The rest is kept in localStorage, and
+// coming back the app books its end again: one alarm with a fixed id, so it replaces the old
+// one rather than adding a second. A paused rest is held again in the notification.
+import { describe, expect, it, beforeEach, afterEach, vi } from 'vitest'
+
+vi.mock('../lib/mobile.js', async importOriginal => ({ ...(await importOriginal()), MOBILE: true }))
+vi.mock('../lib/rest-alert.js', () => ({
+ armRestAlert: vi.fn(() => Promise.resolve(true)),
+ holdRestAlert: vi.fn(),
+ disarmRestAlert: vi.fn(),
+ bindNativeRest: vi.fn(),
+}))
+vi.mock('../lib/sound.js', () => ({ beep: vi.fn(), chime: vi.fn(), vibrate: vi.fn(), alertBuzz: vi.fn() }))
+vi.mock('../lib/api.js', () => ({ api: vi.fn(() => Promise.resolve({ ok: true })) }))
+
+import { useUI, restoreRest, REST_KEY } from './useUI.js'
+import { useStore } from './useStore.js'
+import { armRestAlert, holdRestAlert } from '../lib/rest-alert.js'
+
+let original
+beforeEach(() => {
+ vi.useFakeTimers()
+ original = useStore.getState().S
+ useStore.setState({ S: { ...original, active: { id: 'a', entries: [] } } })
+ useUI.setState({ timer: null, work: null })
+ localStorage.clear(); sessionStorage.clear()
+ armRestAlert.mockClear(); holdRestAlert.mockClear()
+})
+afterEach(() => {
+ useUI.getState().stopRest()
+ useStore.setState({ S: original })
+ vi.useRealTimers()
+})
+
+describe('the rest timer after the app process was killed', () => {
+ it('is kept where a new process can read it', () => {
+ useUI.getState().startRest(90, 0)
+ expect(JSON.parse(localStorage.getItem(REST_KEY))).toMatchObject({ total: 90, forIdx: 0 })
+ expect(sessionStorage.getItem(REST_KEY)).toBeNull()
+ })
+
+ it('comes back with its end booked again for the same moment', () => {
+ const endsAt = Date.now() + 60_000
+ localStorage.setItem(REST_KEY, JSON.stringify({ endsAt, total: 90, forIdx: 1, kind: null, paused: false, left: 60 }))
+ expect(restoreRest()).toBe(true)
+ expect(useUI.getState().timer).toMatchObject({ left: 60, total: 90, endsAt })
+ expect(armRestAlert).toHaveBeenCalledTimes(1)
+ expect(armRestAlert.mock.calls[0][0]).toBe(endsAt)
+ expect(armRestAlert.mock.calls[0][1]).toMatchObject({ totalSec: 90 })
+ })
+
+ it('a paused one is held again in the notification', () => {
+ localStorage.setItem(REST_KEY, JSON.stringify({ endsAt: 0, total: 90, forIdx: 0, kind: null, paused: true, left: 33 }))
+ expect(restoreRest()).toBe(true)
+ expect(holdRestAlert).toHaveBeenCalledWith(33, 90)
+ expect(armRestAlert).not.toHaveBeenCalled()
+ })
+})
diff --git a/frontend/src/store/useUI.rest-reload.test.js b/frontend/src/store/useUI.rest-reload.test.js
index 2dacde94a..6cacd1fdd 100644
--- a/frontend/src/store/useUI.rest-reload.test.js
+++ b/frontend/src/store/useUI.rest-reload.test.js
@@ -1,5 +1,5 @@
// @vitest-environment happy-dom
-// A reload mid-rest keeps the countdown (v1.3.11): the rest is kept in sessionStorage and comes
+// A reload mid-rest keeps the countdown (v1.3.11): the rest is kept in localStorage (not sessionStorage, which dies with the app process) and comes
// back at boot while its end is ahead, without booking its end a second time.
import { describe, expect, it, beforeEach, afterEach, vi } from 'vitest'
@@ -11,7 +11,7 @@ import { api } from '../lib/api.js'
import { useUI, restoreRest, REST_KEY } from './useUI.js'
import { useStore } from './useStore.js'
-const saved = () => JSON.parse(sessionStorage.getItem(REST_KEY) || 'null')
+const saved = () => JSON.parse(localStorage.getItem(REST_KEY) || 'null')
const pushes = () => api.mock.calls.filter(([p]) => p === '/api/push/rest-timer')
describe('the rest timer across a reload', () => {
@@ -21,7 +21,7 @@ describe('the rest timer across a reload', () => {
original = { S: useStore.getState().S, user: useStore.getState().user }
useStore.setState({ S: { ...original.S, sound: true, active: { id: 'a', entries: [] } }, user: { id: 'u1' } })
useUI.setState({ timer: null, work: null, toastMsg: '' })
- sessionStorage.clear()
+ localStorage.clear()
api.mockClear(); chime.mockClear()
})
afterEach(() => {
@@ -42,7 +42,7 @@ describe('the rest timer across a reload', () => {
it('comes back at boot with the time left, books nothing new, and chimes once at the end', () => {
const endsAt = Date.now() + 40_000
- sessionStorage.setItem(REST_KEY, JSON.stringify({ endsAt, total: 90, forIdx: 1, kind: null, paused: false, left: 50 }))
+ localStorage.setItem(REST_KEY, JSON.stringify({ endsAt, total: 90, forIdx: 1, kind: null, paused: false, left: 50 }))
expect(restoreRest()).toBe(true)
expect(useUI.getState().timer).toMatchObject({ left: 40, total: 90, endsAt, forIdx: 1 })
expect(pushes()).toEqual([])
@@ -53,7 +53,7 @@ describe('the rest timer across a reload', () => {
})
it('a paused rest comes back held', () => {
- sessionStorage.setItem(REST_KEY, JSON.stringify({ endsAt: Date.now() - 5000, total: 90, forIdx: 0, kind: null, paused: true, left: 33 }))
+ localStorage.setItem(REST_KEY, JSON.stringify({ endsAt: Date.now() - 5000, total: 90, forIdx: 0, kind: null, paused: true, left: 33 }))
expect(restoreRest()).toBe(true)
expect(useUI.getState().timer).toMatchObject({ left: 33, total: 90, paused: true })
vi.advanceTimersByTime(60_000)
@@ -61,12 +61,12 @@ describe('the rest timer across a reload', () => {
})
it('a rest that ended meanwhile, or one with no session running, is dropped', () => {
- sessionStorage.setItem(REST_KEY, JSON.stringify({ endsAt: Date.now() - 1, total: 90, forIdx: 0, paused: false, left: 3 }))
+ localStorage.setItem(REST_KEY, JSON.stringify({ endsAt: Date.now() - 1, total: 90, forIdx: 0, paused: false, left: 3 }))
expect(restoreRest()).toBe(false)
expect(useUI.getState().timer).toBeNull()
expect(saved()).toBeNull()
useStore.setState({ S: { ...useStore.getState().S, active: null } })
- sessionStorage.setItem(REST_KEY, JSON.stringify({ endsAt: Date.now() + 30_000, total: 90, forIdx: 0, paused: false, left: 30 }))
+ localStorage.setItem(REST_KEY, JSON.stringify({ endsAt: Date.now() + 30_000, total: 90, forIdx: 0, paused: false, left: 30 }))
expect(restoreRest()).toBe(false)
expect(useUI.getState().timer).toBeNull()
})