perf(renderer): reduce activity timer storage writes
This commit is contained in:
parent
03106e0c24
commit
c1ebecf813
1 changed files with 16 additions and 9 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
import { memo, useEffect, useState } from 'react';
|
import { memo, useEffect, useRef, useState } from 'react';
|
||||||
|
|
||||||
import { useAppTranslation } from '@features/localization/renderer';
|
import { useAppTranslation } from '@features/localization/renderer';
|
||||||
import { SyncedLoader2 } from '@renderer/components/ui/SyncedLoader2';
|
import { SyncedLoader2 } from '@renderer/components/ui/SyncedLoader2';
|
||||||
|
|
@ -22,15 +22,19 @@ interface CurrentTaskIndicatorProps {
|
||||||
onOpenTask?: () => void;
|
onOpenTask?: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const ACTIVITY_TIMER_STORAGE_SYNC_INTERVAL_MS = 5_000;
|
||||||
|
|
||||||
function useActivityTimerLabel(
|
function useActivityTimerLabel(
|
||||||
activityTimer: MemberActivityTimerAnchor | null | undefined,
|
activityTimer: MemberActivityTimerAnchor | null | undefined,
|
||||||
isTimerRunning: boolean
|
isTimerRunning: boolean
|
||||||
): string | null {
|
): string | null {
|
||||||
const [nowMs, setNowMs] = useState(() => Date.now());
|
const [nowMs, setNowMs] = useState(() => Date.now());
|
||||||
|
const lastStorageSyncAtRef = useRef(0);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!activityTimer) return;
|
if (!activityTimer) return;
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
|
lastStorageSyncAtRef.current = now;
|
||||||
syncMemberActivityTimer({
|
syncMemberActivityTimer({
|
||||||
timerId: activityTimer.timerId,
|
timerId: activityTimer.timerId,
|
||||||
startedAtMs: activityTimer.startedAtMs,
|
startedAtMs: activityTimer.startedAtMs,
|
||||||
|
|
@ -56,6 +60,8 @@ function useActivityTimerLabel(
|
||||||
if (!activityTimer || !isTimerRunning) return;
|
if (!activityTimer || !isTimerRunning) return;
|
||||||
const handle = window.setInterval(() => {
|
const handle = window.setInterval(() => {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
|
if (now - lastStorageSyncAtRef.current >= ACTIVITY_TIMER_STORAGE_SYNC_INTERVAL_MS) {
|
||||||
|
lastStorageSyncAtRef.current = now;
|
||||||
syncMemberActivityTimer({
|
syncMemberActivityTimer({
|
||||||
timerId: activityTimer.timerId,
|
timerId: activityTimer.timerId,
|
||||||
startedAtMs: activityTimer.startedAtMs,
|
startedAtMs: activityTimer.startedAtMs,
|
||||||
|
|
@ -64,6 +70,7 @@ function useActivityTimerLabel(
|
||||||
runId: activityTimer.runId,
|
runId: activityTimer.runId,
|
||||||
nowMs: now,
|
nowMs: now,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
setNowMs(now);
|
setNowMs(now);
|
||||||
}, 1000);
|
}, 1000);
|
||||||
return () => window.clearInterval(handle);
|
return () => window.clearInterval(handle);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue