Skip to content

Commit 3af4114

Browse files
committed
chore: Pull out accumulatedTime as separate variable and clear it before flusing
1 parent d880ea5 commit 3af4114

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

apps/frontend/src/context/DataContext.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,10 @@ export const DataContextProvider = ({ clockWorker, children }: Props) => {
7272
const dataTick = React.useCallback(
7373
async (delta: number) => {
7474
try {
75-
if (accumulatedTimeRef.current > 0) {
76-
await db.addElapsedTime(accumulatedTimeRef.current);
77-
accumulatedTimeRef.current = 0;
75+
const timeToFlush = accumulatedTimeRef.current;
76+
accumulatedTimeRef.current = 0;
77+
if (timeToFlush > 0) {
78+
await db.addElapsedTime(timeToFlush);
7879
}
7980

8081
const currentHeartRate = heartRateRef.current;
@@ -194,10 +195,11 @@ export const DataContextProvider = ({ clockWorker, children }: Props) => {
194195

195196
clockWorker.postMessage('stopClockTimer');
196197

197-
if (accumulatedTimeRef.current > 0) {
198+
const timeToFlush = accumulatedTimeRef.current;
199+
accumulatedTimeRef.current = 0;
200+
if (timeToFlush > 0) {
198201
try {
199-
await db.addElapsedTime(accumulatedTimeRef.current);
200-
accumulatedTimeRef.current = 0;
202+
await db.addElapsedTime(timeToFlush);
201203
} catch (error) {
202204
console.error('Failed to flush elapsed time on stop:', error);
203205
}

0 commit comments

Comments
 (0)