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 PPENHandler from './files/ppenhandler.js';
import type { GeoJsonObject } from 'geojson';
import type { SearchStatusMessage, StartSearchMessage } from './worker/messages.js';
import type { FromWorkerMessage, ToWorkerMessage } from './worker/messages.js';
// Outside influences
let mapHandler = new MapHandler();
@@ -286,10 +286,13 @@ function updateState() {
// In case of messages from the web worker, do work
worker.onmessage = e => {
let message = e.data as SearchStatusMessage;
state.foundPath = message.path;
state.calculating = false;
updateState();
let fromWorker = e.data as FromWorkerMessage;
let message = fromWorker.message;
if (message.type == 'SearchFinished') {
state.foundPath = message.path;
state.calculating = false;
updateState();
}
}
function calculateRoute(): void {
@@ -326,9 +329,12 @@ function calculateRoute(): void {
weights.push(weightsRow);
}
let startSearchMessage: StartSearchMessage = {
weights: weights
};
let startSearchMessage: ToWorkerMessage = {
message: {
type: "StartSearch",
weights: weights
}
}
worker.postMessage(startSearchMessage);
}
+11 -1
View File
@@ -1,7 +1,17 @@
export interface StartSearchMessage {
type: 'StartSearch'
weights: number[][];
}
export interface SearchStatusMessage {
export interface SearchFinishedMessage {
type: 'SearchFinished'
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 type { SearchStatusMessage, StartSearchMessage } from "./messages"
import type { FromWorkerMessage, ToWorkerMessage } from "./messages"
let modulePromise: Promise<MainModule> = Module();
onmessage = async (e) => {
let module = await modulePromise;
let message = e.data as StartSearchMessage;
let nativeWeights = new module.Weights;
message.weights.forEach(weightsRow => {
let nativeWeightsRow = new module.WeightsRow();
weightsRow.forEach(weight => {
nativeWeightsRow.push_back(weight);
let toWorker = e.data as ToWorkerMessage;
if (toWorker.message.type == 'StartSearch') {
let message = toWorker.message;
let nativeWeights = new module.Weights;
message.weights.forEach(weightsRow => {
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 = [];
for (var i = 0; i < path.size(); i++) {
let pointIndex = path.get(i);
if (typeof pointIndex != 'undefined') {
foundPath.push(pointIndex);
}
};
let foundPath = [];
for (var i = 0; i < path.size(); i++) {
let pointIndex = path.get(i);
if (typeof pointIndex != 'undefined') {
foundPath.push(pointIndex);
}
};
let finishedMessage: SearchStatusMessage = {
path: foundPath
};
postMessage(finishedMessage);
let finishedMessage: FromWorkerMessage = {
message: {
type: 'SearchFinished',
path: foundPath
}
};
postMessage(finishedMessage);
}
}