I am learning web worker and right now I am going through the problem of using await inside the onmessage. How can i do this?
import { Copc, Key } from "copc";
var nodePages, pages, receivedData;
async function load() {
let filename = "https://s3.amazonaws.com/data.entwine.io/millsite.copc.laz";
const copc = await Copc.create(filename);
let scale = copc.header.scale[0];
let [x_min, y_min, z_min, x_max, y_max, z_max] = copc.info.cube;
let width = Math.abs(x_max - x_min);
let center_x = (x_min + x_max) / 2;
let center_y = (y_min + y_max) / 2;
let center_z = (z_min + z_max) / 2;
receivedData = await Copc.loadHierarchyPage(
filename,
copc.info.rootHierarchyPage
);
nodePages = receivedData.nodes;
pages = receivedData.pages;
postMessage(200);
}
onmessage = function (message) {
let index = message.data;
let myRoot = nodePages[keyCountMap[m]];
const view = await Copc.loadPointDataView(filename, copc, myRoot);
};
and again there is another issue, the loadPointDataView function is asynchronous, how can i implement this in my webworker?
Any help please
This is my original code that i want to parallelize:
let filename = "https://s3.amazonaws.com/data.entwine.io/millsite.copc.laz";
const copc = await Copc.create(filename);
scale = copc.header.scale[0];
const { nodes: nodePages, pages: pages } = await Copc.loadHierarchyPage(
filename,
copc.info.rootHierarchyPage
);
for (let m = 0; m < keyCountMap.length; m += 2) {
let myRoot = nodePages[keyCountMap[m]];
const view = await Copc.loadPointDataView(filename, copc, myRoot);
let getters = ["X", "Y", "Z", "Intensity"].map(view.getter);
let chunkCount = 20;
let totalCalled = 0;
let innerPromises = [];
for (let j = 0; j < keyCountMap[m + 1]; j += chunkCount) {
readPoints(index + j, getters)
}
}
const readPoints = async (id, getters) => {
return new Promise((resolve, reject) => {
let returnPoint = getXyzi(id, getters);
positions.push(
returnPoint[0] - x_min - 0.5 * width,
returnPoint[1] - y_min - 0.5 * width,
returnPoint[2] - z_min - 0.5 * width
);
const vx = (returnPoint[3] / 65535) * 255;
color.setRGB(vx, vx, vx);
colors.push(color.r, color.g, color.b);
});
};
function getXyzi(index, getters) {
return getters.map((get) => get(index));
}