diff options
Diffstat (limited to 'web/worker.mjs')
| -rw-r--r-- | web/worker.mjs | 89 |
1 files changed, 77 insertions, 12 deletions
diff --git a/web/worker.mjs b/web/worker.mjs index dca980e..ceceaec 100644 --- a/web/worker.mjs +++ b/web/worker.mjs | |||
| @@ -8,8 +8,10 @@ function log(message) { | |||
| 8 | nissy.setLogger(nissy._addCallbackFunction(log)); | 8 | nissy.setLogger(nissy._addCallbackFunction(log)); |
| 9 | 9 | ||
| 10 | const commands = [ | 10 | const commands = [ |
| 11 | { name: "solve", exec: solve }, | 11 | { name: "load solver data", exec: loadSolverDataFromStorage }, |
| 12 | { name: "download solver data", exec: downloadSolverData }, | 12 | { name: "download solver data", exec: downloadSolverData }, |
| 13 | { name: "generate solver data", exec: generateSolverData }, | ||
| 14 | { name: "solve", exec: solve }, | ||
| 13 | ]; | 15 | ]; |
| 14 | 16 | ||
| 15 | // Message structure: | 17 | // Message structure: |
| @@ -25,6 +27,7 @@ onmessage = (event) => { | |||
| 25 | for (var i = 0; i < commands.length; i++) { | 27 | for (var i = 0; i < commands.length; i++) { |
| 26 | if (commands[i].name == event.data.command) { | 28 | if (commands[i].name == event.data.command) { |
| 27 | commands[i].exec(event.data.id, event.data.arg); | 29 | commands[i].exec(event.data.id, event.data.arg); |
| 30 | console.log("[worker] Received '" + commands[i].name + "'"); | ||
| 28 | return; | 31 | return; |
| 29 | } | 32 | } |
| 30 | } | 33 | } |
| @@ -32,10 +35,41 @@ onmessage = (event) => { | |||
| 32 | log("[nissy worker] unknown command " + event.data.command); | 35 | log("[nissy worker] unknown command " + event.data.command); |
| 33 | }; | 36 | }; |
| 34 | 37 | ||
| 38 | // Load solver data from storage. | ||
| 39 | // Argument: string (the name of the solver) | ||
| 40 | async function loadSolverDataFromStorage(id, solver) { | ||
| 41 | const async_return = (success, message = "") => postMessage({ | ||
| 42 | command: "load solver data", | ||
| 43 | id: id, | ||
| 44 | arg: { | ||
| 45 | success: success, | ||
| 46 | message: message | ||
| 47 | } | ||
| 48 | }); | ||
| 49 | |||
| 50 | if (!(await nissy.isSolverAvailable(solver))) { | ||
| 51 | async_return(false, "Error: solver " + arg.solver + | ||
| 52 | " is not available in this version of nissy."); | ||
| 53 | return; | ||
| 54 | } | ||
| 55 | |||
| 56 | if (await nissy.isSolverLoaded(solver)) { | ||
| 57 | async_return(true); | ||
| 58 | return; | ||
| 59 | } | ||
| 60 | |||
| 61 | if (await nissy.initSolverFromStorage(solver)) { | ||
| 62 | async_return(true); | ||
| 63 | } else { | ||
| 64 | async_return(false, "Could not read data for " + solver + | ||
| 65 | " from local storage"); | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 35 | // Download solver data. | 69 | // Download solver data. |
| 36 | // Argument: string | 70 | // Argument: string (the name of the solver) |
| 37 | async function downloadSolverData(id, solver) { | 71 | async function downloadSolverData(id, solver) { |
| 38 | const downloadSolverDataReturn = (success, message = "") => postMessage({ | 72 | const async_return = (success, message = "") => postMessage({ |
| 39 | command: "download solver data", | 73 | command: "download solver data", |
| 40 | id: id, | 74 | id: id, |
| 41 | arg: { | 75 | arg: { |
| @@ -44,10 +78,41 @@ async function downloadSolverData(id, solver) { | |||
| 44 | } | 78 | } |
| 45 | }); | 79 | }); |
| 46 | 80 | ||
| 47 | if (await nissy.initSolverDownload(arg.solver, "/tables")) { | 81 | if (!(await nissy.isSolverAvailable(solver))) { |
| 48 | downloadSolverDataReturn(true); | 82 | async_return(false, "Error: solver " + arg.solver + |
| 83 | " is not available in this version of nissy."); | ||
| 84 | return; | ||
| 85 | } | ||
| 86 | |||
| 87 | if (await nissy.initSolverDownload(solver, "/tables")) { | ||
| 88 | async_return(true); | ||
| 89 | } else { | ||
| 90 | async_return(false, "Error retrieving solver data"); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | // Generate solver data locally. | ||
| 95 | // Argument: string (the name of the solver) | ||
| 96 | async function generateSolverData(id, solver) { | ||
| 97 | const async_return = (success, message = "") => postMessage({ | ||
| 98 | command: "generate solver data", | ||
| 99 | id: id, | ||
| 100 | arg: { | ||
| 101 | success: success, | ||
| 102 | message: message | ||
| 103 | } | ||
| 104 | }); | ||
| 105 | |||
| 106 | if (!(await nissy.isSolverAvailable(solver))) { | ||
| 107 | async_return(false, "Error: solver " + arg.solver + | ||
| 108 | " is not available in this version of nissy."); | ||
| 109 | return; | ||
| 110 | } | ||
| 111 | |||
| 112 | if (await nissy.initSolverGenerate(solver)) { | ||
| 113 | async_return(true); | ||
| 49 | } else { | 114 | } else { |
| 50 | downloadSolverDataReturn(false, "Error retrieving the solver data"); | 115 | async_return(false, "Error generating solver data"); |
| 51 | } | 116 | } |
| 52 | } | 117 | } |
| 53 | 118 | ||
| @@ -62,7 +127,7 @@ async function downloadSolverData(id, solver) { | |||
| 62 | // threads: number | 127 | // threads: number |
| 63 | // } | 128 | // } |
| 64 | async function solve(id, arg) { | 129 | async function solve(id, arg) { |
| 65 | const solveReturn = (success, solutions = [], message = "") => postMessage({ | 130 | const async_return = (success, solutions = [], message = "") => postMessage({ |
| 66 | command: "solve", | 131 | command: "solve", |
| 67 | id: id, | 132 | id: id, |
| 68 | arg: { | 133 | arg: { |
| @@ -73,14 +138,14 @@ async function solve(id, arg) { | |||
| 73 | }); | 138 | }); |
| 74 | 139 | ||
| 75 | if (!(await nissy.isSolverAvailable(arg.solver))) { | 140 | if (!(await nissy.isSolverAvailable(arg.solver))) { |
| 76 | solveReturn(false, [], "Error: solver " + arg.solver + | 141 | async_return(false, [], "Error: solver " + arg.solver + |
| 77 | " is not available in this version of nissy."); | 142 | " is not available in this version of nissy."); |
| 78 | return; | 143 | return; |
| 79 | } | 144 | } |
| 80 | 145 | ||
| 81 | if (!(await nissy.isSolverLoaded(arg.solver)) && | 146 | if (!(await nissy.isSolverLoaded(arg.solver)) && |
| 82 | !(await nissy.initSolverFromStorage(arg.solver))) { | 147 | !(await nissy.initSolverFromStorage(arg.solver))) { |
| 83 | solveReturn(false, [], "Error: solver " + arg.solver + " has not been " + | 148 | async_return(false, [], "Error: solver " + arg.solver + " has not been " + |
| 84 | "loaded. Its data must be donwloaded or generated before using it."); | 149 | "loaded. Its data must be donwloaded or generated before using it."); |
| 85 | return; | 150 | return; |
| 86 | } | 151 | } |
| @@ -106,9 +171,9 @@ async function solve(id, arg) { | |||
| 106 | //TODO: add solution lenght? | 171 | //TODO: add solution lenght? |
| 107 | var solutions = result.solutions == "" ? [] : result.solutions.split("\n"); | 172 | var solutions = result.solutions == "" ? [] : result.solutions.split("\n"); |
| 108 | solutions.pop(); // Solver always returns string ending in newline | 173 | solutions.pop(); // Solver always returns string ending in newline |
| 109 | solveReturn(true, solutions); | 174 | async_return(true, solutions); |
| 110 | } else { | 175 | } else { |
| 111 | solveReturn(false, [], | 176 | async_return(false, [], |
| 112 | "Error while solving (error " + result.err.value + ")"); | 177 | "Error while solving (error " + result.err.value + ")"); |
| 113 | } | 178 | } |
| 114 | }; | 179 | }; |
