Refactor simulation action handlers: consolidate action handling, enhance spawn logic, and improve type definitions for actions.
This commit is contained in:
@@ -3,23 +3,76 @@ import { useMachineActions } from "./machine/useMachineActions";
|
||||
import { useRoboticArmActions } from "./roboticArm/useRoboticArmActions";
|
||||
import { useStorageActions } from "./storageUnit/useStorageUnitActions";
|
||||
import { useVehicleActions } from "./vehicle/useVehicleActions";
|
||||
import { useCallback, useEffect } from "react";
|
||||
|
||||
// Master hook that selects the appropriate action handler
|
||||
export function useActionHandler(point: PointsScheme) {
|
||||
if ('actions' in point) {
|
||||
// Robotic Arm
|
||||
useRoboticArmActions(point);
|
||||
} else if (point.action.actionType === 'travel') {
|
||||
// Vehicle
|
||||
useVehicleActions(point as VehiclePointSchema);
|
||||
} else if (point.action.actionType === 'process') {
|
||||
// Machine
|
||||
useMachineActions(point as MachinePointSchema);
|
||||
} else if (point.action.actionType === 'store') {
|
||||
// Storage
|
||||
useStorageActions(point as StoragePointSchema);
|
||||
} else {
|
||||
// Conveyor
|
||||
useConveyorActions(point as ConveyorPointSchema);
|
||||
}
|
||||
export function useActionHandler() {
|
||||
// Initialize all action handlers
|
||||
const { handleConveyorAction, cleanup: cleanupConveyor } = useConveyorActions();
|
||||
const { handleVehicleAction, cleanup: cleanupVehicle } = useVehicleActions();
|
||||
const { handleRoboticArmAction, cleanup: cleanupRoboticArm } = useRoboticArmActions();
|
||||
const { handleMachineAction, cleanup: cleanupMachine } = useMachineActions();
|
||||
const { handleStorageAction, cleanup: cleanupStorage } = useStorageActions();
|
||||
|
||||
// Main handler function
|
||||
const handleAction = useCallback((action: Action) => {
|
||||
if (!action) return;
|
||||
|
||||
try {
|
||||
switch (action.actionType) {
|
||||
case 'default': case 'spawn': case 'swap': case 'delay': case 'despawn':
|
||||
handleConveyorAction(action as ConveyorAction);
|
||||
break;
|
||||
case 'travel':
|
||||
handleVehicleAction(action as VehicleAction);
|
||||
break;
|
||||
case 'pickAndPlace':
|
||||
handleRoboticArmAction(action as RoboticArmAction);
|
||||
break;
|
||||
case 'process':
|
||||
handleMachineAction(action as MachineAction);
|
||||
break;
|
||||
case 'store':
|
||||
handleStorageAction(action as StorageAction);
|
||||
break;
|
||||
default:
|
||||
console.warn(`Unknown action type: ${(action as Action).actionType}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error handling action:', error);
|
||||
// Consider adding error recovery or notification here
|
||||
}
|
||||
}, [
|
||||
handleConveyorAction,
|
||||
handleVehicleAction,
|
||||
handleRoboticArmAction,
|
||||
handleMachineAction,
|
||||
handleStorageAction
|
||||
]);
|
||||
|
||||
// Cleanup all actions
|
||||
const cleanup = useCallback(() => {
|
||||
cleanupConveyor();
|
||||
cleanupVehicle();
|
||||
cleanupRoboticArm();
|
||||
cleanupMachine();
|
||||
cleanupStorage();
|
||||
}, [
|
||||
cleanupConveyor,
|
||||
cleanupVehicle,
|
||||
cleanupRoboticArm,
|
||||
cleanupMachine,
|
||||
cleanupStorage
|
||||
]);
|
||||
|
||||
// Auto cleanup on unmount
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
cleanup();
|
||||
};
|
||||
}, [cleanup]);
|
||||
|
||||
return {
|
||||
handleAction,
|
||||
cleanup
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user