handle errors within event handlers

This commit is contained in:
Reimar 2025-10-15 10:25:47 +02:00
parent 01e5a5c7e8
commit 09dda08f52

View File

@ -63,12 +63,22 @@ export class Gamelib {
keydownListener(ev) { keydownListener(ev) {
this.keysPressed.add(ev.key); this.keysPressed.add(ev.key);
this.keyPressHandlers.get(ev.key)?.();
try {
this.keyPressHandlers.get(ev.key)?.();
} catch (e) {
this.console.error(e);
}
} }
keyupListener(ev) { keyupListener(ev) {
this.keysPressed.delete(ev.key); this.keysPressed.delete(ev.key);
this.keyReleaseHandlers.get(ev.key)?.();
try {
this.keyReleaseHandlers.get(ev.key)?.();
} catch (e) {
this.console.error(e);
}
} }
mousemoveListener(ev) { mousemoveListener(ev) {
@ -78,12 +88,16 @@ export class Gamelib {
this.mouseX = ev.offsetX * ratioX; this.mouseX = ev.offsetX * ratioX;
this.mouseY = ev.offsetY * ratioY; this.mouseY = ev.offsetY * ratioY;
this.mouseMoveHandler?.( try {
ev.offsetX * ratioX, this.mouseMoveHandler?.(
ev.offsetY * ratioY, ev.offsetX * ratioX,
ev.movementX * ratioX, ev.offsetY * ratioY,
ev.movementY * ratioY, ev.movementX * ratioX,
); ev.movementY * ratioY,
);
} catch (e) {
this.console.error(e);
}
} }
mousedownListener(ev) { mousedownListener(ev) {
@ -91,7 +105,12 @@ export class Gamelib {
const ratioY = this.canvas.height / this.canvas.clientHeight; const ratioY = this.canvas.height / this.canvas.clientHeight;
this.mouseButtonsPressed.add(ev.button); this.mouseButtonsPressed.add(ev.button);
this.mouseDownHandlers.get(ev.button)?.(ev.offsetX * ratioX, ev.offsetY * ratioY);
try {
this.mouseDownHandlers.get(ev.button)?.(ev.offsetX * ratioX, ev.offsetY * ratioY);
} catch (e) {
this.console.error(e);
}
} }
mouseupListener(ev) { mouseupListener(ev) {
@ -99,7 +118,12 @@ export class Gamelib {
const ratioY = this.canvas.height / this.canvas.clientHeight; const ratioY = this.canvas.height / this.canvas.clientHeight;
this.mouseButtonsPressed.delete(ev.button); this.mouseButtonsPressed.delete(ev.button);
this.mouseUpHandlers.get(ev.button)?.(ev.offsetX * ratioX, ev.offsetY * ratioY);
try {
this.mouseUpHandlers.get(ev.button)?.(ev.offsetX * ratioX, ev.offsetY * ratioY);
} catch (e) {
this.console.error(e);
}
} }
contextMenuListener(ev) { contextMenuListener(ev) {