Change messaginging, allow adding more message types later

This commit is contained in:
Martin Asprusten
2026-08-29 14:14:36 +02:00
parent f3a1f058c0
commit 78c4050166
3 changed files with 52 additions and 30 deletions
+14 -8
View File
@@ -7,7 +7,7 @@ import { createUrl, parseUrl } from './url/UrlHandler.js';
import { wktToGeoJSON } from '@terraformer/wkt'; import { wktToGeoJSON } from '@terraformer/wkt';
import PPENHandler from './files/ppenhandler.js'; import PPENHandler from './files/ppenhandler.js';
import type { GeoJsonObject } from 'geojson'; import type { GeoJsonObject } from 'geojson';
import type { SearchStatusMessage, StartSearchMessage } from './worker/messages.js'; import type { FromWorkerMessage, ToWorkerMessage } from './worker/messages.js';
// Outside influences // Outside influences
let mapHandler = new MapHandler(); let mapHandler = new MapHandler();
@@ -286,10 +286,13 @@ function updateState() {
// In case of messages from the web worker, do work // In case of messages from the web worker, do work
worker.onmessage = e => { worker.onmessage = e => {
let message = e.data as SearchStatusMessage; let fromWorker = e.data as FromWorkerMessage;
state.foundPath = message.path; let message = fromWorker.message;
state.calculating = false; if (message.type == 'SearchFinished') {
updateState(); state.foundPath = message.path;
state.calculating = false;
updateState();
}
} }
function calculateRoute(): void { function calculateRoute(): void {
@@ -326,9 +329,12 @@ function calculateRoute(): void {
weights.push(weightsRow); weights.push(weightsRow);
} }
let startSearchMessage: StartSearchMessage = { let startSearchMessage: ToWorkerMessage = {
weights: weights message: {
}; type: "StartSearch",
weights: weights
}
}
worker.postMessage(startSearchMessage); worker.postMessage(startSearchMessage);
} }
+11 -1
View File
@@ -1,7 +1,17 @@
export interface StartSearchMessage { export interface StartSearchMessage {
type: 'StartSearch'
weights: number[][]; weights: number[][];
} }
export interface SearchStatusMessage { export interface SearchFinishedMessage {
type: 'SearchFinished'
path: number[]; path: number[];
}
export interface ToWorkerMessage {
message: StartSearchMessage
}
export interface FromWorkerMessage {
message: SearchFinishedMessage
} }
+27 -21
View File
@@ -1,32 +1,38 @@
import Module, { type MainModule } from '../../native/salesman.js'; import Module, { type MainModule } from '../../native/salesman.js';
import type { SearchStatusMessage, StartSearchMessage } from "./messages" import type { FromWorkerMessage, ToWorkerMessage } from "./messages"
let modulePromise: Promise<MainModule> = Module(); let modulePromise: Promise<MainModule> = Module();
onmessage = async (e) => { onmessage = async (e) => {
let module = await modulePromise; let module = await modulePromise;
let message = e.data as StartSearchMessage; let toWorker = e.data as ToWorkerMessage;
let nativeWeights = new module.Weights; if (toWorker.message.type == 'StartSearch') {
message.weights.forEach(weightsRow => { let message = toWorker.message;
let nativeWeightsRow = new module.WeightsRow(); let nativeWeights = new module.Weights;
weightsRow.forEach(weight => { message.weights.forEach(weightsRow => {
nativeWeightsRow.push_back(weight); let nativeWeightsRow = new module.WeightsRow();
weightsRow.forEach(weight => {
nativeWeightsRow.push_back(weight);
});
nativeWeights.push_back(nativeWeightsRow);
}); });
nativeWeights.push_back(nativeWeightsRow);
});
let path = module.findShortestPath(nativeWeights); let path = module.findShortestPath(nativeWeights);
let foundPath = []; let foundPath = [];
for (var i = 0; i < path.size(); i++) { for (var i = 0; i < path.size(); i++) {
let pointIndex = path.get(i); let pointIndex = path.get(i);
if (typeof pointIndex != 'undefined') { if (typeof pointIndex != 'undefined') {
foundPath.push(pointIndex); foundPath.push(pointIndex);
} }
}; };
let finishedMessage: SearchStatusMessage = { let finishedMessage: FromWorkerMessage = {
path: foundPath message: {
}; type: 'SearchFinished',
postMessage(finishedMessage); path: foundPath
}
};
postMessage(finishedMessage);
}
} }