10000 Dispatch `py-game` event right before executing code · pyscript/pyscript@11d2c0e · GitHub
[go: up one dir, main page]

Skip to content

Commit 11d2c0e

Browse files
committed
Dispatch py-game event right before executing code
1 parent b13317d commit 11d2c0e

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

core/src/plugins/py-game.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ const hooks = {
1313
toBeWarned = false;
1414
console.warn("⚠️ EXPERIMENTAL `py-game` FEATURE");
1515
}
16+
17+
let config = {};
1618
if (script.hasAttribute("config")) {
1719
const value = script.getAttribute("config");
1820
const { json, toml, text } = configDetails(value);
19-
let config = {};
2021
if (json) config = JSON.parse(text);
2122
else if (toml) {
2223
const { parse } = await import(
@@ -54,7 +55,22 @@ const hooks = {
5455
const target = script.getAttribute("target") || "canvas";
5556
const canvas = document.getElementById(target);
5657
wrap.interpreter.canvas.setCanvas2D(canvas);
57-
await wrap.interpreter.runPythonAsync(code);
58+
59+
// allow 3rd party to hook themselves right before
60+
// the code gets executed
61+
const event = new CustomEvent("py-game", {
62+
bubbles: true,
63+
cancelable: true,
64+
detail: {
65+
canvas,
66+
config,
67+
wrap,
68+
},
69+
});
70+
script.dispatchEvent(event);
71+
// run only if the default was not prevented
72+
if (!event.defaultPrevented)
73+
await wrap.interpreter.runPythonAsync(code);
5874
},
5975
},
6076
};

0 commit comments

Comments
 (0)
0