Fixes mobile with lidar changes
[wrfxweb.git] / fdds / threadManager.js
blob60a91920f0633c6f743475e36bc2fbe49dcaacfd
1 'use strict';
3 const MAX_BATCH_SIZE = 10;
4 const TIMEOUT_MS = 80;
6 async function loadImagesInBatches(imageIndex=0, batchSize, imageInfos, worker) {
7 let batchLimit = Math.min(imageIndex + batchSize, imageInfos.length - 1);
8 for (imageIndex; imageIndex <= batchLimit; imageIndex++) {
9 let imageInfo = imageInfos[imageIndex];
10 worker.postMessage(imageInfo);
12 if (imageIndex < imageInfos.length) {
13 setTimeout(loadImagesInBatches, TIMEOUT_MS, imageIndex, batchSize, imageInfos, worker);
17 export class ThreadManager {
18 constructor(updateCallback) {
19 this.updateCallback = updateCallback;
20 this.N_WORKERS = 2;
21 if(navigator.userAgent.indexOf("Firefox") != -1 ) {
22 this.N_WORKERS = 1;
24 this.workers = [];
27 loadImages(loadFirst, loadLater) {
28 let firstSize = Math.ceil(loadFirst.length / this.N_WORKERS);
29 let laterSize = Math.ceil(loadLater.length / this.N_WORKERS);
30 this.cancelCurrentLoad();
31 for (let i = 0; i < this.N_WORKERS; i++) {
32 let iFirst = i * firstSize;
33 let jFirst = Math.min((i+1) * firstSize, loadFirst.length);
34 let firstBatch = loadFirst.slice(iFirst, jFirst);
36 let iLater = i * laterSize;
37 let jLater = Math.min((i+1) * laterSize, loadLater.length);
38 let laterBatch = loadLater.slice(iLater, jLater);
40 let worker = this.startThread(firstBatch, laterBatch);
41 this.workers.push(worker);
45 startThread(loadFirst, loadLater) {
46 let worker = new Worker('imageLoadingWorker.js');
47 worker.addEventListener('message', async event => {
48 const imageData = event.data;
49 this.updateCallback(imageData);
50 });
52 let batchSize = Math.min(Math.ceil(loadFirst.length / 20), MAX_BATCH_SIZE);
53 loadImagesInBatches(0, batchSize, loadFirst, worker);
54 loadImagesInBatches(0, batchSize, loadLater, worker);
56 return worker;
59 cancelCurrentLoad() {
60 for (let worker of this.workers) {
61 worker.terminate();
63 this.workers = [];