Skip to content

Commit

Permalink
Added a worker attribute to simplify interpreters in workers
Browse files Browse the repository at this point in the history
  • Loading branch information
WebReflection committed Aug 5, 2023
1 parent e5f0104 commit 257e94a
Show file tree
Hide file tree
Showing 8 changed files with 1,918 additions and 9 deletions.
2 changes: 1 addition & 1 deletion dev.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const build = (fileName) => {
() =>
new Promise((resolve, reject) => {
exec(
"npm run rollup:xworker && npm run rollup:core && npm run rollup:pyscript",
"npm run rollup:xworker && npm run rollup:core",
{ cwd: __dirname, env: { ...process.env, NO_MIN: true } },
(error) => {
if (error) reject(error);
Expand Down
1,864 changes: 1,862 additions & 2 deletions docs/core.js

Large diffs are not rendered by default.

17 changes: 16 additions & 1 deletion esm/custom.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import '@ungap/with-resolvers';
import { $$ } from 'basic-devtools';

import { assign, create } from './utils.js';
import { assign, create, defineProperty } from './utils.js';
import { getDetails } from './script-handler.js';
import { registry as defaultRegistry, prefixes, configs } from './interpreters.js';
import { getRuntimeID } from './loader.js';
import { io } from './interpreter/_utils.js';
import { addAllListeners } from './listeners.js';
import { Hook } from './worker/hooks.js';
import { XWorker } from './index.js';

export const CUSTOM_SELECTORS = [];

Expand Down Expand Up @@ -44,6 +45,20 @@ export const handleCustomType = (node) => {
env,
onInterpreterReady,
} = options;

const worker = node.attributes.worker?.value || '';
if (worker) {
const xworker = XWorker.call(new Hook(null, options), worker, {
config,
version,
type: runtime,
async: node.hasAttribute('async')
});
defineProperty(node, 'xworker', { value: xworker });
resolve({ type, xworker });
return;
}

const name = getRuntimeID(runtime, version);
const id = env || `${name}${config ? `|${config}` : ''}`;
const { interpreter: engine, XWorker: Worker } = getDetails(
Expand Down
24 changes: 20 additions & 4 deletions esm/script-handler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { $ } from 'basic-devtools';

import xworker from './worker/class.js';
import $xworker from './worker/class.js';
import { getRuntime, getRuntimeID } from './loader.js';
import { registry } from './interpreters.js';
import { all, resolve, defineProperty } from './utils.js';
Expand Down Expand Up @@ -76,7 +76,7 @@ export const getDetails = (type, id, name, version, config) => {
const details = {
interpreter: getRuntime(name, config),
queue: resolve(),
XWorker: xworker(type, version),
XWorker: $xworker(type, version),
};
interpreters.set(id, details);
// enable sane defaults when single interpreter *of kind* is used in the page
Expand Down Expand Up @@ -107,16 +107,32 @@ export const handle = async (script) => {
// allow a shared config among scripts, beside interpreter,
// and/or source code with different config or interpreter
const {
attributes: { async: isAsync, config, env, target, version },
attributes: { async: isAsync, config, env, target, version, worker },
src,
type,
} = script;

const versionValue = version?.value;
const name = getRuntimeID(type, versionValue);
const targetValue = getValue(target, '');
let configValue = getValue(config, '|');
const id = getValue(env, '') || `${name}${configValue}`;
configValue = configValue.slice(1);

const workerValue = worker?.value;
if (workerValue) {
const XWorker = $xworker(type, versionValue);
const xworker = new XWorker(workerValue, {
async: !!isAsync,
config: configValue
});
handled.set(
defineProperty(script, 'xworker', { value: xworker }),
{ xworker }
);
return;
}

const targetValue = getValue(target, '');
const details = getDetails(type, id, name, versionValue, configValue);

handled.set(
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"dev": "node dev.cjs",
"rollup:core": "rollup --config rollup/core.config.js",
"rollup:integrations": "node rollup/build_integrations.cjs",
"rollup:pyscript": "rollup --config rollup/pyscript.config.js",
"rollup:xworker": "rollup --config rollup/xworker.config.js",
"test": "c8 --100 node --experimental-loader @node-loader/import-maps test/index.js && npm run test:integration",
"test:html": "npm run test && c8 report -r html",
Expand Down
2 changes: 2 additions & 0 deletions test/plugins/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
whenDefined("mpy").then(console.log);
define("mpy", {
interpreter: "micropython",
config: "../fetch.toml",
async onInterpreterReady(micropython, element) {
console.log(micropython);
// Somehow this doesn't work in MicroPython
Expand All @@ -36,6 +37,7 @@
</script>
</head>
<body>
<!-- <mpy-script worker="./worker.py"></mpy-script> -->
<mpy-script mpy-click="test_click">
def test_click(event):
print(event.type)
Expand Down
11 changes: 11 additions & 0 deletions test/plugins/worker.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<script type="module" src="../../core.js"></script>
</head>
<body>
<script type="micropython" worker="./worker.py" config="../fetch.toml"></script>
</body>
</html>
6 changes: 6 additions & 0 deletions test/plugins/worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from polyscript import xworker

print('hello from worker')
xworker.window.document.body.textContent = 'hello from worker'

import a

0 comments on commit 257e94a

Please sign in to comment.