Skip to content

Commit

Permalink
feat(types): add internal annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
neki-dev committed Aug 26, 2024
1 parent d5dd71d commit 325e593
Show file tree
Hide file tree
Showing 22 changed files with 60 additions and 183 deletions.
9 changes: 1 addition & 8 deletions dist/events/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
import type { PathfindingEventPayload, PathfindingEventsParent } from './types';
export declare class PathfindingEvents {
private listeners;
private parent;
constructor(parent: PathfindingEventsParent);
on<K extends keyof PathfindingEventPayload>(event: K, callback: (payload: PathfindingEventPayload[K]) => void): void;
send<K extends keyof PathfindingEventPayload>(event: K, payload: PathfindingEventPayload[K]): void;
}
export {};
54 changes: 1 addition & 53 deletions dist/events/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1 @@
import type { PathfindingTaskResult } from '../task/types';
import type { PathfindingGrid, PathfindingPosition } from '../types';
export declare enum PathfindingEvent {
CreateTask = "CreateTask",
CompleteTask = "CompleteTask",
CancelTask = "CancelTask",
AddLayer = "AddLayer",
RemoveLayer = "RemoveLayer",
SetWalkable = "SetWalkable",
SetWeight = "SetWeight"
}
export type PathfindingEventPayload = {
[PathfindingEvent.CreateTask]: {
idLayer: string;
idTask: number;
from: PathfindingPosition;
to: PathfindingPosition;
};
[PathfindingEvent.CancelTask]: {
idLayer: string;
idTask: number;
};
[PathfindingEvent.SetWeight]: {
idLayer: string;
position: PathfindingPosition;
value: number | null;
};
[PathfindingEvent.SetWalkable]: {
idLayer: string;
position: PathfindingPosition;
state: boolean;
};
[PathfindingEvent.CompleteTask]: {
idLayer: string;
idTask: number;
result: PathfindingTaskResult;
};
[PathfindingEvent.AddLayer]: {
idLayer: string;
grid: PathfindingGrid;
};
[PathfindingEvent.RemoveLayer]: {
idLayer: string;
};
};
export interface PathfindingEventsParent {
on(event: 'message', callback: (body: PathfindingEventsBody) => void): void;
postMessage(payload: Record<string, any>): void;
}
export type PathfindingEventsBody = {
event: string;
payload: Record<string, any>;
};
export {};
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

18 changes: 1 addition & 17 deletions dist/node/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1 @@
import type { PathfindingNodeConfig } from './types';
import type { PathfindingTaskResult } from '../task/types';
import type { PathfindingPosition } from '../types';
export declare class PathfindingNode {
readonly position: PathfindingPosition;
readonly distance: number;
private parent;
private weight;
constructor({ position, distance, weight, }: PathfindingNodeConfig);
getBetterGuessDistance(): number;
getWeight(): number;
setWeight(weight: number): void;
getParent(): PathfindingNode | null;
setParent(parent: PathfindingNode): void;
compute(): PathfindingTaskResult;
getNextWeight(shift: PathfindingPosition, weights: number[][]): number;
}
export {};
7 changes: 1 addition & 6 deletions dist/node/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
import type { PathfindingPosition } from '../types';
export type PathfindingNodeConfig = {
position: PathfindingPosition;
distance: number;
weight?: number;
};
export {};
38 changes: 1 addition & 37 deletions dist/process/const.d.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1 @@
export declare const PATHFINDING_PROCESS_LOOP_RATE = 200;
export declare const PATHFINDING_PROCESS_NEXT_DIRECTIINS_STRAIGHT: {
R: {
x: number;
y: number;
};
L: {
x: number;
y: number;
};
D: {
x: number;
y: number;
};
U: {
x: number;
y: number;
};
};
export declare const PATHFINDING_PROCESS_NEXT_DIRECTIINS_DIAGONAL: {
RD: {
x: number;
y: number;
};
RU: {
x: number;
y: number;
};
LU: {
x: number;
y: number;
};
LD: {
x: number;
y: number;
};
};
export {};
20 changes: 1 addition & 19 deletions dist/process/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1 @@
import type { PathfindingTask } from '../task';
import type { PathfindingGrid, PathfindingPosition } from '../types';
export declare class PathfindingProcess {
private readonly grids;
private readonly weights;
private queue;
private timer;
constructor(loopRate?: number);
destroy(): void;
createTask(task: PathfindingTask): void;
cancelTask(idLayer: string, idTask: number): void;
addLayer(idLayer: string, grid: PathfindingGrid): void;
removeLayer(idLayer: string): void;
setWeight(idLayer: string, position: PathfindingPosition, value: number | null): void;
setWalkable(idLayer: string, position: PathfindingPosition, state: boolean): void;
private next;
private getNextDirections;
private isWalkable;
}
export {};
22 changes: 1 addition & 21 deletions dist/task/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1 @@
import { PathfindingNode } from '../node';
import type { PathfindingTaskConfig, PathfindingTaskResult } from './types';
import type { PathfindingPosition } from '../types';
export declare class PathfindingTask {
readonly from: PathfindingPosition;
readonly to: PathfindingPosition;
readonly id: number;
readonly diagonals: boolean;
readonly idLayer: string;
private tree;
private nodes;
readonly complete: (result: PathfindingTaskResult) => void;
constructor({ idTask, from, to, idLayer, diagonals }: PathfindingTaskConfig, onComplete: (result: PathfindingTaskResult) => void);
private getDistanceFrom;
addNode(parent: PathfindingNode, position: PathfindingPosition, weight: number): void;
private pushNode;
pickNode(position: PathfindingPosition): PathfindingNode;
takeLastNode(): PathfindingNode | null;
useNode(current: PathfindingNode, next: PathfindingNode, weight: number): void;
getNextWeight(currentNode: PathfindingNode, shift: PathfindingPosition, weights: number[][]): number;
}
export {};
14 changes: 1 addition & 13 deletions dist/task/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1 @@
import type { PathfindingPosition } from '../types';
export type PathfindingTaskConfig = {
idTask: number;
idLayer: string;
from: PathfindingPosition;
to: PathfindingPosition;
diagonals?: boolean;
};
export type PathfindingTaskResult = {
path: PathfindingPosition[] | null;
weight: number;
};
export type PathfindingTaskCallback = (result: PathfindingTaskResult) => void;
export {};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "pathfinding-worker",
"description": "Fast node.js pathfinding on workers for grid-based games",
"version": "2.1.0",
"version": "2.1.1",
"keywords": [
"astar",
"dijkstra",
Expand Down
3 changes: 3 additions & 0 deletions src/events/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { PathfindingEventPayload, PathfindingEventsParent } from './types';

/**
* @internal
*/
export class PathfindingEvents {
private listeners = new Map<string, (payload: any) => void>();

Expand Down
12 changes: 12 additions & 0 deletions src/events/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { PathfindingTaskResult } from '../task/types';
import type { PathfindingGrid, PathfindingPosition } from '../types';

/**
* @internal
*/
export enum PathfindingEvent {
CreateTask = 'CreateTask',
CompleteTask = 'CompleteTask',
Expand All @@ -11,6 +14,9 @@ export enum PathfindingEvent {
SetWeight = 'SetWeight',
}

/**
* @internal
*/
export type PathfindingEventPayload = {
[PathfindingEvent.CreateTask]: {
idLayer: string;
Expand Down Expand Up @@ -46,11 +52,17 @@ export type PathfindingEventPayload = {
};
};

/**
* @internal
*/
export interface PathfindingEventsParent {
on(event: 'message', callback: (body: PathfindingEventsBody) => void): void;
postMessage(payload: Record<string, any>): void;
}

/**
* @internal
*/
export type PathfindingEventsBody = {
event: string;
payload: Record<string, any>;
Expand Down
5 changes: 1 addition & 4 deletions src/layer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ export class PathfindingLayer {
return;
}

if (!this.grid[position.y]) {
this.grid[position.y] = [];
}
this.grid[position.y][position.x] = state;

this.pathfinding.events.send(PathfindingEvent.SetWalkable, {
Expand Down Expand Up @@ -94,7 +91,7 @@ export class PathfindingLayer {
* @param position - Tile position
*/
public resetWeight(position: PathfindingPosition): void {
if (this.weights[position.y]?.[position.x] === undefined) {
if (this.getWeight(position) === PATHFINDING_DEFAULT_TILE_WEIGHT) {
return;
}

Expand Down
3 changes: 3 additions & 0 deletions src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import type { PathfindingNodeConfig } from './types';
import type { PathfindingTaskResult } from '../task/types';
import type { PathfindingPosition } from '../types';

/**
* @internal
*/
export class PathfindingNode {
readonly position: PathfindingPosition;

Expand Down
3 changes: 3 additions & 0 deletions src/node/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { PathfindingPosition } from '../types';

/**
* @internal
*/
export type PathfindingNodeConfig = {
position: PathfindingPosition;
distance: number;
Expand Down
9 changes: 9 additions & 0 deletions src/process/const.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
/**
* @internal
*/
export const PATHFINDING_PROCESS_LOOP_RATE = 200;

/**
* @internal
*/
export const PATHFINDING_PROCESS_NEXT_DIRECTIINS_STRAIGHT = {
R: { x: 1, y: 0 }, // →
L: { x: -1, y: 0 }, // ←
D: { x: 0, y: 1 }, // ↓
U: { x: 0, y: -1 }, // ↑
};

/**
* @internal
*/
export const PATHFINDING_PROCESS_NEXT_DIRECTIINS_DIAGONAL = {
RD: { x: 1, y: 1 }, // ↘
RU: { x: 1, y: -1 }, // ↗
Expand Down
2 changes: 1 addition & 1 deletion src/process/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('PathfindingProcess', () => {

beforeAll(() => {
process = new PathfindingProcess();
process.addLayer('layer1', mockGrid());
process.createLayer('layer1', mockGrid());
});

afterAll(() => {
Expand Down
5 changes: 4 additions & 1 deletion src/process/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import type { PathfindingNode } from '../node';
import type { PathfindingTask } from '../task';
import type { PathfindingGrid, PathfindingPosition } from '../types';

/**
* @internal
*/
export class PathfindingProcess {
private readonly grids: Map<string, PathfindingGrid> = new Map();

Expand Down Expand Up @@ -44,7 +47,7 @@ export class PathfindingProcess {
}
}

public addLayer(idLayer: string, grid: PathfindingGrid): void {
public createLayer(idLayer: string, grid: PathfindingGrid): void {
this.grids.set(idLayer, grid);
this.weights.set(idLayer, []);
}
Expand Down
3 changes: 3 additions & 0 deletions src/task/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { PathfindingNode } from '../node';
import type { PathfindingTaskConfig, PathfindingTaskResult } from './types';
import type { PathfindingPosition } from '../types';

/**
* @internal
*/
export class PathfindingTask {
public readonly from: PathfindingPosition;

Expand Down
9 changes: 9 additions & 0 deletions src/task/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { PathfindingPosition } from '../types';

/**
* @internal
*/
export type PathfindingTaskConfig = {
idTask: number;
idLayer: string;
Expand All @@ -8,9 +11,15 @@ export type PathfindingTaskConfig = {
diagonals?: boolean;
};

/**
* @internal
*/
export type PathfindingTaskResult = {
path: PathfindingPosition[] | null;
weight: number;
};

/**
* @internal
*/
export type PathfindingTaskCallback = (result: PathfindingTaskResult) => void;
2 changes: 1 addition & 1 deletion src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ events.on(PathfindingEvent.SetWalkable, (payload) => {
});

events.on(PathfindingEvent.AddLayer, (payload) => {
process.addLayer(payload.idLayer, payload.grid);
process.createLayer(payload.idLayer, payload.grid);
});

events.on(PathfindingEvent.RemoveLayer, (payload) => {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"moduleResolution": "node",
"declaration": true,
"resolveJsonModule": true,
"stripInternal": true,
"esModuleInterop": true,
"strict": true,
"types": ["jest", "node"],
Expand Down

0 comments on commit 325e593

Please sign in to comment.