Skip to content

Commit

Permalink
parse in worker
Browse files Browse the repository at this point in the history
  • Loading branch information
sparkoo committed Dec 19, 2024
1 parent 7f1feb4 commit 41ac39d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 47 deletions.
57 changes: 10 additions & 47 deletions web/src/Player/PlayerApp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
29 changes: 29 additions & 0 deletions web/src/libs/worker.js
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 41ac39d

Please sign in to comment.