-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
main.js
79 lines (65 loc) · 2.09 KB
/
main.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
import { dotnet, exit } from './_framework/dotnet.js'
let progressElement = null;
function updateProgress(status) {
if (progressElement) {
progressElement.innerText = status;
} else {
console.log("Progress: " + status);
}
}
const assemblyName = "Wasm.Browser.Threads.Sample.dll";
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function Run(exports, N) {
while (true) {
await delay(50);
const p = exports.Sample.Test.Progress();
if (p === 0)
break;
}
const answer = exports.Sample.Test.GetAnswer();
document.getElementById("out").innerText = `Fib(${N}) = ${answer}`;
}
async function doMathSlowly(exports) {
progressElement = document.getElementById("progressElement");
const N = parseInt(document.getElementById("inputN").value);
exports.Sample.Test.Start(N);
await Run(exports, N);
}
function setEditable(inputElement, isEditable) {
inputElement.disabled = !isEditable;
}
function onInputValueChanged(exports, inputElement) {
async function handler() {
setEditable(inputElement, false);
await doMathSlowly(exports);
setEditable(inputElement, true);
}
return handler;
}
try {
const inputElement = document.getElementById("inputN");
const { setModuleImports, getAssemblyExports, runMain } = await dotnet
.withEnvironmentVariable("MONO_LOG_LEVEL", "debug")
.withElementOnExit()
.withExitCodeLogging()
.create();
setModuleImports("main.js", {
Sample: {
Test: {
updateProgress
}
}
});
const exports = await getAssemblyExports(assemblyName);
await doMathSlowly(exports);
setEditable(inputElement, true);
inputElement.addEventListener("change", onInputValueChanged(exports, inputElement));
let exit_code = await runMain(assemblyName, []);
exit(exit_code);
} catch (err) {
exit(2, err);
}