Skip to content

Commit

Permalink
Changed the way of time synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurczewski committed Oct 15, 2024
1 parent 9709812 commit 1a7a15f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -37,6 +38,8 @@ export const initializeMuditaHarmony = createAsyncThunk<
)
}

await dispatch(getTime())

const activeDeviceProcessing = isActiveDeviceProcessingSelector(getState())

if (!activeDeviceProcessing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ const TimeSynchronization: FunctionComponent<Props> = ({
const status = useSelector(selectTimeSynchronizationStatus)
const time = useSelector(selectSynchronizedTime)
const syncTimeoutRef = useRef<NodeJS.Timeout>()
const getIntervalRef = useRef<NodeJS.Timeout>()
const getTimeoutRef = useRef<NodeJS.Timeout>()
const firstTimeSyncRef = useRef(false)

const hourCycle = new Intl.DateTimeFormat(undefined, {
timeStyle: "long",
Expand Down Expand Up @@ -117,17 +118,23 @@ const TimeSynchronization: FunctionComponent<Props> = ({
}, [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])

Expand Down

0 comments on commit 1a7a15f

Please sign in to comment.