-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathworker.js
More file actions
48 lines (44 loc) · 1.52 KB
/
worker.js
File metadata and controls
48 lines (44 loc) · 1.52 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
import { dotnet } from './_framework/dotnet.js'
let assemblyExports = null;
let startupError = undefined;
try {
const { getAssemblyExports, getConfig } = await dotnet.create();
const config = getConfig();
assemblyExports = await getAssemblyExports(config.mainAssemblyName);
}
catch (err) {
startupError = err.message;
}
self.addEventListener('message', async function(e) {
try {
if (!assemblyExports) {
throw new Error(startupError || "worker exports not loaded");
}
let result = null;
switch (e.data.command) {
case "generateQR":
const size = Number(e.data.size);
const text = e.data.text;
if (size === undefined || text === undefined)
new Error("Inner error, got empty QR generation data from React");
result = assemblyExports.QRGenerator.Generate(text, size);
break;
default:
throw new Error("Unknown command: " + e.data.command);
}
self.postMessage({
command: "response",
requestId: e.data.requestId,
result,
});
}
catch (err) {
self.postMessage({
command: "response",
requestId: e.data.requestId,
error: err.message,
});
}
}, false);