8000 Dispatch `py-game` event right before executing code by WebReflection · Pull Request #2287 · pyscript/pyscript · GitHub
[go: up one dir, main page]

Skip to content

Dispatch py-game event right before executing code #2287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 27, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions core/src/plugins/py-game.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
},
},
};
Expand Down
Loading
0