Files
pyscript/pyscript.core/esm/worker/class.js
Andrea Giammarchi 339e40063a WIP: Bringing PyScript.next PoC to the main project (#1507)
* kill unwrapped_remote (#1490)

* kill unwrapped_remote

* linting

* don't use callKwargs for python plugins

* fix tests and improve types

* Bringing PyScript.next PoC to the main project

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Madhur Tandon <20173739+madhur-tandon@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-06-05 21:52:28 +02:00

36 lines
1.4 KiB
JavaScript

import xworker from "./xworker.js";
import { assign, defineProperty, absoluteURL } from "../utils.js";
import { getText } from "../fetch-utils.js";
/**
* @typedef {Object} WorkerOptions plugin configuration
* @prop {string} type the runtime/interpreter type to use
* @prop {string} [version] the optional runtime version to use
* @prop {string} [config] the optional config to use within such runtime
*/
export default (...args) =>
/**
* A XWorker is a Worker facade able to bootstrap a channel with any desired runtime.
* @param {string} url the remote file to evaluate on bootstrap
* @param {WorkerOptions} [options] optional arguments to define the runtime to use
* @returns {Worker}
*/
function XWorker(url, options) {
const worker = xworker();
const { postMessage } = worker;
if (args.length) {
const [type, version] = args;
options = assign({}, options || { type, version });
if (!options.type) options.type = type;
}
if (options?.config) options.config = absoluteURL(options.config);
const bootstrap = fetch(url)
.then(getText)
.then((code) => postMessage.call(worker, { options, code }));
return defineProperty(worker, "postMessage", {
value: (data, ...rest) =>
bootstrap.then(() => postMessage.call(worker, data, ...rest)),
});
};