11import {
2- PATHFINDING_PROCESS_LOOP_RATE ,
2+ PATHFINDING_PROCESS_TASK_FINDING_RATE ,
33 PATHFINDING_PROCESS_NEXT_DIRECTIINS_DIAGONAL ,
44 PATHFINDING_PROCESS_NEXT_DIRECTIINS_STRAIGHT ,
55 PATHFINDING_PROCESS_TASK_HANDLING_MAX_STACK_SIZE ,
@@ -21,27 +21,27 @@ export class PathfindingProcess {
2121
2222 private queue : PathfindingTask [ ] = [ ] ;
2323
24- private readonly timer : NodeJS . Timeout ;
24+ private timer : NodeJS . Timeout | null = null ;
2525
26- private processing : boolean = false ;
26+ private readonly maxStackSize : number ;
2727
28- constructor ( {
29- loopRate = PATHFINDING_PROCESS_LOOP_RATE ,
30- } : PathfindingProcessConfig ) {
31- this . timer = setInterval ( ( ) => {
32- try {
33- if ( ! this . processing ) {
34- this . processing = true ;
35- this . handleTask ( ) ;
36- }
37- } catch ( error ) {
38- console . error ( 'Pathfinding process error:' , error ) ;
39- }
40- } , loopRate ) ;
28+ private readonly taskFindingRate : number ;
29+
30+ constructor ( { taskFindingRate, maxStackSize, loopRate } : PathfindingProcessConfig ) {
31+ this . taskFindingRate = ( taskFindingRate ?? loopRate ) ?? PATHFINDING_PROCESS_TASK_FINDING_RATE ;
32+ this . maxStackSize = maxStackSize ?? PATHFINDING_PROCESS_TASK_HANDLING_MAX_STACK_SIZE ;
33+
34+ try {
35+ this . handleTask ( ) ;
36+ } catch ( error ) {
37+ console . error ( 'Pathfinding process initialize error:' , error ) ;
38+ }
4139 }
4240
4341 public destroy ( ) : void {
44- clearTimeout ( this . timer ) ;
42+ if ( this . timer ) {
43+ clearTimeout ( this . timer ) ;
44+ }
4545 }
4646
4747 public createTask ( task : PathfindingTask ) : void {
@@ -105,12 +105,15 @@ export class PathfindingProcess {
105105 private handleTask ( stackSize : number = 0 ) : void {
106106 const task = this . queue [ 0 ] ;
107107 if ( ! task ) {
108- this . processing = false ;
108+ this . timer = setTimeout ( ( ) => {
109+ this . timer = null ;
110+ this . handleTask ( ) ;
111+ } , this . taskFindingRate ) ;
109112 return ;
110113 }
111114
112115 const next = ( ) => {
113- if ( stackSize >= PATHFINDING_PROCESS_TASK_HANDLING_MAX_STACK_SIZE ) {
116+ if ( stackSize >= this . maxStackSize ) {
114117 setTimeout ( ( ) => {
115118 this . handleTask ( ) ;
116119 } ) ;
@@ -122,7 +125,7 @@ export class PathfindingProcess {
122125 const complete = ( result : PathfindingTaskResultRaw ) => {
123126 this . queue . shift ( ) ;
124127 task . complete ( result ) ;
125- this . processing = false ;
128+ next ( ) ;
126129 } ;
127130
128131 try {
0 commit comments