From 1a7a15fb8b7e47210cbc8a661140fddd2136834c Mon Sep 17 00:00:00 2001 From: mkurczewski Date: Tue, 15 Oct 2024 14:48:56 +0200 Subject: [PATCH] Changed the way of time synchronization --- .../initialize-mudita-harmony.action.ts | 3 +++ .../time-synchronization.component.tsx | 27 ++++++++++++------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/libs/core/device-initialization/actions/initialize-mudita-harmony.action.ts b/libs/core/device-initialization/actions/initialize-mudita-harmony.action.ts index bb088cd197..b80b87a67c 100644 --- a/libs/core/device-initialization/actions/initialize-mudita-harmony.action.ts +++ b/libs/core/device-initialization/actions/initialize-mudita-harmony.action.ts @@ -16,6 +16,7 @@ import { isActiveDeviceAttachedSelector } from "device-manager/feature" import { isActiveDeviceProcessingSelector } from "Core/device/selectors/is-active-device-processing.selector" import { getCrashDump } from "Core/crash-dump" import { checkForForceUpdateNeed } from "Core/update/actions" +import { getTime } from "Core/time-synchronization/actions/get-time.action" export const initializeMuditaHarmony = createAsyncThunk< DeviceInitializationStatus, @@ -37,6 +38,8 @@ export const initializeMuditaHarmony = createAsyncThunk< ) } + await dispatch(getTime()) + const activeDeviceProcessing = isActiveDeviceProcessingSelector(getState()) if (!activeDeviceProcessing) { diff --git a/libs/core/overview/components/time-synchronization/time-synchronization.component.tsx b/libs/core/overview/components/time-synchronization/time-synchronization.component.tsx index 74e6410b0a..b7c2ab05aa 100644 --- a/libs/core/overview/components/time-synchronization/time-synchronization.component.tsx +++ b/libs/core/overview/components/time-synchronization/time-synchronization.component.tsx @@ -70,7 +70,8 @@ const TimeSynchronization: FunctionComponent = ({ const status = useSelector(selectTimeSynchronizationStatus) const time = useSelector(selectSynchronizedTime) const syncTimeoutRef = useRef() - const getIntervalRef = useRef() + const getTimeoutRef = useRef() + const firstTimeSyncRef = useRef(false) const hourCycle = new Intl.DateTimeFormat(undefined, { timeStyle: "long", @@ -117,17 +118,23 @@ const TimeSynchronization: FunctionComponent = ({ }, [dispatch, status]) useEffect(() => { - clearInterval(getIntervalRef.current) - getIntervalRef.current = setInterval(() => { - const actualMinute = new Date().getMinutes() - const deviceMinute = time ? new Date(time).getMinutes() : undefined - if (actualMinute !== deviceMinute) { - dispatch(getTime()) - } - }, 1000) + if (!time) return + if (!firstTimeSyncRef.current) { + dispatch(getTime()) + firstTimeSyncRef.current = true + return + } + clearTimeout(getTimeoutRef.current) + + const deviceSeconds = new Date(time).getSeconds() + const timeout = Math.max(1, 60 - deviceSeconds) + 1 + + getTimeoutRef.current = setTimeout(() => { + dispatch(getTime()) + }, timeout * 1000) return () => { - clearInterval(getIntervalRef.current) + clearTimeout(getTimeoutRef.current) } }, [dispatch, time])