diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-05-27 16:45:06 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-05-27 16:45:06 +0200 |
| commit | cf2896324e60e2dc3890c658378499383100e79c (patch) | |
| tree | ad81c2dd806f561857824c5f1d34b80f8e20f86b /web/http/nissyapp.mjs | |
| parent | 7c934801f88c640970ad41b5ddd39f4e39609f28 (diff) | |
| download | nissy-core-cf2896324e60e2dc3890c658378499383100e79c.tar.gz nissy-core-cf2896324e60e2dc3890c658378499383100e79c.zip | |
Big progress with web version
Diffstat (limited to 'web/http/nissyapp.mjs')
| -rw-r--r-- | web/http/nissyapp.mjs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/web/http/nissyapp.mjs b/web/http/nissyapp.mjs new file mode 100644 index 0000000..23e74d0 --- /dev/null +++ b/web/http/nissyapp.mjs | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | // Run ./build web from the main folder before running this example. | ||
| 2 | // The necessary modules (including worker.mjs) will be built and / or moved | ||
| 3 | // to this folder when running ./build web. | ||
| 4 | |||
| 5 | var solveButton = document.getElementById("solveButton"); | ||
| 6 | var scrField = document.getElementById("scrambleText"); | ||
| 7 | var resultsLabel = document.getElementById("resultsLabel"); | ||
| 8 | var resultsText = document.getElementById("results"); | ||
| 9 | var solverSelector = document.getElementById("solverSelector"); | ||
| 10 | |||
| 11 | var lastCallbackId = 0; | ||
| 12 | var callbacks = new Map(); // Values: { f: function, arg: object } | ||
| 13 | var worker = new Worker("./worker.mjs", { type: "module" }); | ||
| 14 | worker.onmessage = (event) => { | ||
| 15 | if (event.data.command == "log") { | ||
| 16 | console.log(event.data.object); // TODO | ||
| 17 | } else if (!callbacks.has(event.data.id)) { | ||
| 18 | console.log("[nissy app] Unknown callback " + event.data.id + | ||
| 19 | " for command " + event.data.command); | ||
| 20 | } else { | ||
| 21 | var callback = callbacks.get(event.data.id); | ||
| 22 | callback.f(callback.arg, event.data.arg); | ||
| 23 | } | ||
| 24 | }; | ||
| 25 | |||
| 26 | function updateResults(label, results, buttonState) { | ||
| 27 | resultsLabel.innerText = label; | ||
| 28 | resultsText.innerText = results; | ||
| 29 | solveButton.disable = !buttonState; | ||
| 30 | } | ||
| 31 | |||
| 32 | solveButton.addEventListener("click", () => { | ||
| 33 | updateResults("Loading solutions...", "", false); | ||
| 34 | const callbackId = ++lastCallbackId; | ||
| 35 | callbacks.set(callbackId, { | ||
| 36 | f: (callbackArg, solveResult) => { | ||
| 37 | if (solveResult.success) { | ||
| 38 | const n = solveResult.solutions.length; | ||
| 39 | const label = n == 0 ? "No solution found" : | ||
| 40 | ("Found " + n + " solution" + (n == 1 ? "" : "s")) + ":"; | ||
| 41 | const text = solveResult.solutions.join("\n"); | ||
| 42 | updateResults(label, text, true); | ||
| 43 | } else { | ||
| 44 | updateResults("Unexpected error while solving!", "", true); | ||
| 45 | } | ||
| 46 | }, | ||
| 47 | arg: "" // Currently unused | ||
| 48 | }); | ||
| 49 | worker.postMessage({ | ||
| 50 | command: "solve", | ||
| 51 | id: callbackId, | ||
| 52 | arg: { | ||
| 53 | solver: solverSelector.options[solverSelector.selectedIndex].value, | ||
| 54 | scramble: scrField.value, | ||
| 55 | minmoves: 0, | ||
| 56 | maxmoves: 20, | ||
| 57 | maxsolutions: 1, | ||
| 58 | optimal: 20, | ||
| 59 | threads: window.navigator.hardwareConcurrency, | ||
| 60 | } | ||
| 61 | }); | ||
| 62 | }); | ||
