Skip to content

Commit 263d511

Browse files
committed
fix(*): fix process
1 parent d800286 commit 263d511

File tree

9 files changed

+67
-31
lines changed

9 files changed

+67
-31
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ const pathfinding = new Pathfinding(
4141

4242
- `config` - _Pathfinding configuration_
4343

44-
| Prop | Description | Default |
45-
| -------------- | ---------------------- | ------- |
46-
| loopRate | Finding next task rate | 200 ms |
47-
| resourceLimits | Worker resource limits | - |
44+
| Prop | Description | Default |
45+
| -------------- | -------------------------------------------------------------------------------- | ------- |
46+
| loopRate | Pause before executing the next task after all previous ones have been completed | 100 ms |
47+
| maxStackSize | Max recursive depth to handle task in one tick | 128 |
48+
| resourceLimits | Worker resource limits | - |
4849

4950
### ⚡️ Terminate worker thread
5051

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/pathfinding/types.d.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import type { ResourceLimits } from 'worker_threads';
22
export type PathfindingConfig = {
33
/**
4-
* Finding next task rate
5-
* Default: 200 ms
4+
* Use taskFindingRate instead
5+
* @deprecated
66
*/
77
loopRate?: number;
8+
/**
9+
* Pause before executing the next task after all previous ones have been completed
10+
* Default: 100 ms
11+
*/
12+
taskFindingRate?: number;
13+
/**
14+
* Max recursive depth to handle task in one tick
15+
* Default: 128
16+
*/
17+
maxStackSize?: number;
818
/**
919
* Worker resource limits
1020
*/

dist/process/types.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
export type PathfindingProcessConfig = {
2+
/**
3+
* @deprecated
4+
*/
25
loopRate?: number;
6+
taskFindingRate?: number;
7+
maxStackSize?: number;
38
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "pathfinding-worker",
33
"description": "Fast node.js pathfinding on workers for grid-based games",
4-
"version": "2.6.0",
4+
"version": "2.7.0",
55
"keywords": [
66
"astar",
77
"dijkstra",

src/pathfinding/types.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,23 @@ import type { ResourceLimits } from 'worker_threads';
22

33
export type PathfindingConfig = {
44
/**
5-
* Finding next task rate
6-
* Default: 200 ms
5+
* Use taskFindingRate instead
6+
* @deprecated
77
*/
88
loopRate?: number;
99

10+
/**
11+
* Pause before executing the next task after all previous ones have been completed
12+
* Default: 100 ms
13+
*/
14+
taskFindingRate?: number;
15+
16+
/**
17+
* Max recursive depth to handle task in one tick
18+
* Default: 128
19+
*/
20+
maxStackSize?: number;
21+
1022
/**
1123
* Worker resource limits
1224
*/

src/process/const.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @internal
33
*/
4-
export const PATHFINDING_PROCESS_LOOP_RATE = 100;
4+
export const PATHFINDING_PROCESS_TASK_FINDING_RATE = 100;
55

66
/**
77
* @internal

src/process/index.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
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 {

src/process/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
export type PathfindingProcessConfig = {
2+
/**
3+
* @deprecated
4+
*/
25
loopRate?: number;
6+
taskFindingRate?: number;
7+
maxStackSize?: number;
38
};

0 commit comments

Comments
 (0)