Skip to content

Commit 942cef4

Browse files
author
gourijain026@gmail.com
committed
Fix race condition and defensive checks for spam-clicking
1 parent fa0ed73 commit 942cef4

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

js/activity.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1655,7 +1655,7 @@ class Activity {
16551655

16561656
this._doFastButton = env => {
16571657
// Prevent spam-clicking by checking if already running
1658-
if (this.logo._alreadyRunning) {
1658+
if (this.logo._alreadyRunning || this._restartPending) {
16591659
return;
16601660
}
16611661

@@ -1691,11 +1691,17 @@ class Activity {
16911691
this.logo.step();
16921692
} else {
16931693
// Stop and restart.
1694+
if (this._restartPending) {
1695+
return;
1696+
}
1697+
16941698
document.getElementById("stop").style.color = "white";
16951699
this.logo.doStopTurtles();
16961700

1701+
this._restartPending = true;
16971702
const that = this;
16981703
setTimeout(() => {
1704+
this._restartPending = false;
16991705
document.getElementById("stop").style.color = "#ea174c";
17001706
that.logo.runLogoCommands(null, env);
17011707
}, 500);

js/logo.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,7 @@ class Logo {
10981098
*/
10991099
runLogoCommands(startHere, env) {
11001100
this._prematureRestart = this._alreadyRunning;
1101+
this._alreadyRunning = true;
11011102
if (this._alreadyRunning && this._runningBlock !== null) {
11021103
this._ignoringBlock = this._runningBlock;
11031104
} else {
@@ -1404,8 +1405,6 @@ class Logo {
14041405
* @returns {void}
14051406
*/
14061407
runFromBlockNow(logo, turtle, blk, isflow, receivedArg, queueStart) {
1407-
this._alreadyRunning = true;
1408-
14091408
this.receivedArg = receivedArg;
14101409

14111410
// Sometimes we don't want to unwind the entire queue.
@@ -1760,7 +1759,7 @@ class Logo {
17601759
if (!logo._prematureRestart) {
17611760
// Make sure any unissued signals are dispatched.
17621761
for (const b in tur.endOfClampSignals) {
1763-
const signalsLength = tur.endOfClampSignals[b].length;
1762+
const signalsLength = (tur.endOfClampSignals[b] && tur.endOfClampSignals[b].length) || 0;
17641763
for (let i = 0; i < signalsLength; i++) {
17651764
if (tur.endOfClampSignals[b][i] != null) {
17661765
if (

0 commit comments

Comments
 (0)