Skip to content

Commit

Permalink
debounce jog controls
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhuff committed Aug 12, 2024
1 parent 1201666 commit a545e55
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions app/src/organisms/DropTipWizardFlows/hooks/useDropTipCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,18 +36,16 @@ type UseDropTipSetupCommandsParams = UseDTWithTypeParams & {
}

export interface UseDropTipCommandsResult {
/* */
handleCleanUpAndClose: (homeOnExit?: boolean) => Promise<void>
moveToAddressableArea: (addressableArea: AddressableAreaName) => Promise<void>
handleJog: (axis: Axis, dir: Sign, step: StepSize) => Promise<void>
handleJog: (axis: Axis, dir: Sign, step: StepSize) => void
blowoutOrDropTip: (
currentStep: DropTipFlowsStep,
proceed: () => void
) => Promise<void>
handleMustHome: () => Promise<void>
}

// Returns setup commands used in Drop Tip Wizard.
export function useDropTipCommands({
issuedCommandsType,
toggleIsExiting,
Expand All @@ -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<Array<() => Promise<void>>>([])
const [isJogging, setIsJogging] = React.useState(false)

const { deleteMaintenanceRun } = useDeleteMaintenanceRunMutation({
onSuccess: () => {
Expand Down Expand Up @@ -149,7 +150,7 @@ export function useDropTipCommands({
})
}

const handleJog = (axis: Axis, dir: Sign, step: StepSize): Promise<void> => {
const executeJog = (axis: Axis, dir: Sign, step: StepSize): Promise<void> => {
return new Promise((resolve, reject) => {
return runCommand({
command: {
Expand All @@ -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
Expand Down

0 comments on commit a545e55

Please sign in to comment.