From 99a804cc668d29a7d587114a7b7018b41b7ff027 Mon Sep 17 00:00:00 2001 From: Niko PLP Date: Thu, 7 Sep 2023 23:57:02 +0300 Subject: [PATCH] fix worker race condition issue --- ng-app/src/lib/Login.svelte | 29 +++++++++++++++++++-------- ng-app/src/worker.js | 39 ++++++++++++++++++++----------------- 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/ng-app/src/lib/Login.svelte b/ng-app/src/lib/Login.svelte index db16e09..858cebe 100644 --- a/ng-app/src/lib/Login.svelte +++ b/ng-app/src/lib/Login.svelte @@ -14,7 +14,7 @@ import { onMount, createEventDispatcher, tick } from "svelte"; import ng from "../api"; import { emoji_cat, emojis, load_svg } from "../wallet_emojis"; - import Worker from "../worker.js?worker"; + //import Worker from "../worker.js?worker&inline"; export let wallet; let tauri_platform = import.meta.env.TAURI_PLATFORM; @@ -131,14 +131,27 @@ id: secret_wallet.V0.wallet_id, }); } else { - //let worker_file = await import("../worker.js?worker"); - //const myWorker = new worker_file.default(); - const myWorker = new Worker(); - myWorker.postMessage({ wallet, pazzle, pin_code }); - + let worker_import = await import("../worker.js?worker&inline"); + const myWorker = new worker_import.default(); + //const myWorker = new Worker(); + myWorker.onerror = (e) => { + console.error(e); + error = e; + step = "end"; + dispatch("error", { error: e }); + }; + myWorker.onmessageerror = (e) => { + console.error(e); + error = e; + step = "end"; + dispatch("error", { error: e }); + }; myWorker.onmessage = (msg) => { - console.log("Message received from worker", msg.data); - if (msg.data.success) { + //console.log("Message received from worker", msg.data); + if (msg.data.loaded) { + myWorker.postMessage({ wallet, pazzle, pin_code }); + //console.log("postMessage"); + } else if (msg.data.success) { step = "end"; dispatch("opened", { wallet: msg.data.success, diff --git a/ng-app/src/worker.js b/ng-app/src/worker.js index c5804fd..2f5a41f 100644 --- a/ng-app/src/worker.js +++ b/ng-app/src/worker.js @@ -1,21 +1,24 @@ -import { - default as ng, - } from "./api"; +import * as api from "ng-sdk-js"; +import { default as ng } from "./api"; - onmessage = (e) => { - console.log("Message received by worker", e.data); - (async function() { - try { - let secret_wallet = await ng.wallet_open_wallet_with_pazzle( - e.data.wallet, - e.data.pazzle, - e.data.pin_code - ); - postMessage({success:secret_wallet}); - } catch (e) { - postMessage({error:e}); - } - })(); - }; +//console.log("loaded worker"); + +onmessage = (e) => { + //console.log("Message received by worker", e.data); + (async function() { + try { + let secret_wallet = await ng.wallet_open_wallet_with_pazzle( + e.data.wallet, + e.data.pazzle, + e.data.pin_code + ); + postMessage({success:secret_wallet}); + } catch (e) { + postMessage({error:e}); + } + })(); +}; + +postMessage({loaded:true}); \ No newline at end of file