All files / client/src/hooks useSimulatorActions.ts

53.84% Statements 7/13
100% Branches 0/0
14.28% Functions 1/7
53.84% Lines 7/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96                                                                                                                            7x       7x       7x       7x       7x       7x       7x                  
/**
 * useSimulatorActions Hook
 * 
 * Encapsulates simulator control actions (start, stop, reset, compile & run).
 * Provides a clean interface for simulator control components.
 * 
 * This hook wraps and centralizes the simulator action handlers that were
 * previously scattered across ArduinoSimulatorPage, reducing coupling and
 * improving testability.
 */
 
import { useCallback } from "react";
 
/**
 * Actions that can be performed on the simulator
 */
interface SimulatorActions {
  /** Start a previously compiled sketch or return early if already running */
  handleStart: () => void;
  
  /** Stop the running simulation and clean up state */
  handleStop: () => void;
  
  /** Pause a running simulation (preserves state) */
  handlePause: () => void;
  
  /** Resume a paused simulation */
  handleResume: () => void;
  
  /** Reset simulator state (clear outputs, reset pins) */
  handleReset: () => void;
  
  /** Compile the current sketch and start simulation on success */
  handleCompileAndStart: () => void;
}
 
/**
 * Parameters required by useSimulatorActions
 */
interface UseSimulatorActionsParams {
  // Action implementations from parent hooks (useCompileAndRun, useSimulation, etc.)
  onStart: () => void;
  onStop: () => void;
  onPause: () => void;
  onResume: () => void;
  onReset: () => void;
  onCompileAndStart: () => void;
}
 
/**
 * Hook that manages simulator control actions.
 * 
 * Acts as an organizational layer that:
 * - Centralizes simulator action handlers
 * - Provides a consistent interface for components
 * - Ensures proper sequencing and state management
 * 
 * @param params Action implementations from useCompileAndRun hook
 * @returns SimulatorActions interface with memoized handlers
 */
export function useSimulatorActions(params: UseSimulatorActionsParams): SimulatorActions {
  // Memoize all handlers to prevent unnecessary re-renders in child components
  const handleStart = useCallback(() => {
    params.onStart();
  }, [params]);
 
  const handleStop = useCallback(() => {
    params.onStop();
  }, [params]);
 
  const handlePause = useCallback(() => {
    params.onPause();
  }, [params]);
 
  const handleResume = useCallback(() => {
    params.onResume();
  }, [params]);
 
  const handleReset = useCallback(() => {
    params.onReset();
  }, [params]);
 
  const handleCompileAndStart = useCallback(() => {
    params.onCompileAndStart();
  }, [params]);
 
  return {
    handleStart,
    handleStop,
    handlePause,
    handleResume,
    handleReset,
    handleCompileAndStart,
  };
}