Fixed issues around XWorker context (#1556)

This commit is contained in:
Andrea Giammarchi
2023-06-22 19:04:57 +02:00
committed by GitHub
parent f6dfc5361e
commit 3b7099cd3d
7 changed files with 38 additions and 24 deletions

View File

@@ -11,6 +11,7 @@ import {
import { getRuntimeID } from "./loader.js";
import { io } from "./interpreter/_utils.js";
import { addAllListeners } from "./listeners.js";
import { Hook } from "./worker/hooks.js";
export const CUSTOM_SELECTORS = [];
@@ -64,18 +65,9 @@ export const handleCustomType = (node) => {
onBeforeRunAsync,
onAfterRun,
onAfterRunAsync,
codeBeforeRunWorker,
codeBeforeRunWorkerAsync,
codeAfterRunWorker,
codeAfterRunWorkerAsync,
} = options;
const hooks = {
beforeRun: codeBeforeRunWorker?.(),
beforeRunAsync: codeBeforeRunWorkerAsync?.(),
afterRun: codeAfterRunWorker?.(),
afterRunAsync: codeAfterRunWorkerAsync?.(),
};
const hooks = new Hook(options);
const XWorker = function XWorker(...args) {
return Worker.apply(hooks, args);

View File

@@ -90,14 +90,10 @@ document.head.appendChild(document.createElement("style")).textContent = `
env: "py-script",
interpreter: "pyodide",
codeBeforeRunWorker() {
const { codeBeforeRunWorker: set } = hooks;
const prefix = 'print("codeBeforeRunWorker")';
return [prefix].concat(...set).join("\n");
return [...hooks.codeBeforeRunWorker].join("\n");
},
codeAfterRunWorker() {
const { codeAfterRunWorker: set } = hooks;
const prefix = 'print("codeAfterRunWorker")';
return [prefix].concat(...set).join("\n");
return [...hooks.codeAfterRunWorker].join("\n");
},
onBeforeRun(pyodide, element) {
bootstrapNodeAndPlugins(pyodide, element, before, "onBeforeRun");

View File

@@ -3,6 +3,7 @@ import coincident from "coincident/window";
import xworker from "./xworker.js";
import { assign, defineProperties, absoluteURL } from "../utils.js";
import { getText } from "../fetch-utils.js";
import { Hook } from "./hooks.js";
/**
* @typedef {Object} WorkerOptions custom configuration
@@ -21,7 +22,6 @@ export default (...args) =>
function XWorker(url, options) {
const worker = xworker();
const { postMessage } = worker;
const hooks = this instanceof XWorker ? void 0 : this;
if (args.length) {
const [type, version] = args;
options = assign({}, options || { type, version });
@@ -30,7 +30,10 @@ export default (...args) =>
if (options?.config) options.config = absoluteURL(options.config);
const bootstrap = fetch(url)
.then(getText)
.then((code) => postMessage.call(worker, { options, code, hooks }));
.then((code) => {
const hooks = this instanceof Hook ? this : void 0;
postMessage.call(worker, { options, code, hooks });
});
return defineProperties(worker, {
postMessage: {
value: (data, ...rest) =>

View File

@@ -0,0 +1,15 @@
// REQUIRES INTEGRATION TEST
/* c8 ignore start */
const workerHooks = [
["beforeRun", "codeBeforeRunWorker"],
["beforeRunAsync", "codeBeforeRunWorkerAsync"],
["afterRun", "codeAfterRunWorker"],
["afterRunAsync", "codeAfterRunWorkerAsync"],
];
export class Hook {
constructor(fields) {
for (const [key, value] of workerHooks) this[key] = fields[value]?.();
}
}
/* c8 ignore stop */

View File

@@ -1,7 +1,7 @@
{
"name": "@pyscript/core",
"version": "0.0.1",
"description": "",
"version": "0.0.4",
"description": "PyScript Next core",
"main": "./cjs/index.js",
"types": "./types/index.d.ts",
"scripts": {
@@ -19,9 +19,13 @@
"size:worker": "echo worker is $(cat esm/worker/xworker.js | brotli | wc -c) bytes once compressed",
"ts": "tsc -p ."
},
"keywords": [],
"author": "",
"license": "ISC",
"keywords": [
"py-script",
"pyscript",
"next"
],
"author": "Anaconda Inc.",
"license": "MIT",
"devDependencies": {
"@node-loader/import-maps": "^1.1.0",
"@rollup/plugin-node-resolve": "^15.1.0",

File diff suppressed because one or more lines are too long

View File

@@ -8,10 +8,14 @@
<script type="module">
// how would PyScript plugins add their own behavior?
import { hooks } from "@pyscript/element";
let counter = 0;
hooks.onBeforeRun.add((pyodide, { localName }) => {
// console.log(++counter, 'elements so far', localName, pyodide);
});
hooks.codeBeforeRunWorker.add('print("codeBeforeRunWorker")');
hooks.codeAfterRunWorker.add('print("codeAfterRunWorker")');
</script>
</head>
<body>