Fix #2360 - Better shared env/setup handling (#2361)

This commit is contained in:
Andrea Giammarchi
2025-07-11 04:56:14 -04:00
committed by GitHub
parent b528ba67a9
commit eaa6711756
3 changed files with 69 additions and 46 deletions

54
core/package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "@pyscript/core",
"version": "0.6.64",
"version": "0.6.65",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@pyscript/core",
"version": "0.6.64",
"version": "0.6.65",
"license": "APACHE-2.0",
"dependencies": {
"@ungap/with-resolvers": "^0.1.0",
@@ -14,7 +14,7 @@
"@webreflection/utils": "^0.1.1",
"add-promise-listener": "^0.1.3",
"basic-devtools": "^0.1.6",
"polyscript": "^0.18.1",
"polyscript": "^0.18.3",
"sticky-module": "^0.1.1",
"to-json-callback": "^0.1.1",
"type-checked-collections": "^0.1.7"
@@ -25,7 +25,7 @@
"@codemirror/language": "^6.11.2",
"@codemirror/state": "^6.5.2",
"@codemirror/view": "^6.38.0",
"@playwright/test": "^1.53.2",
"@playwright/test": "^1.54.0",
"@rollup/plugin-commonjs": "^28.0.6",
"@rollup/plugin-node-resolve": "^16.0.1",
"@rollup/plugin-terser": "^0.4.4",
@@ -624,13 +624,13 @@
]
},
"node_modules/@playwright/test": {
"version": "1.53.2",
"resolved": "https://registry.npmjs.org/@playwright/test/-/test-1.53.2.tgz",
"integrity": "sha512-tEB2U5z74ebBeyfGNZ3Jfg29AnW+5HlWhvHtb/Mqco9pFdZU1ZLNdVb2UtB5CvmiilNr2ZfVH/qMmAROG/XTzw==",
"version": "1.54.0",
"resolved": "https://registry.npmjs.org/@playwright/test/-/test-1.54.0.tgz",
"integrity": "sha512-6Mnd5daQmLivaLu5kxUg6FxPtXY4sXsS5SUwKjWNy4ISe4pKraNHoFxcsaTFiNUULbjy0Vlb5HT86QuM0Jy1pQ==",
"dev": true,
"license": "Apache-2.0",
"dependencies": {
"playwright": "1.53.2"
"playwright": "1.54.0"
},
"bin": {
"playwright": "cli.js"
@@ -1397,14 +1397,14 @@
}
},
"node_modules/coincident": {
"version": "4.0.19",
"resolved": "https://registry.npmjs.org/coincident/-/coincident-4.0.19.tgz",
"integrity": "sha512-4AepKY1917fgARiwz+tg3Q8Jd2ZCZ+6uw5+20AqdfRIAuntNCwcQw/hM6I0/egqgQZ+uiHDbhkWpYiJ5epd+RQ==",
"version": "4.0.21",
"resolved": "https://registry.npmjs.org/coincident/-/coincident-4.0.21.tgz",
"integrity": "sha512-pT0MbJ2fhOEGEAlG0wVctaueisWW5zk3WA4vK+yQMDB8ZtJQUlHsMWtWTwmVAFavKt5sQfhO7eF4kLKNdn3erA==",
"license": "MIT",
"dependencies": {
"@webreflection/utils": "^0.1.1",
"next-resolver": "^0.1.4",
"reflected-ffi": "^0.5.7"
"reflected-ffi": "^0.5.9"
},
"bin": {
"coincident": "cli.cjs"
@@ -2719,13 +2719,13 @@
"license": "ISC"
},
"node_modules/playwright": {
"version": "1.53.2",
"resolved": "https://registry.npmjs.org/playwright/-/playwright-1.53.2.tgz",
"integrity": "sha512-6K/qQxVFuVQhRQhFsVZ9fGeatxirtrpPgxzBYWyZLEXJzqYwuL4fuNmfOfD5et1tJE4GScKyPNeLhZeRwuTU3A==",
"version": "1.54.0",
"resolved": "https://registry.npmjs.org/playwright/-/playwright-1.54.0.tgz",
"integrity": "sha512-y9yzHmXRwEUOpghM7XGcA38GjWuTOUMaTIcm/5rHcYVjh5MSp9qQMRRMc/+p1cx+csoPnX4wkxAF61v5VKirxg==",
"dev": true,
"license": "Apache-2.0",
"dependencies": {
"playwright-core": "1.53.2"
"playwright-core": "1.54.0"
},
"bin": {
"playwright": "cli.js"
@@ -2738,9 +2738,9 @@
}
},
"node_modules/playwright-core": {
"version": "1.53.2",
"resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.53.2.tgz",
"integrity": "sha512-ox/OytMy+2w1jcYEYlOo1Hhp8hZkLCximMTUTMBXjGUA1KoFfiSZ+DU+3a739jsPY0yoKH2TFy9S2fsJas8yAw==",
"version": "1.54.0",
"resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.54.0.tgz",
"integrity": "sha512-uiWpWaJh3R3etpJ0QrpligEMl62Dk1iSAB6NUXylvmQz+e3eipXHDHvOvydDAssb5Oqo0E818qdn0L9GcJSTyA==",
"dev": true,
"license": "Apache-2.0",
"bin": {
@@ -2751,9 +2751,9 @@
}
},
"node_modules/polyscript": {
"version": "0.18.1",
"resolved": "https://registry.npmjs.org/polyscript/-/polyscript-0.18.1.tgz",
"integrity": "sha512-eOO8dYqNPk6lrnw0nFZnTh81k+dbcZ/2qsrbn3xX2m3+RSDrCksav9YX9WlZtqTGOeIoMM1133QGrAnS4fnUZA==",
"version": "0.18.3",
"resolved": "https://registry.npmjs.org/polyscript/-/polyscript-0.18.3.tgz",
"integrity": "sha512-0iB4AmSbTZh2cJZSzP7EkeNa0Im6tVUN6+3eBUtrkMzL1vtcpSrtIVEs0X1fZ5xWHdmczafHx5PPoMe1zSXE9g==",
"license": "APACHE-2.0",
"dependencies": {
"@webreflection/fetch": "^0.1.5",
@@ -2761,9 +2761,9 @@
"@webreflection/utils": "^0.1.1",
"basic-devtools": "^0.1.6",
"codedent": "^0.1.2",
"coincident": "^4.0.19",
"coincident": "^4.0.21",
"html-escaper": "^3.0.3",
"reflected-ffi": "^0.5.7",
"reflected-ffi": "^0.5.8",
"sticky-module": "^0.1.1",
"to-json-callback": "^0.1.1"
}
@@ -3447,9 +3447,9 @@
}
},
"node_modules/reflected-ffi": {
"version": "0.5.7",
"resolved": "https://registry.npmjs.org/reflected-ffi/-/reflected-ffi-0.5.7.tgz",
"integrity": "sha512-U4GiS/JVK7WUla4QseKfhsS2nQBAtL3ehmtM0W0Ffj1kzD0XboqdxclN3ufWRz/wPQ9Cp0ivVWwdrdl5yQqzgA==",
"version": "0.5.9",
"resolved": "https://registry.npmjs.org/reflected-ffi/-/reflected-ffi-0.5.9.tgz",
"integrity": "sha512-0BH/0dbg7GsZQH7rdY/Fw3pz/FHhX2JsH4kw5hTMVKj6CQRzjH1WrYXRKJ0mkI1z84Iu5SDCfO6b8X+cDw3KaA==",
"license": "MIT"
},
"node_modules/resolve": {

View File

@@ -1,6 +1,6 @@
{
"name": "@pyscript/core",
"version": "0.6.64",
"version": "0.6.65",
"type": "module",
"description": "PyScript",
"module": "./index.js",
@@ -70,7 +70,7 @@
"@webreflection/utils": "^0.1.1",
"add-promise-listener": "^0.1.3",
"basic-devtools": "^0.1.6",
"polyscript": "^0.18.1",
"polyscript": "^0.18.3",
"sticky-module": "^0.1.1",
"to-json-callback": "^0.1.1",
"type-checked-collections": "^0.1.7"
@@ -81,7 +81,7 @@
"@codemirror/language": "^6.11.2",
"@codemirror/state": "^6.5.2",
"@codemirror/view": "^6.38.0",
"@playwright/test": "^1.53.2",
"@playwright/test": "^1.54.0",
"@rollup/plugin-commonjs": "^28.0.6",
"@rollup/plugin-node-resolve": "^16.0.1",
"@rollup/plugin-terser": "^0.4.4",

View File

@@ -38,7 +38,7 @@ const getRelatedScript = (target, type) => {
return editor?.parentNode?.previousElementSibling;
};
async function execute({ currentTarget }) {
async function execute({ currentTarget, script }) {
const { env, pySrc, outDiv } = this;
const hasRunButton = !!currentTarget;
@@ -91,14 +91,13 @@ async function execute({ currentTarget }) {
// creation and destruction of editors on the fly
if (hasRunButton) {
for (const type of TYPES.keys()) {
const script = getRelatedScript(currentTarget, type);
if (script) {
defineProperties(script, { xworker: { value: xworker } });
break;
}
script = getRelatedScript(currentTarget, type);
if (script) break;
}
}
defineProperties(script, { xworker: { value: xworker } });
const { sync } = xworker;
const { promise, resolve } = withResolvers();
envs.set(env, promise);
@@ -157,6 +156,20 @@ async function execute({ currentTarget }) {
});
}
const replaceScript = (script, type) => {
script.xworker?.terminate();
const clone = script.cloneNode(true);
clone.type = `${type}-editor`;
const editor = editors.get(script);
if (editor) {
const content = editor.state.doc.toString();
clone.textContent = content;
editors.delete(script);
script.nextElementSibling.remove();
}
script.replaceWith(clone);
};
const makeRunButton = (handler, type) => {
const runButton = document.createElement("button");
runButton.className = `absolute ${type}-editor-run-button`;
@@ -169,15 +182,25 @@ const makeRunButton = (handler, type) => {
) {
const script = getRelatedScript(runButton, type);
if (script) {
const editor = editors.get(script);
const content = editor.state.doc.toString();
const clone = script.cloneNode(true);
clone.type = `${type}-editor`;
clone.textContent = content;
script.xworker.terminate();
script.nextElementSibling.remove();
script.replaceWith(clone);
editors.delete(script);
const env = script.getAttribute("env");
// remove the bootstrapped env which could be one or shared
if (env) {
for (const [key, value] of TYPES) {
if (key === type) {
configs.delete(`${value}-${env}`);
envs.delete(`${value}-${env}`);
break;
}
}
}
// lonley script without setup node should be replaced
if (script.xworker) replaceScript(script, type);
// all scripts sharing the same env should be replaced
else {
const sel = `script[type^="${type}-editor"][env="${env}"]`;
for (const script of document.querySelectorAll(sel))
replaceScript(script, type);
}
}
return;
}
@@ -365,7 +388,7 @@ const init = async (script, type, interpreter) => {
};
if (isSetup) {
await context.handleEvent({ currentTarget: null });
await context.handleEvent({ currentTarget: null, script });
notifyEditor();
return;
}