0

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));
  }
Pravin Poudel
  • 1,433
  • 3
  • 16
  • 38
  • I don't see any nested for loops here, but it doesn't look like a use case for webworker, parallelize promises instead – Max Feb 02 '23 at 22:39
  • @Max Hi Max Sorry for not clear post, i changed the problem statement and in this post i want to find a way with which i can do async operation to load data which is sent by the main thread as a number to web worker – Pravin Poudel Feb 02 '23 at 22:51
  • 1
    if you want to make it work with webworkers you would have to make initial implementation yourself and then people would help you fix the errors; they will not write the implementation for you. More importantly webworkers are for computationally intensive tasks (fetching data, i/o is not computationally expensive), your problem is not computationally expensive, it's just running "waiting" tasks in parallel and then waiting for them to execute, which is a use case for Promises – Max Feb 02 '23 at 23:00
  • @Max after it read the file, it has to loop for like 60,000 times to read the data so it is computationally expensive job – Pravin Poudel Feb 02 '23 at 23:18
  • I wrote the common code and tried to implement that in WebWorker because it takes so much time to get all the data – Pravin Poudel Feb 02 '23 at 23:21

0 Answers0