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", "name": "@pyscript/core",
"version": "0.6.5", "version": "0.6.6",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@pyscript/core", "name": "@pyscript/core",
"version": "0.6.5", "version": "0.6.6",
"license": "APACHE-2.0", "license": "APACHE-2.0",
"dependencies": { "dependencies": {
"@ungap/with-resolvers": "^0.1.0", "@ungap/with-resolvers": "^0.1.0",

View File

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

View File

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

View File

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

View File

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

View File

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