diff --git a/app/src/organisms/DropTipWizardFlows/hooks/useDropTipCommands.ts b/app/src/organisms/DropTipWizardFlows/hooks/useDropTipCommands.ts index 44679342b822..6d9c88db3d81 100644 --- a/app/src/organisms/DropTipWizardFlows/hooks/useDropTipCommands.ts +++ b/app/src/organisms/DropTipWizardFlows/hooks/useDropTipCommands.ts @@ -21,6 +21,7 @@ import type { RunCommandByCommandTypeParams } from './useDropTipCreateCommands' const JOG_COMMAND_TIMEOUT_MS = 10000 const MAXIMUM_BLOWOUT_FLOW_RATE_UL_PER_S = 50 +const MAX_QUEUED_JOGS = 3 type UseDropTipSetupCommandsParams = UseDTWithTypeParams & { activeMaintenanceRunId: string | null @@ -35,10 +36,9 @@ type UseDropTipSetupCommandsParams = UseDTWithTypeParams & { } export interface UseDropTipCommandsResult { - /* */ handleCleanUpAndClose: (homeOnExit?: boolean) => Promise moveToAddressableArea: (addressableArea: AddressableAreaName) => Promise - handleJog: (axis: Axis, dir: Sign, step: StepSize) => Promise + handleJog: (axis: Axis, dir: Sign, step: StepSize) => void blowoutOrDropTip: ( currentStep: DropTipFlowsStep, proceed: () => void @@ -46,7 +46,6 @@ export interface UseDropTipCommandsResult { handleMustHome: () => Promise } -// Returns setup commands used in Drop Tip Wizard. export function useDropTipCommands({ issuedCommandsType, toggleIsExiting, @@ -61,6 +60,8 @@ export function useDropTipCommands({ }: UseDropTipSetupCommandsParams): UseDropTipCommandsResult { const isFlex = robotType === FLEX_ROBOT_TYPE const [hasSeenClose, setHasSeenClose] = React.useState(false) + const [jogQueue, setJogQueue] = React.useState Promise>>([]) + const [isJogging, setIsJogging] = React.useState(false) const { deleteMaintenanceRun } = useDeleteMaintenanceRunMutation({ onSuccess: () => { @@ -149,7 +150,7 @@ export function useDropTipCommands({ }) } - const handleJog = (axis: Axis, dir: Sign, step: StepSize): Promise => { + const executeJog = (axis: Axis, dir: Sign, step: StepSize): Promise => { return new Promise((resolve, reject) => { return runCommand({ command: { @@ -175,6 +176,31 @@ export function useDropTipCommands({ }) } + const processJogQueue = (): void => { + if (jogQueue.length > 0 && !isJogging) { + setIsJogging(true) + const nextJog = jogQueue[0] + setJogQueue(prevQueue => prevQueue.slice(1)) + nextJog().finally(() => { + setIsJogging(false) + // processJogQueue() + }) + } + } + + React.useEffect(() => { + processJogQueue() + }, [jogQueue.length, isJogging]) + + const handleJog = (axis: Axis, dir: Sign, step: StepSize): void => { + setJogQueue(prevQueue => { + if (prevQueue.length < MAX_QUEUED_JOGS) { + return [...prevQueue, () => executeJog(axis, dir, step)] + } + return prevQueue + }) + } + const blowoutOrDropTip = ( currentStep: DropTipFlowsStep, proceed: () => void