|
1 | | -import { useState, useEffect } from 'react'; |
| 1 | +import { useState, useEffect, useRef } from 'react'; |
2 | 2 | import { Cell } from '../algorithms/utils/PathfindingUtils'; |
3 | 3 | import { BreadthFirstSearch } from '../algorithms/pathfinding/BreadthFirstSearch.ts'; |
4 | 4 | import { Dijkstra } from '../algorithms/pathfinding/Dijkstra.ts'; |
@@ -27,49 +27,58 @@ const pathfindingAlgorithmHooks = ({ |
27 | 27 | setMaze, |
28 | 28 | }: PathfindingAlgorithmHooksProps) => { |
29 | 29 | const [algorithm, setAlgorithm] = useState<Algorithm | null>(null); |
| 30 | + const isRunningRef = useRef(false); |
30 | 31 |
|
31 | 32 | useEffect(() => { |
32 | | - if (!algorithm) return; |
| 33 | + if (!algorithm || isRunningRef.current) return; |
33 | 34 |
|
| 35 | + isRunningRef.current = true; |
34 | 36 | const startCell = maze[startNode[0]][startNode[1]]; |
35 | 37 | const endCell = maze[endNode[0]][endNode[1]]; |
36 | 38 |
|
37 | | - const algorithmMap: Record<Algorithm, () => Promise<void> | void> = { |
38 | | - [Algorithm.BFS]: async () => { |
39 | | - const optimalPath = await BreadthFirstSearch(maze, startCell, endCell, setMaze); |
40 | | - if (optimalPath) populateOptimalPath(optimalPath); |
41 | | - }, |
42 | | - [Algorithm.DIJKSTRA]: async () => { |
43 | | - const optimalPath = await Dijkstra(maze, startCell, endCell, setMaze); |
44 | | - if (optimalPath) populateOptimalPath(optimalPath); |
45 | | - }, |
46 | | - [Algorithm.DFS]: async () => { |
47 | | - const optimalPath = await DepthFirstSearch(maze, startCell, endCell, setMaze); |
48 | | - if (optimalPath) populateOptimalPath(optimalPath); |
49 | | - }, |
50 | | - [Algorithm.PRIMS_MAZE]: () => { |
51 | | - primsMazeGeneration(maze, startCell, endCell, setMaze); |
52 | | - }, |
53 | | - [Algorithm.DFS_MAZE]: () => { |
54 | | - dfsMazeGeneration(startCell, endCell); |
55 | | - }, |
56 | | - [Algorithm.KRUSKAL_MAZE]: () => { |
57 | | - kruskalsMazeGeneration(maze, startCell, endCell, setMaze); |
58 | | - }, |
59 | | - [Algorithm.RECURSIVE_DIVISION_MAZE]: () => { |
60 | | - recursiveDivisionMazeGeneration(maze, startCell, endCell, setMaze); |
61 | | - }, |
62 | | - [Algorithm.RECURSIVE_DIVISION_MAZE_HORIZONTAL_BIAS]: () => { |
63 | | - recursiveDivisionMazeGeneration(maze, startCell, endCell, setMaze, 0.4); |
64 | | - }, |
65 | | - [Algorithm.RECURSIVE_DIVISION_MAZE_VERTICAL_BIAS]: () => { |
66 | | - recursiveDivisionMazeGeneration(maze, startCell, endCell, setMaze, 0.6); |
| 39 | + const runAlgorithm = async () => { |
| 40 | + const algorithmMap: Record<Algorithm, () => Promise<void> | void> = { |
| 41 | + [Algorithm.BFS]: async () => { |
| 42 | + const optimalPath = await BreadthFirstSearch(startCell, endCell); |
| 43 | + if (optimalPath) populateOptimalPath(optimalPath); |
| 44 | + }, |
| 45 | + [Algorithm.DIJKSTRA]: async () => { |
| 46 | + const optimalPath = await Dijkstra(maze, startCell, endCell, setMaze); |
| 47 | + if (optimalPath) populateOptimalPath(optimalPath); |
| 48 | + }, |
| 49 | + [Algorithm.DFS]: async () => { |
| 50 | + const optimalPath = await DepthFirstSearch(maze, startCell, endCell, setMaze); |
| 51 | + if (optimalPath) populateOptimalPath(optimalPath); |
| 52 | + }, |
| 53 | + [Algorithm.PRIMS_MAZE]: () => { |
| 54 | + primsMazeGeneration(maze, startCell, endCell, setMaze); |
| 55 | + }, |
| 56 | + [Algorithm.DFS_MAZE]: () => { |
| 57 | + dfsMazeGeneration(startCell, endCell); |
| 58 | + }, |
| 59 | + [Algorithm.KRUSKAL_MAZE]: () => { |
| 60 | + kruskalsMazeGeneration(maze, startCell, endCell, setMaze); |
| 61 | + }, |
| 62 | + [Algorithm.RECURSIVE_DIVISION_MAZE]: () => { |
| 63 | + recursiveDivisionMazeGeneration(maze, startCell, endCell, setMaze); |
| 64 | + }, |
| 65 | + [Algorithm.RECURSIVE_DIVISION_MAZE_HORIZONTAL_BIAS]: () => { |
| 66 | + recursiveDivisionMazeGeneration(maze, startCell, endCell, setMaze, 0.4); |
| 67 | + }, |
| 68 | + [Algorithm.RECURSIVE_DIVISION_MAZE_VERTICAL_BIAS]: () => { |
| 69 | + recursiveDivisionMazeGeneration(maze, startCell, endCell, setMaze, 0.6); |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + if (algorithm) { |
| 74 | + await algorithmMap[algorithm]?.(); |
| 75 | + setAlgorithm(null); |
| 76 | + isRunningRef.current = false; |
67 | 77 | } |
68 | 78 | }; |
69 | 79 |
|
70 | | - algorithmMap[algorithm]?.(); |
| 80 | + runAlgorithm(); |
71 | 81 |
|
72 | | - setAlgorithm(null); |
73 | 82 | }, [algorithm, maze, startNode, endNode, clearLatestRun, populateOptimalPath, setMaze]); |
74 | 83 |
|
75 | 84 | const triggerAlgorithm = (algorithm: Algorithm) => { |
|
0 commit comments