Skip to content

Commit 41ac39d

Browse files
committed
parse in worker
1 parent 7f1feb4 commit 41ac39d

File tree

2 files changed

+39
-47
lines changed

2 files changed

+39
-47
lines changed

web/src/Player/PlayerApp.jsx

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,70 +5,33 @@ import MessageBus from "./MessageBus.js";
55
import Player from "./Player.js";
66
import Map2d from "./map/Map2d.jsx";
77
import InfoPanel from "./panel/InfoPanel.jsx";
8-
import '../libs/wasm_exec.js';
8+
// import '../libs/wasm_exec.js';
99
import './protos/Message_pb.js'
1010
import DemoContext from "../context.js"
11+
// import workerScript from "./worker.js";
1112

1213
export function PlayerApp() {
1314
const demoData = useContext(DemoContext);
15+
const worker = new Worker("src/libs/worker.js");
1416

1517
const [messageBus] = useState(new MessageBus())
1618
const [player] = useState(new Player(messageBus))
1719
const [serverHost] = useState(window.location.host.includes("localhost") ? "http://localhost:8080" : "");
1820
const [isWasmLoaded, setIsWasmLoaded] = useState(false)
1921

20-
useEffect(() => {
21-
console.log("run run run", demoData)
22-
23-
if (!isWasmLoaded) {
24-
loadWasm();
25-
return
26-
}
22+
worker.onmessage = (e) => {
23+
console.log("Message received from worker", e);
24+
const msg = proto.Message.deserializeBinary(e.data).toObject()
25+
messageBus.emit(msg)
26+
};
2727

28+
useEffect(() => {
2829
if (demoData.demoData) {
29-
window.testt(demoData.demoData, async function (data) {
30-
if (data instanceof Uint8Array) {
31-
const msg = proto.Message.deserializeBinary(data).toObject()
32-
messageBus.emit(msg)
33-
} else {
34-
console.log("[message] text data received from server, this is weird. We're using protobufs ?!?!?", data);
35-
messageBus.emit(JSON.parse(data))
36-
}
37-
})
38-
demoData.setDemoData(null)
30+
setTimeout(() => worker.postMessage(demoData.demoData), 1000)
3931
}
40-
41-
// const urlParams = new URLSearchParams(window.location.search);
42-
// const channel = new BroadcastChannel(urlParams.get("uuid"));
43-
// channel.onmessage = async (event) => {
44-
// console.log("received", event, isWasmLoaded)
45-
// await window.testt(event.data, function (data) {
46-
// if (data instanceof Uint8Array) {
47-
// const msg = proto.Message.deserializeBinary(data).toObject()
48-
// setMessage("123")
49-
// console.log("huha?")
50-
// messageBus.emit(msg)
51-
// } else {
52-
// console.log("[message] text data received from server, this is weird. We're using protobufs ?!?!?", data);
53-
// messageBus.emit(JSON.parse(data))
54-
// }
55-
// })
56-
// };
5732
messageBus.listen([13], function (msg) {
5833
alert(msg.message)
59-
// window.testt(byteArray)
6034
})
61-
// Connect(this.messageBus)
62-
63-
async function loadWasm() {
64-
const go = new window.Go();
65-
WebAssembly.instantiateStreaming(fetch(serverHost + "/wasm"), go.importObject)
66-
.then((result) => {
67-
go.run(result.instance);
68-
console.log("should be loaded now")
69-
setIsWasmLoaded(true)
70-
});
71-
}
7235
}, [isWasmLoaded])
7336

7437
return (

web/src/libs/worker.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const serverHost = globalThis.location.host.includes("localhost") ? "http://localhost:8080" : "";
2+
3+
importScripts('wasm_exec.js');
4+
5+
onmessage = (event) => {
6+
if (event.data instanceof Uint8Array) {
7+
globalThis.testt(event.data, async function (data) {
8+
if (data instanceof Uint8Array) {
9+
postMessage(data)
10+
// const msg = proto.Message.deserializeBinary(data).toObject()
11+
// messageBus.emit(msg)
12+
} else {
13+
console.log("[message] text data received from server, this is weird. We're using protobufs ?!?!?", data);
14+
postMessage(JSON.parse(data))
15+
}
16+
})
17+
}
18+
}
19+
20+
async function loadWasm() {
21+
console.log("hus", serverHost + "/wasm")
22+
const go = new globalThis.Go();
23+
await WebAssembly.instantiateStreaming(fetch(serverHost + "/wasm"), go.importObject)
24+
.then((result) => {
25+
go.run(result.instance);
26+
console.log("should be loaded now")
27+
});
28+
}
29+
loadWasm();

0 commit comments

Comments
 (0)