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