Skip to content

Commit 312195e

Browse files
committed
Remove EventWorker
- because we don't await start anyway, I removed the initialized reply
1 parent cf05fa8 commit 312195e

File tree

3 files changed

+27
-123
lines changed

3 files changed

+27
-123
lines changed

EventWorker.js

Lines changed: 0 additions & 91 deletions
This file was deleted.

raylib-wrapper.js

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
import EventWorker from "./EventWorker.js";
21
import { DataSchema } from "./SharedData.js";
32

4-
function setListeners(eventWorker, handlers, ctx = handlers) {
5-
for (const prop in handlers) {
6-
eventWorker.setListener(prop, handlers[prop].bind(ctx));
7-
}
3+
function setListeners(/** @type {Worker} */ worker, handlers) {
4+
worker.onmessage = (ev) => {
5+
if ("topic" in ev.data && "data" in ev.data) {
6+
if (ev.data.topic in handlers) {
7+
handlers[ev.data.topic](...ev.data.data);
8+
} else {
9+
throw new TypeError(`No handler for ${ev.data.topic}!`);
10+
}
11+
} else {
12+
throw new TypeError("Invalid message received!");
13+
}
14+
};
815
}
916

1017
export default class RaylibJs {
@@ -18,7 +25,7 @@ export default class RaylibJs {
1825
}
1926
this.ctx = canvas.getContext("bitmaprenderer");
2027
if (this.worker === undefined) {
21-
this.worker = new EventWorker("./raylib.js", { type: "module" });
28+
this.worker = new Worker("./raylib.js", { type: "module" });
2229
} else {
2330
throw new Error("raylib.js worker already exists!");
2431
}
@@ -32,15 +39,11 @@ export default class RaylibJs {
3239
});
3340
this.views = DataSchema.view(shared);
3441
// bind listeners
35-
setListeners(
36-
this.worker,
37-
{
38-
frame: this.#onFrame,
39-
window: this.#onWindow,
40-
requestAnimationFrame: this.#onRequestAnimationFrame,
41-
},
42-
this,
43-
);
42+
setListeners(this.worker, {
43+
frame: this.#onFrame.bind(this),
44+
window: this.#onWindow.bind(this),
45+
requestAnimationFrame: this.#onRequestAnimationFrame.bind(this),
46+
});
4447
window.addEventListener("keydown", (e) => {
4548
const key = glfwKeyMapping[e.code];
4649
this.views.keys[~~(key / 8)] |= 1 << key % 8;
@@ -58,16 +61,8 @@ export default class RaylibJs {
5861
});
5962

6063
// Initialize raylib.js worker
61-
return new Promise((resolve) => {
62-
this.worker.setListener("initialized", () => {
63-
this.worker.removeListener("initialized");
64-
// TODO: listen to real changss in boundingClientRect
65-
console.log("initialized");
66-
this.#setBoundingRect();
67-
resolve();
68-
});
69-
this.worker.send("init", { wasmPath, shared });
70-
});
64+
this.#setBoundingRect();
65+
this.worker.postMessage({ wasmPath, shared });
7166
}
7267

7368
stop() {

raylib.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { registerWorkerEvents, reply } from "./EventWorker.js";
21
import { DataSchema } from "./SharedData.js";
32

43
if (typeof importScripts !== "function") {
@@ -83,7 +82,6 @@ class RaylibJs {
8382
this.#wait(() => reply("requestAnimationFrame"));
8483
this.previous = this.views.time[0];
8584
this.#wait(() => reply("requestAnimationFrame"));
86-
reply("initialized");
8785

8886
this.wasm.instance.exports.main();
8987
// backwards compatibility
@@ -422,8 +420,10 @@ function getColorFromMemory(buffer, color_ptr) {
422420
return color_hex_unpacked(r, g, b, a);
423421
}
424422

425-
registerWorkerEvents({
426-
init(options) {
427-
new RaylibJs().start(options);
428-
}
429-
});
423+
onmessage = (ev) => {
424+
new RaylibJs().start(ev.data);
425+
}
426+
427+
function reply(topic, ...data) {
428+
postMessage({topic, data});
429+
}

0 commit comments

Comments
 (0)