8000 Add a "donkey" worker that execs or evaluates all the things · pyscript/pyscript@87ade66 · GitHub
[go: up one dir, main page]

Skip to content

Commit 87ade66

Browse files
pre-commit-ci[bot]WebReflection
authored andcommitted
Add a "donkey" worker that execs or evaluates all the things
1 parent 7ef966d commit 87ade66

File tree

2 files changed

+43
-37
lines changed

2 files changed

+43
-37
lines changed

core/src/core.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ import {
3737
import { stdlib, optional } from "./stdlib.js";
3838
export { stdlib, optional, inputFailure };
3939

40-
export const donkey = options => import(
41-
/* webpackIgnore: true */ "./plugins/donkey.js"
42-
).then(module => module.default(options));
40+
export const donkey = (options) =>
41+
import(/* webpackIgnore: true */ "./plugins/donkey.js").then((module) =>
42+
module.default(options),
43+
);
4344

4445
// generic helper to disambiguate between custom element and script
4546
const isScript = ({ tagName }) => tagName === "SCRIPT";

core/src/plugins/donkey.js

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { assign, dedent } from "polyscript/exports";
22

3-
const invoke = (name, args) => `${name}(code, ${args.join(', ')})`;
3+
const invoke = (name, args) => `${name}(code, ${args.join(", ")})`;
44

55
export default (options = {}) => {
6-
const type = options.type || 'py';
7-
const args = options.persistent ? ['globals()', '__locals__'] : ['{}', '{}'];
8-
const src = URL.createObjectURL(new Blob([
9-
dedent(`
6+
const type = options.type || "py";
7+
const args = options.persistent
8+
? ["globals()", "__locals__"]
9+
: ["{}", "{}"];
10+
const src = URL.createObjectURL(
11+
new Blob([
12+
dedent(`
1013
from pyscript import sync, config
1114
__message__ = lambda e,v: f"\x1b[31m\x1b[1m{e.__name__}\x1b[0m: {v}"
1215
__locals__ = {}
@@ -18,42 +21,44 @@ export default (options = {}) => {
1821
else:
1922
__error__ = lambda e: __message__(e.__class__, e.value)
2023
def execute(code):
21-
try: return ${invoke('exec', args)};
24+
try: return ${invoke("exec", args)};
2225
except Exception as e: print(__error__(e));
2326
def evaluate(code):
24-
try: return ${invoke('eval', args)};
27+
try: return ${invoke("eval", args)};
2528
except Exception as e: print(__error__(e));
2629
sync.execute = execute
2730
sync.evaluate = evaluate
28-
`)
29-
]));
31+
`),
32+
]),
33+
);
3034

31-
const script = assign(document.createElement('script'), { type, src });
35+
const script = assign(document.createElement("script"), { type, src });
3236

33-
script.toggleAttribute('worker', true);
34-
script.toggleAttribute('terminal', true);
35-
if (options.terminal) script.setAttribute('target', options.terminal);
36-
if (options.config) script.setAttribute('config', JSON.stringify(options.config));
37+
script.toggleAttribute("worker", true);
38+
script.toggleAttribute("terminal", true);
39+
if (options.terminal) script.setAttribute("target", options.terminal);
40+
if (options.config)
41+
script.setAttribute("config", JSON.stringify(options.config));
3742

38-
return new Promise(resolve => {
39-
script.addEventListener(`${type}:done`, event => {
40-
event.stopPropagation();
41-
URL.revokeObjectURL(src);
42-
const { xworker, process, terminal } = script;
43-
const { execute, evaluate } = xworker.sync;
44-
script.remove();
45-
resolve({
46-
process,
47-
execute: code => execute(dedent(code)),
48-
evaluate: code => evaluate(dedent(code)),
49-
clear: () => terminal.clear(),
50-
reset: () => terminal.reset(),
51-
kill: () => {
52-
xworker.terminate();
53-
terminal.dispose();
54-
},
55-
});
43+
return new Promise((resolve) => {
44+
script.addEventListener(`${type}:done`, (event) => {
45+
event.stopPropagation();
46+
URL.revokeObjectURL(src);
47+
const { xworker, process, terminal } = script;
48+
const { execute, evaluate } = xworker.sync;
49+
script.remove();
50+
resolve({
51+
process,
52+
execute: (code) => execute(dedent(code)),
53+
evaluate: (code) => evaluate(dedent(code)),
54+
clear: () => terminal.clear(),
55+
reset: () => terminal.reset(),
56+
kill: () => {
57+
xworker.terminate();
58+
terminal.dispose();
59+
},
60+
});
61+
});
62+
document.body.append(script);
5663
});
57-
document.body.append(script);
58-
});
5964
};

0 commit comments

Comments
 (0)
0