use worker to avoid freezing
This commit is contained in:
parent
6175224c9b
commit
fcdf795387
15
main.js
15
main.js
|
|
@ -16,7 +16,11 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|||
*/
|
||||
import { tokenize } from "./modules/lexer.js";
|
||||
import { shunt } from "./modules/parser.js";
|
||||
import { compute } from "./modules/compute.js";
|
||||
|
||||
const worker = new Worker("./modules/compute.js");
|
||||
worker.addEventListener("message", (message) => {
|
||||
document.querySelector("#result").value = message.data;
|
||||
});
|
||||
|
||||
function evaluate() {
|
||||
const expr = document.querySelector("#expr");
|
||||
|
|
@ -44,13 +48,8 @@ function evaluate() {
|
|||
|
||||
console.log(queue);
|
||||
|
||||
try {
|
||||
result.value = compute(queue, m);
|
||||
} catch(e) {
|
||||
result.value = e;
|
||||
console.log(e);
|
||||
return;
|
||||
}
|
||||
worker.postMessage({command: "compute", queue, m});
|
||||
result.value = "calculating...";
|
||||
}
|
||||
|
||||
const expr = document.querySelector("#expr");
|
||||
|
|
|
|||
|
|
@ -14,7 +14,17 @@ GNU General Public License for more details.
|
|||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import { modinv, modpow, modsqrt, ord } from "./math.js";
|
||||
importScripts("./math.js")
|
||||
|
||||
addEventListener("message", (message) => {
|
||||
if (message.data.command === "compute") {
|
||||
try {
|
||||
compute(message.data.queue, message.data.m);
|
||||
} catch(e) {
|
||||
postMessage(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
function binaryOpPop(stack) {
|
||||
const b = stack.pop();
|
||||
|
|
@ -88,7 +98,5 @@ function compute(queue, modulus) {
|
|||
result += modulus;
|
||||
}
|
||||
|
||||
return result;
|
||||
postMessage(result);
|
||||
}
|
||||
|
||||
export { compute };
|
||||
|
|
|
|||
|
|
@ -220,5 +220,3 @@ function ord(n, modulus) {
|
|||
|
||||
return k;
|
||||
}
|
||||
|
||||
export { modsqrt, modinv, modpow, ord };
|
||||
|
|
|
|||
Loading…
Reference in New Issue