8000 Reduce conflicts on multiple custom scripts (#1897) · shivashankarv/pyscript@136e954 · GitHub
[go: up one dir, main page]

Skip to content

Commit 136e954

Browse files
Reduce conflicts on multiple custom scripts (pyscript#1897)
1 parent 6a3e283 commit 136e954

File tree

4 files changed

+55
-32
lines changed

4 files changed

+55
-32
lines changed

pyscript.core/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyscript.core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pyscript/core",
3-
"version": "0.3.9",
3+
"version": "0.3.10",
44
"type": "module",
55
"description": "PyScript",
66
"module": "./index.js",
< 10000 /div>

pyscript.core/src/core.js

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,9 @@ import { ErrorCode } from "./exceptions.js";
2626
import { robustFetch as fetch, getText } from "./fetch.js";
2727
import { hooks, main, worker, codeFor, createFunction } from "./hooks.js";
2828

29-
// allows lazy element features on code evaluation
30-
let currentElement;
31-
3229
// generic helper to disambiguate between custom element and script
3330
const isScript = ({ tagName }) => tagName === "SCRIPT";
3431

35-
let shouldRegister = true;
36-
const registerModule = ({ XWorker: $XWorker, interpreter, io }) => {
37-
// automatically use the pyscript stderr (when/if defined)
38-
// this defaults to console.error
39-
function PyWorker(...args) {
40-
const worker = $XWorker(...args);
41-
worker.onerror = ({ error }) => io.stderr(error);
42-
return worker;
43-
}
44-
45-
// enrich the Python env with some JS utility for main
46-
interpreter.registerJsModule("_pyscript", {
47-
PyWorker,
48-
get target() {
49-
return isScript(currentElement)
50-
? currentElement.target.id
51-
: currentElement.id;
52-
},
53-
});
54-
};
55-
5632
// avoid multiple initialization of the same library
5733
const [
5834
{
@@ -118,6 +94,36 @@ for (const [TYPE, interpreter] of TYPES) {
11894
return code;
11995
};
12096

97+
// register once any interpreter
98+
let alreadyRegistered = false;
99+
100+
// allows lazy element features on code evaluation
101+
let currentElement;
102+
103+
const registerModule = ({ XWorker, interpreter, io }) => {
104+
// avoid multiple registration of the same interpreter
105+
if (alreadyRegistered) return;
106+
alreadyRegistered = true;
107+
108+
// automatically use the pyscript stderr (when/if defined)
109+
// this defaults to console.error
110+
function PyWorker(...args) {
111+
const worker = XWorker(...args);
112+
worker.onerror = ({ error }) => io.stderr(error);
113+
return worker;
114+
}
115+
116+
// enrich the Python env with some JS utility for main
117+
interpreter.registerJsModule("_pyscript", {
118+
PyWorker,
119+
get target() {
120+
return isScript(currentElement)
121+
? currentElement.target.id
122+
: currentElement.id;
123+
},
124+
});
125+
};
126+
121127
// define the module as both `<script type="py">` and `<py-script>`
122128
// but only if the config didn't throw an error
123129
if (!error) {
@@ -133,10 +139,7 @@ for (const [TYPE, interpreter] of TYPES) {
133139
main: {
134140
...codeFor(main),
135141
async onReady(wrap, element) {
136-
if (shouldRegister) {
137-
shouldRegister = false;
138-
registerModule(wrap);
139-
}
142+
registerModule(wrap);
140143

141144
// allows plugins to do whatever they want with the element
142145
// before regular stuff happens in here
@@ -320,7 +323,7 @@ for (const [TYPE, interpreter] of TYPES) {
320323
function PyWorker(file, options) {
321324
const hooks = hooked.get("py");
322325
// this propagates pyscript worker hooks without needing a pyscript
323-
// bootstrap + it passes arguments and enforces `pyodide`
326+
// bootstrap + it passes arguments and it defaults to `pyodide`
324327
// as the interpreter to use in the worker, as all hooks assume that
325328
// and as `pyodide` is the only default interpreter that can deal with
326329
// all the features we need to deliver pyscript out there.

pyscript.core/test/multi.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<script type="module" src="../dist/core.js"></script>
7+
</head>
8+
<body>
9+
<script type="mpy">
10+
from pyscript import document
11+
import sys
12+
document.body.append(sys.version)
13+
</script>
14+
<script type="py">
15+
from pyscript import document
16+
import sys
17+
document.body.append(sys.version)
18+
</script>
19+
</body>
20+
</html>

0 commit comments

Comments
 (0)
0