-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker.js
33 lines (28 loc) · 943 Bytes
/
worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
'use strict';
importScripts('image-processor/pkg/image_processor.js');
const initWasm = wasm_bindgen;
const {generate_layout, render_specific_layout} = wasm_bindgen;
initWasm('image-processor/pkg/image_processor_bg.wasm').then(() => {
postMessage(['ready'])
});
onmessage = function(event) {
const [action, ...payload] = event.data;
console.log('Worker received a message', {action, payload});
switch (action) {
case 'generate_layout': {
const imageArrays = payload[0];
const seed = payload[1];
const result = generate_layout(imageArrays, seed);
postMessage(result, [result.buffer]);
break;
}
case 'render_specific_layout': {
const [layoutBlueprint, imageArrays] = payload;
const result = render_specific_layout(layoutBlueprint, imageArrays);
postMessage(result, [result.buffer]);
break;
}
default:
throw new Error(`Unknown action: ${action}`)
}
}