3 const MAX_BATCH_SIZE
= 10;
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
;
21 if(navigator
.userAgent
.indexOf("Firefox") != -1 ) {
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
);
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
);
60 for (let worker
of this.workers
) {