Fix #1799 - Avoid multiple bootstraps when embedded (#1800)

This commit is contained in:
Andrea Giammarchi
2023-10-23 15:54:51 +02:00
committed by GitHub
parent 7a6f8ab3ad
commit a24113f42b
6 changed files with 46 additions and 40 deletions

View File

@@ -1,17 +1,18 @@
{
"name": "@pyscript/core",
"version": "0.2.9",
"version": "0.2.10",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@pyscript/core",
"version": "0.2.9",
"version": "0.2.10",
"license": "APACHE-2.0",
"dependencies": {
"@ungap/with-resolvers": "^0.1.0",
"basic-devtools": "^0.1.6",
"polyscript": "^0.4.20",
"sticky-module": "^0.1.0",
"type-checked-collections": "^0.1.7"
},
"devDependencies": {
@@ -1015,9 +1016,9 @@
}
},
"node_modules/electron-to-chromium": {
"version": "1.4.548",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.548.tgz",
"integrity": "sha512-R77KD6mXv37DOyKLN/eW1rGS61N6yHOfapNSX9w+y9DdPG83l9Gkuv7qkCFZ4Ta4JPhrjgQfYbv4Y3TnM1Hi2Q==",
"version": "1.4.549",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.549.tgz",
"integrity": "sha512-gpXfJslSi4hYDkA0mTLEpYKRv9siAgSUgZ+UWyk+J5Cttpd1ThCVwdclzIwQSclz3hYn049+M2fgrP1WpvF8xg==",
"dev": true
},
"node_modules/entities": {
@@ -2566,9 +2567,9 @@
}
},
"node_modules/resolve": {
"version": "1.22.6",
"resolved": "https://registry.npmjs.org/resolve/-/resolve-1.22.6.tgz",
"integrity": "sha512-njhxM7mV12JfufShqGy3Rz8j11RPdLy4xi15UurGJeoHLfJpVXKdh3ueuOqbYUcDZnffr6X739JBo5LzyahEsw==",
"version": "1.22.8",
"resolved": "https://registry.npmjs.org/resolve/-/resolve-1.22.8.tgz",
"integrity": "sha512-oKWePCxqpd6FlLvGV1VU0x7bkPmmCNolxzjMf4NczoDnQcIWrAF+cPtZn5i6n+RfD2d9i0tzpKnG6Yk168yIyw==",
"dev": true,
"dependencies": {
"is-core-module": "^2.13.0",

View File

@@ -1,6 +1,6 @@
{
"name": "@pyscript/core",
"version": "0.2.9",
"version": "0.2.10",
"type": "module",
"description": "PyScript",
"module": "./index.js",
@@ -34,6 +34,7 @@
"@ungap/with-resolvers": "^0.1.0",
"basic-devtools": "^0.1.6",
"polyscript": "^0.4.20",
"sticky-module": "^0.1.0",
"type-checked-collections": "^0.1.7"
},
"devDependencies": {

View File

@@ -1,20 +1,20 @@
/*! (c) PyScript Development Team */
import stickyModule from "sticky-module";
import "@ungap/with-resolvers";
// These imports can hook more than usual and help debugging possible polyscript issues
import {
INVALID_CONTENT,
define,
Hook,
XWorker,
} from "../node_modules/polyscript/esm/index.js";
import { queryTarget } from "../node_modules/polyscript/esm/script-handler.js";
import {
assign,
dedent,
define,
defineProperty,
dispatch,
queryTarget,
unescape,
} from "../node_modules/polyscript/esm/utils.js";
import { Hook } from "../node_modules/polyscript/esm/worker/hooks.js";
} from "polyscript/exports";
import "./all-done.js";
import TYPES from "./types.js";
@@ -25,8 +25,6 @@ import stdlib from "./stdlib.js";
import { ErrorCode } from "./exceptions.js";
import { robustFetch as fetch, getText } from "./fetch.js";
const { assign, defineProperty } = Object;
// allows lazy element features on code evaluation
let currentElement;
@@ -86,10 +84,26 @@ const workerHooks = {
[...hooks.codeAfterRunWorkerAsync].map(dedent).join("\n"),
};
const exportedConfig = {};
export { exportedConfig as config, hooks };
// avoid multiple initialization of the same library
const [
{
PyWorker: exportedPyWorker,
hooks: exportedHooks,
config: exportedConfig,
},
alreadyLive,
] = stickyModule("@pyscript/core", { PyWorker, hooks, config: {} });
export {
exportedPyWorker as PyWorker,
exportedHooks as hooks,
exportedConfig as config,
};
for (const [TYPE, interpreter] of TYPES) {
// avoid any dance if the module already landed
if (alreadyLive) break;
const dispatchDone = (element, isAsync, result) => {
if (isAsync) result.then(() => dispatch(element, TYPE, "done"));
else dispatch(element, TYPE, "done");
@@ -290,7 +304,7 @@ for (const [TYPE, interpreter] of TYPES) {
* @param {{config?: string | object, async?: boolean}} [options] optional configuration for the worker.
* @returns {Worker & {sync: ProxyHandler<object>}}
*/
export function PyWorker(file, options) {
function PyWorker(file, options) {
// this propagates pyscript worker hooks without needing a pyscript
// bootstrap + it passes arguments and enforces `pyodide`
// as the interpreter to use in the worker, as all hooks assume that

View File

@@ -1,3 +1,5 @@
import { assign } from "polyscript/exports";
const CLOSEBUTTON =
"<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='currentColor' width='12px'><path d='M.293.293a1 1 0 011.414 0L8 6.586 14.293.293a1 1 0 111.414 1.414L9.414 8l6.293 6.293a1 1 0 01-1.414 1.414L8 9.414l-6.293 6.293a1 1 0 01-1.414-1.414L6.586 8 .293 1.707a1 1 0 010-1.414z'/></svg>";
@@ -87,13 +89,13 @@ export function _createAlertBanner(
}
const content = messageType === "html" ? "innerHTML" : "textContent";
const banner = Object.assign(document.createElement("div"), {
const banner = assign(document.createElement("div"), {
className: `alert-banner py-${level}`,
[content]: message,
});
if (level === "warning") {
const closeButton = Object.assign(document.createElement("button"), {
const closeButton = assign(document.createElement("button"), {
id: "alert-close-button",
innerHTML: CLOSEBUTTON,
});

View File

@@ -1,5 +1,5 @@
import { FetchError, ErrorCode } from "./exceptions.js";
import { getText } from "../node_modules/polyscript/esm/fetch-utils.js";
import { getText } from "polyscript/exports";
export { getText };

View File

@@ -1,16 +1,4 @@
/**
* A `Worker` facade able to bootstrap on the worker thread only a PyScript module.
* @param {string} file the python file to run ina worker.
* @param {{config?: string | object, async?: boolean}} [options] optional configuration for the worker.
* @returns {Worker & {sync: ProxyHandler<object>}}
*/
export function PyWorker(file: string, options?: {
config?: string | object;
async?: boolean;
}): Worker & {
sync: ProxyHandler<object>;
};
import sync from "./sync.js";
declare const exportedConfig: {};
import hooks from "./hooks.js";
export { exportedConfig as config, hooks };
declare const exportedPyWorker: any;
declare const exportedHooks: any;
declare const exportedConfig: any;
export { exportedPyWorker as PyWorker, exportedHooks as hooks, exportedConfig as config };