Fix #1946 - Do not hold while bootstrapping (#1953)

This commit is contained in:
Andrea Giammarchi
2024-01-26 15:04:02 +01:00
committed by GitHub
parent f6470dcad5
commit 63f2453091
4 changed files with 42 additions and 5 deletions

View File

@@ -1,12 +1,12 @@
{
"name": "@pyscript/core",
"version": "0.3.20",
"version": "0.3.21",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@pyscript/core",
"version": "0.3.20",
"version": "0.3.21",
"license": "APACHE-2.0",
"dependencies": {
"@ungap/with-resolvers": "^0.1.0",

View File

@@ -1,6 +1,6 @@
{
"name": "@pyscript/core",
"version": "0.3.20",
"version": "0.3.21",
"type": "module",
"description": "PyScript",
"module": "./index.js",

View File

@@ -200,6 +200,9 @@ const init = async (script, type, interpreter) => {
// avoid too greedy MutationObserver operations at distance
let timeout = 0;
// avoid delayed initialization
let queue = Promise.resolve();
// reset interval value then check for new scripts
const resetTimeout = () => {
timeout = 0;
@@ -213,11 +216,14 @@ const pyEditor = async () => {
for (const [type, interpreter] of TYPES) {
const selector = `script[type="${type}-editor"]`;
for (const script of document.querySelectorAll(selector)) {
// avoid any further bootstrap
// avoid any further bootstrap by changing the type as active
script.type += "-active";
await init(script, type, interpreter);
// don't await in here or multiple calls might happen
// while the first script is being initialized
queue = queue.then(() => init(script, type, interpreter));
}
}
return queue;
};
new MutationObserver(pyEditor).observe(document, {

View File

@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>PyScript Test</title>
<link rel="stylesheet" href="../dist/core.css">
<script type="module" src="../dist/core.js"></script>
</head>
<body>
<script type="py-editor">
0
</script>
<script type="py-editor">
1
</script>
<script type="py-editor">
2
</script>
<script type="py-editor">
3
</script>
<script type="py-editor">
4
</script>
<script type="py-editor">
5
</script>
<!-- more... -->
</body>
</html>