From 41ac39dae5811b071c1a7c788a66e46a2c9d406f Mon Sep 17 00:00:00 2001 From: Michal Vala Date: Thu, 19 Dec 2024 06:51:31 +0100 Subject: [PATCH] parse in worker --- web/src/Player/PlayerApp.jsx | 57 +++++++----------------------------- web/src/libs/worker.js | 29 ++++++++++++++++++ 2 files changed, 39 insertions(+), 47 deletions(-) create mode 100644 web/src/libs/worker.js diff --git a/web/src/Player/PlayerApp.jsx b/web/src/Player/PlayerApp.jsx index 1434bb7..589761e 100644 --- a/web/src/Player/PlayerApp.jsx +++ b/web/src/Player/PlayerApp.jsx @@ -5,70 +5,33 @@ import MessageBus from "./MessageBus.js"; import Player from "./Player.js"; import Map2d from "./map/Map2d.jsx"; import InfoPanel from "./panel/InfoPanel.jsx"; -import '../libs/wasm_exec.js'; +// import '../libs/wasm_exec.js'; import './protos/Message_pb.js' import DemoContext from "../context.js" +// import workerScript from "./worker.js"; export function PlayerApp() { const demoData = useContext(DemoContext); + const worker = new Worker("src/libs/worker.js"); const [messageBus] = useState(new MessageBus()) const [player] = useState(new Player(messageBus)) const [serverHost] = useState(window.location.host.includes("localhost") ? "http://localhost:8080" : ""); const [isWasmLoaded, setIsWasmLoaded] = useState(false) - useEffect(() => { - console.log("run run run", demoData) - - if (!isWasmLoaded) { - loadWasm(); - return - } + worker.onmessage = (e) => { + console.log("Message received from worker", e); + const msg = proto.Message.deserializeBinary(e.data).toObject() + messageBus.emit(msg) + }; + useEffect(() => { if (demoData.demoData) { - window.testt(demoData.demoData, async function (data) { - if (data instanceof Uint8Array) { - const msg = proto.Message.deserializeBinary(data).toObject() - messageBus.emit(msg) - } else { - console.log("[message] text data received from server, this is weird. We're using protobufs ?!?!?", data); - messageBus.emit(JSON.parse(data)) - } - }) - demoData.setDemoData(null) + setTimeout(() => worker.postMessage(demoData.demoData), 1000) } - - // const urlParams = new URLSearchParams(window.location.search); - // const channel = new BroadcastChannel(urlParams.get("uuid")); - // channel.onmessage = async (event) => { - // console.log("received", event, isWasmLoaded) - // await window.testt(event.data, function (data) { - // if (data instanceof Uint8Array) { - // const msg = proto.Message.deserializeBinary(data).toObject() - // setMessage("123") - // console.log("huha?") - // messageBus.emit(msg) - // } else { - // console.log("[message] text data received from server, this is weird. We're using protobufs ?!?!?", data); - // messageBus.emit(JSON.parse(data)) - // } - // }) - // }; messageBus.listen([13], function (msg) { alert(msg.message) - // window.testt(byteArray) }) - // Connect(this.messageBus) - - async function loadWasm() { - const go = new window.Go(); - WebAssembly.instantiateStreaming(fetch(serverHost + "/wasm"), go.importObject) - .then((result) => { - go.run(result.instance); - console.log("should be loaded now") - setIsWasmLoaded(true) - }); - } }, [isWasmLoaded]) return ( diff --git a/web/src/libs/worker.js b/web/src/libs/worker.js new file mode 100644 index 0000000..55e7cb3 --- /dev/null +++ b/web/src/libs/worker.js @@ -0,0 +1,29 @@ +const serverHost = globalThis.location.host.includes("localhost") ? "http://localhost:8080" : ""; + +importScripts('wasm_exec.js'); + +onmessage = (event) => { + if (event.data instanceof Uint8Array) { + globalThis.testt(event.data, async function (data) { + if (data instanceof Uint8Array) { + postMessage(data) + // const msg = proto.Message.deserializeBinary(data).toObject() + // messageBus.emit(msg) + } else { + console.log("[message] text data received from server, this is weird. We're using protobufs ?!?!?", data); + postMessage(JSON.parse(data)) + } + }) + } +} + +async function loadWasm() { + console.log("hus", serverHost + "/wasm") + const go = new globalThis.Go(); + await WebAssembly.instantiateStreaming(fetch(serverHost + "/wasm"), go.importObject) + .then((result) => { + go.run(result.instance); + console.log("should be loaded now") + }); +} +loadWasm();