Skip to content

Commit c93d932

Browse files
committed
feat(process): add 'taskProcessRate' parameter
1 parent d59641a commit c93d932

12 files changed

Lines changed: 172 additions & 94 deletions

File tree

README.md

Lines changed: 71 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## 🧩 Pathfinding Worker
2+
23
[![Version](https://badgen.net/npm/v/pathfinding-worker)](https://npmjs.com/package/pathfinding-worker)
34
[![Small size](https://img.badgesize.io/neki-dev/pathfinding-worker/main/dist/index.js)](https://github.com/neki-dev/pathfinding-worker/blob/main/dist/index.js)
45
[![Build](https://github.com/neki-dev/pathfinding-worker/actions/workflows/build.yml/badge.svg)](https://github.com/neki-dev/pathfinding-worker/actions/workflows/build.yml)
@@ -10,13 +11,13 @@ Fast node.js pathfinding on workers for grid-based games.
1011

1112
Documentation
1213

13-
* [Install](https://github.com/neki-dev/pathfinding-worker?tab=readme-ov-file#install)
14-
* [General](https://github.com/neki-dev/pathfinding-worker?tab=readme-ov-file#general)
15-
* [Layers](https://github.com/neki-dev/pathfinding-worker?tab=readme-ov-file#layers)
16-
* [Finding](https://github.com/neki-dev/pathfinding-worker?tab=readme-ov-file#finding)
17-
* [Tile walkable](https://github.com/neki-dev/pathfinding-worker?tab=readme-ov-file#tile-walkable)
18-
* [Tile weight](https://github.com/neki-dev/pathfinding-worker?tab=readme-ov-file#tile-weight)
19-
* [Example](https://github.com/neki-dev/pathfinding-worker?tab=readme-ov-file#example)
14+
- [Install](https://github.com/neki-dev/pathfinding-worker?tab=readme-ov-file#install)
15+
- [General](https://github.com/neki-dev/pathfinding-worker?tab=readme-ov-file#general)
16+
- [Layers](https://github.com/neki-dev/pathfinding-worker?tab=readme-ov-file#layers)
17+
- [Finding](https://github.com/neki-dev/pathfinding-worker?tab=readme-ov-file#finding)
18+
- [Tile walkable](https://github.com/neki-dev/pathfinding-worker?tab=readme-ov-file#tile-walkable)
19+
- [Tile weight](https://github.com/neki-dev/pathfinding-worker?tab=readme-ov-file#tile-weight)
20+
- [Example](https://github.com/neki-dev/pathfinding-worker?tab=readme-ov-file#example)
2021

2122
.
2223

@@ -31,131 +32,155 @@ npm i pathfinding-worker
3132
## General
3233

3334
### ⚡️ Create worker thread
35+
3436
```ts
3537
const pathfinding = new Pathfinding(
3638
config: PathfindingConfig
3739
)
3840
```
39-
* `config` - _Pathfinding configuration_
40-
41-
| Prop | Description | Default |
42-
| ---- | ----------- | ------- |
43-
| loopRate | Finding loop rate | 200 ms |
44-
| resourceLimits | Worker resource limits | - |
41+
42+
- `config` - _Pathfinding configuration_
43+
44+
| Prop | Description | Default |
45+
| --------------- | ---------------------- | ------- |
46+
| loopRate | Finding next task rate | 100 ms |
47+
| taskProcessRate | Processing task rate | 0 ms |
48+
| resourceLimits | Worker resource limits | - |
4549

4650
### ⚡️ Terminate worker thread
51+
4752
```ts
48-
pathfinding.destroy()
53+
pathfinding.destroy();
4954
```
5055

5156
.
5257

5358
## Layers
5459

5560
### ⚡️ Create a new layer of grid
61+
5662
```ts
5763
const layer = pathfinding.createLayer(
5864
grid: PathfindingGrid,
5965
)
6066
```
61-
* `grid` - _Grid with walkable tiles_
67+
68+
- `grid` - _Grid with walkable tiles_
6269

6370
### ⚡️ Get list of created layers
71+
6472
```ts
65-
pathfinding.getLayers()
73+
pathfinding.getLayers();
6674
```
6775

6876
### ⚡️ Get layer by id
77+
6978
```ts
7079
pathfinding.getLayer(
7180
id: string
7281
)
7382
```
74-
* `id` - _Layer id_
83+
84+
- `id` - _Layer id_
7585

7686
### ⚡️ Remove exist layer of grid
87+
7788
```ts
78-
layer.remove()
89+
layer.remove();
7990
```
8091

8192
.
8293

8394
## Finding
8495

8596
### ⚡️ Create pathfinder task
97+
8698
```ts
8799
const idTask = layer.findPath(
88100
config: PathfindingTaskConfig,
89101
callback: PathfindingTaskCallback,
90102
)
91103
```
92-
* `config` - _Task configuration_
93-
94-
| Prop | Description | Default |
95-
| ---- | ----------- | ------- |
96-
| from | Begin tile position | |
97-
| to | End tile position | |
98-
| diagonals | Allow diagonal directions | true |
99104

100-
* `callback` - _Callback with result_
105+
- `config` - _Task configuration_
106+
107+
| Prop | Description | Default |
108+
| --------- | ------------------------- | ------- |
109+
| from | Begin tile position | |
110+
| to | End tile position | |
111+
| diagonals | Allow diagonal directions | true |
112+
113+
- `callback` - _Callback with result_
101114

102115
### ⚡️ Cancel pathfinder task
116+
103117
```ts
104118
layer.cancel(id: number)
105119
```
106-
* `id` - _Task id_
120+
121+
- `id` - _Task id_
107122

108123
.
109124

110125
## Tile walkable
111126

112127
### ⚡️ Set walkable state
128+
113129
```ts
114130
layer.setWalkable(
115131
position: PathfindingPosition,
116132
value: number,
117133
)
118134
```
119-
* `position` - _Tile position_
120-
* `state` - _Walkable state_
135+
136+
- `position` - _Tile position_
137+
- `state` - _Walkable state_
121138

122139
### ⚡️ Get walkable state
140+
123141
```ts
124142
const walkable = pathfinder.isWalkable(
125143
position: PathfindingPosition,
126144
)
127145
```
128-
* `position` - _Tile position_
146+
147+
- `position` - _Tile position_
129148

130149
.
131150

132151
## Tile weight
133152

134153
### ⚡️ Set weight
154+
135155
```ts
136156
layer.setWeight(
137157
position: PathfindingPosition,
138158
value: number,
139159
)
140160
```
141-
* `position` - _Tile position_
142-
* `value` - _New weight_
161+
162+
- `position` - _Tile position_
163+
- `value` - _New weight_
143164

144165
### ⚡️ Reset weight
166+
145167
```ts
146168
layer.resetWeight(
147169
position: PathfindingPosition,
148170
)
149171
```
150-
* `position` - _Tile position_
172+
173+
- `position` - _Tile position_
151174

152175
### ⚡️ Get weight
176+
153177
```ts
154178
const weight = layer.getWeight(
155179
position: PathfindingPosition,
156180
)
157181
```
158-
* `position` - _Tile position_
182+
183+
- `position` - _Tile position_
159184

160185
.
161186

@@ -167,17 +192,20 @@ const pathfinding = new Pathfinding({
167192
});
168193

169194
const layer = pathfinding.createLayer([
170-
[true, true, true, true],
171-
[true, true, false, true],
195+
[true, true, true, true],
196+
[true, true, false, true],
172197
[true, false, false, true],
173198
[true, false, false, false],
174199
]);
175200

176-
layer.findPath({
177-
from: { x: 0, y: 0 },
178-
to: { x: 3, y: 2 },
179-
}, ({ path, cost }) => {
180-
console.log('Result path:', path);
181-
console.log('Total cost:', cost);
182-
})
201+
layer.findPath(
202+
{
203+
from: { x: 0, y: 0 },
204+
to: { x: 3, y: 2 },
205+
},
206+
({ path, cost }) => {
207+
console.log("Result path:", path);
208+
console.log("Total cost:", cost);
209+
}
210+
);
183211
```

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/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export declare class Pathfinding {
1212
*
1313
* @param config - Pathfinding configuration
1414
*/
15-
constructor({ loopRate, resourceLimits }?: PathfindingConfig);
15+
constructor({ loopRate, taskProcessRate, resourceLimits }?: PathfindingConfig);
1616
/**
1717
* Terminate worker thread.
1818
*/

dist/pathfinding/types.d.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import type { ResourceLimits } from 'worker_threads';
22
export type PathfindingConfig = {
33
/**
4-
* Finding process loop rate
5-
* Default: 200 ms
4+
* Finding next task rate
5+
* Default: 100 ms
66
*/
77
loopRate?: number;
8+
/**
9+
* Processing task rate
10+
* Default: 0 ms
11+
*/
12+
taskProcessRate?: number;
813
/**
914
* Worker resource limits
1015
*/

dist/process/types.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export type PathfindingProcessConfig = {
2+
loopRate?: number;
3+
taskProcessRate?: number;
4+
};

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.4.0",
4+
"version": "2.5.1",
55
"keywords": [
66
"astar",
77
"dijkstra",

src/pathfinding/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ export class Pathfinding {
2020
*
2121
* @param config - Pathfinding configuration
2222
*/
23-
constructor({ loopRate, resourceLimits }: PathfindingConfig = {}) {
23+
constructor({ loopRate, taskProcessRate, resourceLimits }: PathfindingConfig = {}) {
2424
this.worker = new Worker(INLINE_WORKER, {
2525
name: 'pathfinding',
2626
eval: true,
27-
workerData: { loopRate },
27+
workerData: { loopRate, taskProcessRate },
2828
resourceLimits,
2929
});
3030

src/pathfinding/types.ts

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

33
export type PathfindingConfig = {
44
/**
5-
* Finding process loop rate
6-
* Default: 200 ms
5+
* Finding next task rate
6+
* Default: 100 ms
77
*/
88
loopRate?: number;
99

10+
/**
11+
* Processing task rate
12+
* Default: 0 ms
13+
*/
14+
taskProcessRate?: number;
15+
1016
/**
1117
* Worker resource limits
1218
*/

src/process/const.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
/**
22
* @internal
33
*/
4-
export const PATHFINDING_PROCESS_LOOP_RATE = 200;
4+
export const PATHFINDING_PROCESS_LOOP_RATE = 100;
5+
6+
/**
7+
* @internal
8+
*/
9+
export const PATHFINDING_PROCESS_TASK_PROCESS_RATE = 0;
510

611
/**
712
* @internal

0 commit comments

Comments
 (0)