Skip to content

Commit

Permalink
[CP-3170] useAbortFlashingOnDeviceDetached -> useMscDeviceDetachedEff…
Browse files Browse the repository at this point in the history
…ect (with debounce logic)
  • Loading branch information
dkarski committed Oct 15, 2024
1 parent 8314fad commit ea43f57
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { useDeviceLockedEffect } from "Core/core/hooks/use-device-locked-effect"
import { useDeviceDetachedEffect } from "Core/core/hooks/use-device-detached-effect"
import { useDeviceConnectFailedEffect } from "Core/core/hooks/use-device-connect-failed-effect"
import { useDiscoveryRedirectEffect } from "Core/core/hooks/use-discovery-redirect-effect"
import { useAbortFlashingOnDeviceDetached } from "Core/core/hooks/use-abort-flashing-on-device-detached"
import { useMscDeviceDetachedEffect } from "Core/core/hooks/use-msc-device-detached-effect"
import { useRouterListener } from "Core/core/hooks"
import {
OutboxWrapper,
Expand Down Expand Up @@ -48,7 +48,7 @@ const BaseApp: FunctionComponent = () => {
useHelp()

// MSC
useAbortFlashingOnDeviceDetached()
useMscDeviceDetachedEffect()

return (
<>
Expand Down
35 changes: 0 additions & 35 deletions libs/core/core/hooks/use-abort-flashing-on-device-detached.ts

This file was deleted.

55 changes: 55 additions & 0 deletions libs/core/core/hooks/use-msc-device-detached-effect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright (c) Mudita sp. z o.o. All rights reserved.
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md
*/

import { useCallback, useEffect } from "react"
import { useDispatch, useSelector } from "react-redux"
import { answerMain, useDebouncedEventsHandler } from "shared/utils"
import {
DeviceBaseProperties,
DeviceProtocolMainEvent,
DeviceType,
} from "device-protocol/models"
import {
abortMscFlashing,
FlashingProcessState,
selectIsFlashingInActivePhases,
} from "msc-flash-harmony"
import { Dispatch } from "Core/__deprecated__/renderer/store"

export const useMscDeviceDetachedEffect = () => {
const handleDevicesDetached = useHandleDevicesDetached()

const batchDeviceDetachedEvents =
useDebouncedEventsHandler<DeviceBaseProperties>(handleDevicesDetached)

useEffect(() => {
return answerMain(
DeviceProtocolMainEvent.DeviceDetached,
batchDeviceDetachedEvents
)
}, [batchDeviceDetachedEvents])
}

const useHandleDevicesDetached = () => {
const dispatch = useDispatch<Dispatch>()
const flashingInActivePhases = useSelector(selectIsFlashingInActivePhases)
return useCallback(
(deviceDetachedEvents: DeviceBaseProperties[]) => {
const mscEvents = deviceDetachedEvents.filter(
({ deviceType }) => deviceType === DeviceType.MuditaHarmonyMsc
)

if (mscEvents.length === 0) {
return
}

const reason = flashingInActivePhases
? FlashingProcessState.Failed
: undefined
dispatch(abortMscFlashing({ reason }))
},
[dispatch, flashingInActivePhases]
)
}

0 comments on commit ea43f57

Please sign in to comment.