From 5d62f304785d1eb1847afe939d83f1211024b61b Mon Sep 17 00:00:00 2001 From: webreflection Date: Mon, 10 Feb 2025 11:00:58 +0100 Subject: [PATCH] Dispatch `py-game` event right before executing code --- core/src/plugins/py-game.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/core/src/plugins/py-game.js b/core/src/plugins/py-game.js index c25b3d0e562..b93b979cc9a 100644 --- a/core/src/plugins/py-game.js +++ b/core/src/plugins/py-game.js @@ -13,10 +13,11 @@ const hooks = { toBeWarned = false; console.warn("⚠️ EXPERIMENTAL `py-game` FEATURE"); } + + let config = {}; if (script.hasAttribute("config")) { const value = script.getAttribute("config"); const { json, toml, text } = configDetails(value); - let config = {}; if (json) config = JSON.parse(text); else if (toml) { const { parse } = await import( @@ -54,7 +55,23 @@ const hooks = { const target = script.getAttribute("target") || "canvas"; const canvas = document.getElementById(target); wrap.interpreter.canvas.setCanvas2D(canvas); - await wrap.interpreter.runPythonAsync(code); + + // allow 3rd party to hook themselves right before + // the code gets executed + const event = new CustomEvent("py-game", { + bubbles: true, + cancelable: true, + detail: { + canvas, + code, + config, + wrap, + }, + }); + script.dispatchEvent(event); + // run only if the default was not prevented + if (!event.defaultPrevented) + await wrap.interpreter.runPythonAsync(code); }, }, };