8000 Donkey clear and reset now terminate when busy (#2225) · pyscript/pyscript@c3517f7 · GitHub
[go: up one dir, main page]

Skip to content

Commit c3517f7

Browse files
Donkey clear and reset now terminate when busy (#2225)
* Donkey clear and reset now terminate when busy
1 parent b1c33b7 commit c3517f7

File tree

6 files changed

+38
-24
lines changed

6 files changed

+38
-24
lines changed

core/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pyscript/core",
3-
"version": "0.6.5",
3+
"version": "0.6.6",
44
"type": "module",
55
"description": "PyScript",
66
"module": "./index.js",

core/src/plugins/donkey.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,22 @@ const utils = async (options) => {
7373
export default async (options = {}) => {
7474
let farmer = await utils(options);
7575
let working = false;
76+
const kill = () => {
77+
if (farmer) {
78+
farmer.xworker.terminate();
79+
farmer.terminal.dispose();
80+
farmer = null;
81+
}
82+
working = false;
83+
};
84+
const reload = async () => {
85+
kill();
86+
farmer = await utils(options);
87+
};
7688
const asyncTask = (method) => async (code) => {
7789
// race condition ... a new task has been
7890
// assigned while the previous one didn't finish
79-
if (working) {
80-
kill();
81-
farmer = await utils(options);
82-
}
91+
if (working) await reload();
8392
working = true;
8493
try {
8594
return await farmer[method](dedent(code));
@@ -89,20 +98,16 @@ export default async (options = {}) => {
8998
working = false;
9099
}
91100
};
92-
const kill = () => {
93-
if (farmer) {
94-
farmer.xworker.terminate();
95-
farmer.terminal.dispose();
96-
farmer = null;
97-
working = false;
98-
}
101+
const asyncMethod = (method) => async () => {
102+
if (working) await reload();
103+
else farmer?.terminal[method]();
99104
};
100105
return {
101106
process: asyncTask("process"),
102107
execute: asyncTask("execute"),
103108
evaluate: asyncTask("evaluate"),
104-
clear: () => farmer?.terminal.clear(),
105-
reset: () => farmer?.terminal.reset(),
109+
clear: asyncMethod("clear"),
110+
reset: asyncMethod("reset"),
106111
kill,
107112
};
108113
};

core/tests/manual/donkey/index.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,23 @@ const {
1212
kill,
1313
} = await donkey({ terminal: '#container' });
1414

15-
clearButton.onclick = clear;
16-
killButton.onclick = kill;
15+
clearButton.onclick = async () => {
16+
killButton.disabled = true;
17+
clearButton.disabled = true;
18+
await clear();
19+
runButton.disabled = false;
20+
};
21+
killButton.onclick = () => {
22+
killButton.disabled = true;
23+
clearButton.disabled = true;
24+
runButton.disabled = true;
25+
kill();
26+
};
1727

1828
runButton.disabled = false;
1929
runButton.onclick = async () => {
2030
killButton.disabled = false;
21-
clearButton.disabled = true;
31+
clearButton.disabled = false;
2232
runButton.disabled = true;
2333
// multiline code
2434
await execute(`
@@ -29,6 +39,5 @@ runButton.onclick = async () => {
2939
const name = await evaluate('input("what is your name? ")');
3040
alert(`Hello ${name}`);
3141
killButton.disabled = true;
32-
clearButton.disabled = false;
3342
runButton.disabled = false;
3443
};

core/types/core.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ export function donkey(options: any): Promise<{
22
process: (code: any) => Promise<any>;
33
execute: (code: any) => Promise<any>;
44
evaluate: (code: any) => Promise<any>;
5-
clear: () => any;
6-
reset: () => any;
5+
clear: () => Promise<void>;
6+
reset: () => Promise<void>;
77
kill: () => void;
88
}>;
99
export function offline_interpreter(config: any): string;

core/types/plugins/donkey.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ declare function _default(options?: {}): Promise<{
22
process: (code: any) => Promise<any>;
33
execute: (code: any) => Promise<any>;
44
evaluate: (code: any) => Promise<any>;
5-
clear: () => any;
6-
reset: () => any;
5+
clear: () => Promise<void>;
6+
reset: () => Promise<void>;
77
kill: () => void;
88
}>;
99
export default _default;

0 commit comments

Comments
 (0)
0