[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

@@ -16,7 +16,7 @@
import { registerPlugin } from "@pyscript/core";
registerPlugin("mpy-script", {
type: "micropython", // or just 'mpy'
async onRuntimeReady(element, micropython) {
async onRuntimeReady(micropython, element) {
console.log(micropython);
// Somehow this doesn't work in MicroPython
micropython.io.stdout = (message) => {

View File

@@ -16,7 +16,7 @@
import { registerPlugin } from "@pyscript/core";
registerPlugin("lua-script", {
type: "wasmoon", // or just 'lua'
async onRuntimeReady(element, wasmoon) {
async onRuntimeReady(wasmoon, element) {
// Somehow this doesn't work in Wasmoon
wasmoon.io.stdout = (message) => {
console.log("🌑", wasmoon.type, message);

View File

@@ -28,5 +28,9 @@
js.console.log(a.x, b.x)
'Hello Web!'
</py-script>
<py-script>
XWorker('../a.py')
'OK'
</py-script>
</body>
</html>

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);
};