mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-20 10:47:35 -05:00
Compare commits
3 Commits
2024.10.2
...
better-don
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a41b3328aa | ||
|
|
3e55024351 | ||
|
|
5f526496f4 |
@@ -2,7 +2,7 @@ import { assign, dedent } from "polyscript/exports";
|
|||||||
|
|
||||||
const invoke = (name, args) => `${name}(code, ${args.join(", ")})`;
|
const invoke = (name, args) => `${name}(code, ${args.join(", ")})`;
|
||||||
|
|
||||||
export default (options = {}) => {
|
const donkey = (options = {}) => {
|
||||||
const type = options.type || "py";
|
const type = options.type || "py";
|
||||||
const args = options.persistent
|
const args = options.persistent
|
||||||
? ["globals()", "__locals__"]
|
? ["globals()", "__locals__"]
|
||||||
@@ -44,21 +44,55 @@ export default (options = {}) => {
|
|||||||
script.addEventListener(`${type}:done`, (event) => {
|
script.addEventListener(`${type}:done`, (event) => {
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
URL.revokeObjectURL(src);
|
URL.revokeObjectURL(src);
|
||||||
|
let executed = true;
|
||||||
|
let evaluated = true;
|
||||||
const { xworker, process, terminal } = script;
|
const { xworker, process, terminal } = script;
|
||||||
const { execute, evaluate } = xworker.sync;
|
const { execute, evaluate } = xworker.sync;
|
||||||
script.remove();
|
const kill = () => {
|
||||||
resolve({
|
xworker.terminate();
|
||||||
|
terminal.dispose();
|
||||||
|
};
|
||||||
|
const farmer = {
|
||||||
process,
|
process,
|
||||||
execute: (code) => execute(dedent(code)),
|
execute: async (code) => {
|
||||||
evaluate: (code) => evaluate(dedent(code)),
|
if (!executed) {
|
||||||
|
kill();
|
||||||
|
assign(farmer, await donkey(options));
|
||||||
|
return farmer.execute(code);
|
||||||
|
}
|
||||||
|
executed = false;
|
||||||
|
try {
|
||||||
|
const result = await execute(dedent(code));
|
||||||
|
executed = true;
|
||||||
|
return result;
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
evaluate: async (code) => {
|
||||||
|
if (!evaluated) {
|
||||||
|
kill();
|
||||||
|
assign(farmer, await donkey(options));
|
||||||
|
return farmer.evaluate(code);
|
||||||
|
}
|
||||||
|
evaluated = false;
|
||||||
|
try {
|
||||||
|
const result = await evaluate(dedent(code));
|
||||||
|
evaluated = true;
|
||||||
|
return result;
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
},
|
||||||
clear: () => terminal.clear(),
|
clear: () => terminal.clear(),
|
||||||
reset: () => terminal.reset(),
|
reset: () => terminal.reset(),
|
||||||
kill: () => {
|
kill,
|
||||||
xworker.terminate();
|
};
|
||||||
terminal.dispose();
|
resolve(farmer);
|
||||||
},
|
script.remove();
|
||||||
});
|
|
||||||
});
|
});
|
||||||
document.body.append(script);
|
document.body.append(script);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export default donkey;
|
||||||
|
|||||||
4
core/types/plugins/donkey.d.ts
vendored
4
core/types/plugins/donkey.d.ts
vendored
@@ -1,2 +1,2 @@
|
|||||||
declare function _default(options?: {}): Promise<any>;
|
export default donkey;
|
||||||
export default _default;
|
declare function donkey(options?: {}): Promise<any>;
|
||||||
|
|||||||
Reference in New Issue
Block a user