[next] Enabled hooks around plugins (#1522)

This commit is contained in:
Andrea Giammarchi
2023-06-12 22:18:55 +02:00
committed by GitHub
parent bb364b0524
commit da544929ac
10 changed files with 138 additions and 19 deletions

View File

@@ -11,19 +11,32 @@ document.head.appendChild(document.createElement("style")).textContent = `
let id = 0;
const getID = (prefix = "py-script") => `${prefix}-${id++}`;
let bootstrap = true;
let bootstrap = true,
XWorker,
sharedRuntime;
const sharedPyodide = new Promise((resolve) => {
const pyConfig = document.querySelector("py-config");
const config = pyConfig?.getAttribute("src") || pyConfig?.textContent;
registerPlugin("py-script", {
config,
type: "pyodide", // or just 'py'
async onRuntimeReady(_, pyodide) {
codeBeforeRunWorker: `print('codeBeforeRunWorker')`,
codeAfterRunWorker: `print('codeAfterRunWorker')`,
onBeforeRun(pyodide, node) {
pyodide.runtime.globals.set("XWorker", XWorker);
console.log("onBeforeRun", sharedRuntime === pyodide, node);
},
onAfterRun(pyodide, node) {
console.log("onAfterRun", sharedRuntime === pyodide, node);
},
async onRuntimeReady(pyodide) {
// bootstrap the shared runtime once
// as each node as plugin gets onRuntimeReady called once
// because no custom-element is strictly needed
if (bootstrap) {
bootstrap = false;
sharedRuntime = pyodide;
XWorker = pyodide.XWorker;
pyodide.io.stdout = (message) => {
console.log("🐍", pyodide.type, message);
};