Skip to content

Commit 3c1fc6c

Browse files
committed
Added multiple run safeguard to prevent too many Promises from crashing the app.
Signed-off-by: ubi de feo <[email protected]>
1 parent 5b81693 commit 3c1fc6c

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

ui/arduino/store.js

+32-2
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,15 @@ async function store(state, emitter) {
228228
})
229229

230230
// CODE EXECUTION
231+
emitter.on('run-from-button', (onlySelected = false) => {
232+
if (onlySelected) {
233+
runCodeSelection()
234+
} else {
235+
runCode()
236+
}
237+
})
238+
239+
231240
emitter.on('run', async (onlySelected = false) => {
232241
log('run')
233242
const openFile = state.openFiles.find(f => f.id == state.editingFile)
@@ -1506,14 +1515,35 @@ async function store(state, emitter) {
15061515
emitter.emit('render')
15071516
}
15081517

1518+
// Ensures that even if the RUN button is clicked multiple times
1519+
// there's a 100ms delay between each execution to prevent double runs
1520+
// and entering an unstable state because of getPrompt() calls
1521+
let preventDoubleRun = false
1522+
function timedReset() {
1523+
preventDoubleRun = true
1524+
setTimeout(() => {
1525+
preventDoubleRun = false
1526+
}, 500);
1527+
1528+
}
1529+
1530+
function filterDoubleRun(onlySelected = false) {
1531+
if (preventDoubleRun) return
1532+
console.log('>>> RUN CODE ACTUAL <<<')
1533+
emitter.emit('run', onlySelected)
1534+
timedReset()
1535+
}
1536+
15091537
function runCode() {
1538+
console.log('>>> RUN CODE REQUEST <<<')
15101539
if (canExecute({ view: state.view, isConnected: state.isConnected })) {
1511-
emitter.emit('run')
1540+
filterDoubleRun()
15121541
}
15131542
}
15141543
function runCodeSelection() {
1544+
console.log('>>> RUN CODE REQUEST <<<')
15151545
if (canExecute({ view: state.view, isConnected: state.isConnected })) {
1516-
emitter.emit('run', true)
1546+
filterDoubleRun(true)
15171547
}
15181548
}
15191549
function stopCode() {

ui/arduino/views/components/elements/button.js

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ function Button(args) {
1111
label,
1212
background
1313
} = args
14+
15+
1416
let tooltipEl = html``
1517
if (tooltip) {
1618
tooltipEl = html`<div class="tooltip">${tooltip}</div>`

ui/arduino/views/components/toolbar.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ function Toolbar(state, emit) {
3838
disabled: !_canExecute,
3939
onClick: (e) => {
4040
if (e.altKey) {
41-
emit('run', true)
41+
emit('run-from-button', true)
4242
}else{
43-
emit('run')
43+
emit('run-from-button')
4444
}
4545
}
4646
})}

0 commit comments

Comments
 (0)