Donkey clear and reset now terminate when busy (#2225)

* Donkey clear and reset now terminate when busy
This commit is contained in:
Andrea Giammarchi
2024-10-15 12:56:11 +02:00
committed by GitHub
parent b1c33b7f79
commit c3517f7973
6 changed files with 38 additions and 24 deletions

View File

@@ -1,12 +1,12 @@
{
"name": "@pyscript/core",
"version": "0.6.5",
"version": "0.6.6",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@pyscript/core",
"version": "0.6.5",
"version": "0.6.6",
"license": "APACHE-2.0",
"dependencies": {
"@ungap/with-resolvers": "^0.1.0",

View File

@@ -1,6 +1,6 @@
{
"name": "@pyscript/core",
"version": "0.6.5",
"version": "0.6.6",
"type": "module",
"description": "PyScript",
"module": "./index.js",

View File

@@ -73,13 +73,22 @@ const utils = async (options) => {
export default async (options = {}) => {
let farmer = await utils(options);
let working = false;
const kill = () => {
if (farmer) {
farmer.xworker.terminate();
farmer.terminal.dispose();
farmer = null;
}
working = false;
};
const reload = async () => {
kill();
farmer = await utils(options);
};
const asyncTask = (method) => async (code) => {
// race condition ... a new task has been
// assigned while the previous one didn't finish
if (working) {
kill();
farmer = await utils(options);
}
if (working) await reload();
working = true;
try {
return await farmer[method](dedent(code));
@@ -89,20 +98,16 @@ export default async (options = {}) => {
working = false;
}
};
const kill = () => {
if (farmer) {
farmer.xworker.terminate();
farmer.terminal.dispose();
farmer = null;
working = false;
}
const asyncMethod = (method) => async () => {
if (working) await reload();
else farmer?.terminal[method]();
};
return {
process: asyncTask("process"),
execute: asyncTask("execute"),
evaluate: asyncTask("evaluate"),
clear: () => farmer?.terminal.clear(),
reset: () => farmer?.terminal.reset(),
clear: asyncMethod("clear"),
reset: asyncMethod("reset"),
kill,
};
};

View File

@@ -12,13 +12,23 @@ const {
kill,
} = await donkey({ terminal: '#container' });
clearButton.onclick = clear;
killButton.onclick = kill;
clearButton.onclick = async () => {
killButton.disabled = true;
clearButton.disabled = true;
await clear();
runButton.disabled = false;
};
killButton.onclick = () => {
killButton.disabled = true;
clearButton.disabled = true;
runButton.disabled = true;
kill();
};
runButton.disabled = false;
runButton.onclick = async () => {
killButton.disabled = false;
clearButton.disabled = true;
clearButton.disabled = false;
runButton.disabled = true;
// multiline code
await execute(`
@@ -29,6 +39,5 @@ runButton.onclick = async () => {
const name = await evaluate('input("what is your name? ")');
alert(`Hello ${name}`);
killButton.disabled = true;
clearButton.disabled = false;
runButton.disabled = false;
};

View File

@@ -2,8 +2,8 @@ export function donkey(options: any): Promise<{
process: (code: any) => Promise<any>;
execute: (code: any) => Promise<any>;
evaluate: (code: any) => Promise<any>;
clear: () => any;
reset: () => any;
clear: () => Promise<void>;
reset: () => Promise<void>;
kill: () => void;
}>;
export function offline_interpreter(config: any): string;

View File

@@ -2,8 +2,8 @@ declare function _default(options?: {}): Promise<{
process: (code: any) => Promise<any>;
execute: (code: any) => Promise<any>;
evaluate: (code: any) => Promise<any>;
clear: () => any;
reset: () => any;
clear: () => Promise<void>;
reset: () => Promise<void>;
kill: () => void;
}>;
export default _default;