Skip to content

Commit a0c2ec0

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

File tree

2 files changed

+51
-48
lines changed

2 files changed

+51
-48
lines changed

js/activity.js

Lines changed: 48 additions & 44 deletions
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);
@@ -2253,7 +2259,7 @@ class Activity {
22532259
const changeText = () => {
22542260
const randomLoadMessage =
22552261
messages.load_messages[
2256-
Math.floor(Math.random() * messages.load_messages.length)
2262+
Math.floor(Math.random() * messages.load_messages.length)
22572263
];
22582264
document.getElementById("messageText").innerHTML = randomLoadMessage + "...";
22592265
counter++;
@@ -3427,9 +3433,9 @@ class Activity {
34273433
that.blocks.moveBlock(
34283434
newBlock,
34293435
(x || that.blocksContainer.x + 100) -
3430-
that.blocksContainer.x,
3436+
that.blocksContainer.x,
34313437
(y || that.blocksContainer.y + 100) -
3432-
that.blocksContainer.y
3438+
that.blocksContainer.y
34333439
);
34343440
}
34353441
);
@@ -4902,8 +4908,8 @@ class Activity {
49024908
console.log(
49034909
"%cMusic Blocks",
49044910
"font-size: 24px; font-weight: bold; font-family: sans-serif; padding:20px 0 0 110px; background: url(" +
4905-
imgUrl +
4906-
") no-repeat;"
4911+
imgUrl +
4912+
") no-repeat;"
49074913
);
49084914
// eslint-disable-next-line no-console
49094915
console.log(
@@ -4975,10 +4981,10 @@ class Activity {
49754981
typeof flags !== "undefined"
49764982
? flags
49774983
: {
4978-
run: false,
4979-
show: false,
4980-
collapse: false
4981-
};
4984+
run: false,
4985+
show: false,
4986+
collapse: false
4987+
};
49824988
this.loading = true;
49834989
document.body.style.cursor = "wait";
49844990
this.doLoadAnimation();
@@ -5341,9 +5347,8 @@ class Activity {
53415347
[
53425348
"nameddo",
53435349
{
5344-
value: `V: ${parseInt(lineId) + 1} Line ${
5345-
staffBlocksMap[lineId]?.baseBlocks?.length + 1
5346-
}`
5350+
value: `V: ${parseInt(lineId) + 1} Line ${staffBlocksMap[lineId]?.baseBlocks?.length + 1
5351+
}`
53475352
}
53485353
],
53495354
0,
@@ -5352,12 +5357,12 @@ class Activity {
53525357
staffBlocksMap[lineId].baseBlocks.length === 0
53535358
? null
53545359
: staffBlocksMap[lineId].baseBlocks[
5355-
staffBlocksMap[lineId].baseBlocks.length - 1
5356-
][0][
5357-
staffBlocksMap[lineId].baseBlocks[
5358-
staffBlocksMap[lineId].baseBlocks.length - 1
5359-
][0].length - 4
5360-
][0],
5360+
staffBlocksMap[lineId].baseBlocks.length - 1
5361+
][0][
5362+
staffBlocksMap[lineId].baseBlocks[
5363+
staffBlocksMap[lineId].baseBlocks.length - 1
5364+
][0].length - 4
5365+
][0],
53615366
null
53625367
]
53635368
],
@@ -5373,9 +5378,8 @@ class Activity {
53735378
[
53745379
"text",
53755380
{
5376-
value: `V: ${parseInt(lineId) + 1} Line ${
5377-
staffBlocksMap[lineId]?.baseBlocks?.length + 1
5378-
}`
5381+
value: `V: ${parseInt(lineId) + 1} Line ${staffBlocksMap[lineId]?.baseBlocks?.length + 1
5382+
}`
53795383
}
53805384
],
53815385
0,
@@ -5410,14 +5414,14 @@ class Activity {
54105414
staffBlocksMap[staffIndex].startBlock.length - 3
54115415
][4][2] =
54125416
staffBlocksMap[staffIndex].baseBlocks[0][0][
5413-
staffBlocksMap[staffIndex].baseBlocks[0][0].length - 4
5417+
staffBlocksMap[staffIndex].baseBlocks[0][0].length - 4
54145418
][0];
54155419
// Update the first namedo block with settimbre
54165420
staffBlocksMap[staffIndex].baseBlocks[0][0][
54175421
staffBlocksMap[staffIndex].baseBlocks[0][0].length - 4
54185422
][4][0] =
54195423
staffBlocksMap[staffIndex].startBlock[
5420-
staffBlocksMap[staffIndex].startBlock.length - 3
5424+
staffBlocksMap[staffIndex].startBlock.length - 3
54215425
][0];
54225426
const repeatblockids = staffBlocksMap[staffIndex].repeatArray;
54235427
for (const repeatId of repeatblockids) {
@@ -5429,7 +5433,7 @@ class Activity {
54295433
0,
54305434
[
54315435
staffBlocksMap[staffIndex].startBlock[
5432-
staffBlocksMap[staffIndex].startBlock.length - 3
5436+
staffBlocksMap[staffIndex].startBlock.length - 3
54335437
][0] /*setribmre*/,
54345438
blockId + 1,
54355439
staffBlocksMap[staffIndex].nameddoArray[staffIndex][0],
@@ -5438,8 +5442,8 @@ class Activity {
54385442
] === null
54395443
? null
54405444
: staffBlocksMap[staffIndex].nameddoArray[staffIndex][
5441-
repeatId.end + 1
5442-
]
5445+
repeatId.end + 1
5446+
]
54435447
]
54445448
]);
54455449
staffBlocksMap[staffIndex].repeatBlock.push([
@@ -5473,7 +5477,7 @@ class Activity {
54735477
const secondnammedo = _searchIndexForMusicBlock(
54745478
staffBlocksMap[staffIndex].baseBlocks[repeatId.end + 1][0],
54755479
staffBlocksMap[staffIndex].nameddoArray[staffIndex][
5476-
repeatId.end + 1
5480+
repeatId.end + 1
54775481
]
54785482
);
54795483

@@ -5496,31 +5500,31 @@ class Activity {
54965500
const prevnameddo = _searchIndexForMusicBlock(
54975501
staffBlocksMap[staffIndex].baseBlocks[repeatId.start - 1][0],
54985502
staffBlocksMap[staffIndex].baseBlocks[repeatId.start][0][
5499-
currentnammeddo
5503+
currentnammeddo
55005504
][4][0]
55015505
);
55025506
const afternamedo = _searchIndexForMusicBlock(
55035507
staffBlocksMap[staffIndex].baseBlocks[repeatId.end][0],
55045508
staffBlocksMap[staffIndex].baseBlocks[repeatId.start][0][
5505-
currentnammeddo
5509+
currentnammeddo
55065510
][4][1]
55075511
);
55085512
let prevrepeatnameddo = -1;
55095513
if (prevnameddo === -1) {
55105514
prevrepeatnameddo = _searchIndexForMusicBlock(
55115515
staffBlocksMap[staffIndex].repeatBlock,
55125516
staffBlocksMap[staffIndex].baseBlocks[repeatId.start][0][
5513-
currentnammeddo
5517+
currentnammeddo
55145518
][4][0]
55155519
);
55165520
}
55175521
const prevBlockId =
55185522
staffBlocksMap[staffIndex].baseBlocks[repeatId.start][0][
5519-
currentnammeddo
5523+
currentnammeddo
55205524
][4][0];
55215525
const currentBlockId =
55225526
staffBlocksMap[staffIndex].baseBlocks[repeatId.start][0][
5523-
currentnammeddo
5527+
currentnammeddo
55245528
][0];
55255529

55265530
// Needs null checking optmizie
@@ -5534,7 +5538,7 @@ class Activity {
55345538
0,
55355539
[
55365540
staffBlocksMap[staffIndex].baseBlocks[repeatId.start][0][
5537-
currentnammeddo
5541+
currentnammeddo
55385542
][4][0],
55395543
blockId + 1,
55405544
currentBlockId,
@@ -6832,12 +6836,12 @@ class Activity {
68326836
return $j("<li></li>")
68336837
.append(
68346838
'<img src="' +
6835-
(item.artwork || "") +
6836-
'" height = "20px">' +
6837-
"<a>" +
6838-
" " +
6839-
item.label +
6840-
"</a>"
6839+
(item.artwork || "") +
6840+
'" height = "20px">' +
6841+
"<a>" +
6842+
" " +
6843+
item.label +
6844+
"</a>"
68416845
)
68426846
.appendTo(ul.css("z-index", 35000));
68436847
};
@@ -6961,10 +6965,10 @@ class Activity {
69616965
container.setAttribute(
69626966
"style",
69636967
"position: absolute; right:" +
6964-
(document.body.clientWidth - x) +
6965-
"px; top: " +
6966-
y +
6967-
"px;"
6968+
(document.body.clientWidth - x) +
6969+
"px; top: " +
6970+
y +
6971+
"px;"
69686972
);
69696973
document.getElementById("buttoncontainerBOTTOM").appendChild(container);
69706974
return container;

js/logo.js

Lines changed: 3 additions & 4 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.
@@ -1710,7 +1709,7 @@ class Logo {
17101709
tur.parentFlowQueue.length > 0 &&
17111710
tur.queue.length > 0 &&
17121711
logo.deps.utils.last(tur.queue).parentBlk !==
1713-
logo.deps.utils.last(tur.parentFlowQueue)
1712+
logo.deps.utils.last(tur.parentFlowQueue)
17141713
) {
17151714
tur.unhighlightQueue.push(logo.deps.utils.last(tur.parentFlowQueue));
17161715
} else if (tur.unhighlightQueue.length > 0) {
@@ -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)