8000 Fix #1799 - Avoid multiple bootstraps when embedded by WebReflection · Pull Request #1800 · pyscript/pyscript · GitHub
[go: up one dir, main page]

Skip to content

Fix #1799 - Avoid multiple bootstraps when embedded #1800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions pyscript.core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyscript.core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pyscript/core",
"version": "0.2.9",
"version": "0.2.10",
"type": "module",
"description": "PyScript",
"module": "./index.js",
Expand Down Expand Up @@ -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": {
Expand Down
38 changes: 26 additions & 12 deletions pyscript.core/src/core.js
8000
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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;

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions pyscript.core/src/exceptions.js
Original file line number Diff line number Diff line change
@@ -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>";

Expand Down Expand Up @@ -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,
});
Expand Down
2 changes: 1 addition & 1 deletion pyscript.core/src/fetch.js
8000
Original file line number Diff line number Diff line change
@@ -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 };

Expand Down
20 changes: 4 additions & 16 deletions pyscript.core/types/core.d.ts
Original file line number Diff line number Diff line change
@@ -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 };
2A4E
0