-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CP-3170] useAbortFlashingOnDeviceDetached -> useMscDeviceDetachedEff…
…ect (with debounce logic)
- Loading branch information
Showing
3 changed files
with
57 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 0 additions & 35 deletions
35
libs/core/core/hooks/use-abort-flashing-on-device-detached.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
) | ||
} |