Skip to content

Commit

Permalink
Automatically run the code on input events, if there are no globals
Browse files Browse the repository at this point in the history
  • Loading branch information
rossant committed Mar 13, 2024
1 parent 299beed commit e7cf24a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
15 changes: 13 additions & 2 deletions scripts/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Runner {

this.pyodide = null;
this.globals = null;
this.hasGlobals = false;
this.outputElement = document.getElementById("terminal-output");

this.interval = DEFAULT_INTERVAL;
Expand Down Expand Up @@ -61,6 +62,7 @@ class Runner {

// Clear the global variables.
this.globals = this.pyodide.toPy({});
this.hasGlobals = false;
}

clearGrid() {
Expand Down Expand Up @@ -200,6 +202,7 @@ class Runner {

// Run the code.
let out = await this._run(code);
this.hasGlobals = true;

// If there is a frame function, start the animation.
this.tryPlay();
Expand Down Expand Up @@ -247,14 +250,22 @@ class Runner {
/* Input events */
/*********************************************************************************************/

_firstRun() {
// Run the code if it hasn't run yet.
if (!this.hasGlobals) {
const code = this.model.editor.getCode();
this.start(code);
}
}

click(row, col) {
// console.log("click", row, col);
this._firstRun();
if (this.has("click"))
this._run(`click(${row}, ${col})`, false, false, false, false);
}

keyboard(key) {
// console.log("keyboard", key);
this._firstRun();
if (this.has("keyboard"))
this._run(`keyboard("${key}")`, false, false, false, false);
}
Expand Down
2 changes: 0 additions & 2 deletions scripts/spinner.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ class Spinner {
this.model = model;
this.dispatcher = dispatcher;

// this.isSpinning = false;
this.setupDispatcher();
}

setSpinning(isSpinning) {
// this.isSpinning = isSpinning;
if (isSpinning)
document.body.classList.add('waiting');
else
Expand Down

0 comments on commit e7cf24a

Please sign in to comment.