diff --git a/core/package-lock.json b/core/package-lock.json index ddb90c59..88970054 100644 --- a/core/package-lock.json +++ b/core/package-lock.json @@ -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", diff --git a/core/package.json b/core/package.json index 5f8dd84e..26c13526 100644 --- a/core/package.json +++ b/core/package.json @@ -1,6 +1,6 @@ { "name": "@pyscript/core", - "version": "0.6.5", + "version": "0.6.6", "type": "module", "description": "PyScript", "module": "./index.js", diff --git a/core/src/plugins/donkey.js b/core/src/plugins/donkey.js index 78aaa8cd..617c9a8f 100644 --- a/core/src/plugins/donkey.js +++ b/core/src/plugins/donkey.js @@ -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, }; }; diff --git a/core/tests/manual/donkey/index.js b/core/tests/manual/donkey/index.js index a86f9f23..870a714b 100644 --- a/core/tests/manual/donkey/index.js +++ b/core/tests/manual/donkey/index.js @@ -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; }; diff --git a/core/types/core.d.ts b/core/types/core.d.ts index 38d07b37..998e5ea8 100644 --- a/core/types/core.d.ts +++ b/core/types/core.d.ts @@ -2,8 +2,8 @@ export function donkey(options: any): Promise<{ process: (code: any) => Promise; execute: (code: any) => Promise; evaluate: (code: any) => Promise; - clear: () => any; - reset: () => any; + clear: () => Promise; + reset: () => Promise; kill: () => void; }>; export function offline_interpreter(config: any): string; diff --git a/core/types/plugins/donkey.d.ts b/core/types/plugins/donkey.d.ts index 7c8d0037..f328f997 100644 --- a/core/types/plugins/donkey.d.ts +++ b/core/types/plugins/donkey.d.ts @@ -2,8 +2,8 @@ declare function _default(options?: {}): Promise<{ process: (code: any) => Promise; execute: (code: any) => Promise; evaluate: (code: any) => Promise; - clear: () => any; - reset: () => any; + clear: () => Promise; + reset: () => Promise; kill: () => void; }>; export default _default;