From d880ea54f7046f31a8ba622996bacb914b077f15 Mon Sep 17 00:00:00 2001 From: Sivert Schou Olsen Date: Mon, 9 Feb 2026 17:59:12 +0100 Subject: [PATCH 1/2] fix: Use wall-clock time Ah shieeet, here we go again --- apps/frontend/src/context/DataContext.tsx | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/apps/frontend/src/context/DataContext.tsx b/apps/frontend/src/context/DataContext.tsx index 2f8d355d..db1a0304 100644 --- a/apps/frontend/src/context/DataContext.tsx +++ b/apps/frontend/src/context/DataContext.tsx @@ -62,9 +62,9 @@ export const DataContextProvider = ({ clockWorker, children }: Props) => { const hasValidData = trackedData.length > 0; - const accumulatedTimeRef = useLiveRef(0); - const pausedTimeRef = useLiveRef(0); - const lastElapsedRef = useLiveRef(0); + const accumulatedTimeRef = React.useRef(0); + const pausedTimeRef = React.useRef(0); + const lastElapsedRef = React.useRef(0); const powerRef = useLiveRef(power); const heartRateRef = useLiveRef(heartRate); const cadenceRef = useLiveRef(cadence); @@ -166,6 +166,12 @@ export const DataContextProvider = ({ clockWorker, children }: Props) => { syncResistance(); } + // Initialize refs from current elapsed time when resuming a previous session + if (state === 'not_started' && workoutState.elapsedTime > 0) { + pausedTimeRef.current = workoutState.elapsedTime; + lastElapsedRef.current = workoutState.elapsedTime; + } + startActiveWorkout(); clockWorker.postMessage('startClockTimer'); setState('running'); @@ -175,6 +181,8 @@ export const DataContextProvider = ({ clockWorker, children }: Props) => { syncResistance, startActiveWorkout, trackedData.length, + state, + workoutState.elapsedTime, ]); const stop = React.useCallback(async () => { @@ -206,14 +214,6 @@ export const DataContextProvider = ({ clockWorker, children }: Props) => { workoutState.elapsedTime, ]); - // Initialize paused time when workout state loads with existing elapsed time - React.useEffect(() => { - if (state === 'not_started' && workoutState.elapsedTime > 0) { - pausedTimeRef.current = workoutState.elapsedTime; - lastElapsedRef.current = workoutState.elapsedTime; - } - }, [workoutState.elapsedTime, state]); - return ( Date: Tue, 10 Feb 2026 07:50:22 +0100 Subject: [PATCH 2/2] chore: Pull out accumulatedTime as separate variable and clear it before flusing --- apps/frontend/src/context/DataContext.tsx | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/apps/frontend/src/context/DataContext.tsx b/apps/frontend/src/context/DataContext.tsx index db1a0304..d11eaee3 100644 --- a/apps/frontend/src/context/DataContext.tsx +++ b/apps/frontend/src/context/DataContext.tsx @@ -72,9 +72,10 @@ export const DataContextProvider = ({ clockWorker, children }: Props) => { const dataTick = React.useCallback( async (delta: number) => { try { - if (accumulatedTimeRef.current > 0) { - await db.addElapsedTime(accumulatedTimeRef.current); - accumulatedTimeRef.current = 0; + const timeToFlush = accumulatedTimeRef.current; + accumulatedTimeRef.current = 0; + if (timeToFlush > 0) { + await db.addElapsedTime(timeToFlush); } const currentHeartRate = heartRateRef.current; @@ -194,10 +195,11 @@ export const DataContextProvider = ({ clockWorker, children }: Props) => { clockWorker.postMessage('stopClockTimer'); - if (accumulatedTimeRef.current > 0) { + const timeToFlush = accumulatedTimeRef.current; + accumulatedTimeRef.current = 0; + if (timeToFlush > 0) { try { - await db.addElapsedTime(accumulatedTimeRef.current); - accumulatedTimeRef.current = 0; + await db.addElapsedTime(timeToFlush); } catch (error) { console.error('Failed to flush elapsed time on stop:', error); }