-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathworker.js
50 lines (50 loc) · 1.69 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
importScripts("wasm_exec.js")
//expects an array of uintArray if merge or compress, will only compress first.
self.addEventListener('message', function (e) {
if (!WebAssembly.instantiateStreaming) { // polyfill
WebAssembly.instantiateStreaming = async (resp, importObject) => {
const source = await (await resp).arrayBuffer()
return await WebAssembly.instantiate(source, importObject)
}
}
const go = new Go();
WebAssembly.instantiateStreaming(fetch("pdfcomprezzor.wasm"), go.importObject).then((result) => {
go.run(result.instance);
var a = performance.now();
if (e.data.action == "merge") {
console.log(e.data.array)
bytesCount = merge(e.data.array, (err, message) => {
self.postMessage({
err,
message,
type: "log"
})
});
bytes = new Uint8Array(bytesCount);
readBack(bytes);
var b = performance.now();
self.postMessage({
result: bytes,
type: "result",
time: b - a
});
return;
} else {
let bytesCount = compress(e.data.array[0], (err, message) => {
self.postMessage({
err,
message,
type: "log"
})
});
bytes = new Uint8Array(bytesCount);
readBack(bytes);
var b = performance.now();
self.postMessage({
result: bytes,
type: "result",
time: b - a
});
}
});
}, false);