Skip to content

Commit 81897ff

Browse files
committed
fix: call functions inside setTimeout callbacks, not immediately
Fixed 3 occurrences where functions were being executed immediately instead of being passed as callbacks to setTimeout. 1. js/widgets/rhythmruler.js: - setTimeout(this._calculateZebraStripes(id), 1000) + setTimeout(() => this._calculateZebraStripes(id), 1000) Impact: Prevents heavy DOM manipulation from blocking the UI thread synchronously when pausing playback. 2. js/widgets/modewidget.js: - setTimeout(this._resetNotes(), 500) + setTimeout(() => this._resetNotes(), 500) Impact: Ensures note highlights persist for the intended duration instead of disappearing instantly. 3. js/widgets/modewidget.js: - setTimeout(this.widgetWindow.sendToCenter, 0) + setTimeout(() => this.widgetWindow.sendToCenter(), 0) Impact: Preserves 'this' context for the sendToCenter method.
1 parent da12f94 commit 81897ff

File tree

2 files changed

+43
-61
lines changed

2 files changed

+43
-61
lines changed

js/widgets/modewidget.js

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,11 @@ class ModeWidget {
105105
}
106106
};
107107

108-
this.widgetWindow.addButton(
109-
"export-chunk.svg",
110-
ModeWidget.ICONSIZE,
111-
_("Save")
112-
).onclick = this._save.bind(this);
108+
this.widgetWindow.addButton("export-chunk.svg", ModeWidget.ICONSIZE, _("Save")).onclick =
109+
this._save.bind(this);
113110

114-
this.widgetWindow.addButton(
115-
"erase-button.svg",
116-
ModeWidget.ICONSIZE,
117-
_("Clear")
118-
).onclick = this._clear.bind(this);
111+
this.widgetWindow.addButton("erase-button.svg", ModeWidget.ICONSIZE, _("Clear")).onclick =
112+
this._clear.bind(this);
119113

120114
this.widgetWindow.addButton(
121115
"rotate-left.svg",
@@ -129,17 +123,11 @@ class ModeWidget {
129123
_("Rotate clockwise")
130124
).onclick = this._rotateRight.bind(this);
131125

132-
this.widgetWindow.addButton(
133-
"invert.svg",
134-
ModeWidget.ICONSIZE,
135-
_("Invert")
136-
).onclick = this._invert.bind(this);
126+
this.widgetWindow.addButton("invert.svg", ModeWidget.ICONSIZE, _("Invert")).onclick =
127+
this._invert.bind(this);
137128

138-
this.widgetWindow.addButton(
139-
"restore-button.svg",
140-
ModeWidget.ICONSIZE,
141-
_("Undo")
142-
).onclick = this._undo.bind(this);
129+
this.widgetWindow.addButton("restore-button.svg", ModeWidget.ICONSIZE, _("Undo")).onclick =
130+
this._undo.bind(this);
143131

144132
this._piemenuMode();
145133

@@ -157,7 +145,7 @@ class ModeWidget {
157145

158146
//.TRANS: A circle of notes represents the musical mode.
159147
activity.textMsg(_("Click in the circle to select notes for the mode."), 3000);
160-
setTimeout(this.widgetWindow.sendToCenter, 0);
148+
setTimeout(() => this.widgetWindow.sendToCenter(), 0);
161149
}
162150

163151
/**
@@ -689,7 +677,7 @@ class ModeWidget {
689677
this.__playNextNote(i + 1);
690678
} else {
691679
this._locked = false;
692-
setTimeout(this._resetNotes(), 500);
680+
setTimeout(() => this._resetNotes(), 500);
693681
return;
694682
}
695683
}, 1000 * time);
@@ -744,7 +732,7 @@ class ModeWidget {
744732
this.__playNextNote(i + 1);
745733
} else {
746734
this._locked = false;
747-
setTimeout(this._resetNotes(), 500);
735+
setTimeout(() => this._resetNotes(), 500);
748736
return;
749737
}
750738
}, 1000 * time);

js/widgets/rhythmruler.js

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -475,45 +475,39 @@ class RhythmRuler {
475475
* @private
476476
* @returns {void}
477477
*/
478-
widgetWindow.addButton(
479-
"export-chunk.svg",
480-
iconSize,
481-
_("Save rhythms")
482-
).onclick = async () => {
483-
// this._save(0);
484-
// Debounce button
485-
if (!this._get_save_lock()) {
486-
this._save_lock = true;
487-
488-
// Save a merged version of the rulers.
489-
this._saveTupletsMerged(this._mergeRulers());
490-
491-
// Rather than each ruler individually.
492-
// this._saveTuplets(0);
493-
await delayExecution(1000);
494-
this._save_lock = false;
495-
}
496-
};
478+
widgetWindow.addButton("export-chunk.svg", iconSize, _("Save rhythms")).onclick =
479+
async () => {
480+
// this._save(0);
481+
// Debounce button
482+
if (!this._get_save_lock()) {
483+
this._save_lock = true;
484+
485+
// Save a merged version of the rulers.
486+
this._saveTupletsMerged(this._mergeRulers());
487+
488+
// Rather than each ruler individually.
489+
// this._saveTuplets(0);
490+
await delayExecution(1000);
491+
this._save_lock = false;
492+
}
493+
};
497494

498495
/**
499496
* Event handler for the click event of the save drum machine button.
500497
* Saves the drum machine.
501498
* @private
502499
* @returns {void}
503500
*/
504-
widgetWindow.addButton(
505-
"export-drums.svg",
506-
iconSize,
507-
_("Save drum machine")
508-
).onclick = async () => {
509-
// Debounce button
510-
if (!this._get_save_lock()) {
511-
this._save_lock = true;
512-
this._saveMachine(0);
513-
await delayExecution(1000);
514-
this._save_lock = false;
515-
}
516-
};
501+
widgetWindow.addButton("export-drums.svg", iconSize, _("Save drum machine")).onclick =
502+
async () => {
503+
// Debounce button
504+
if (!this._get_save_lock()) {
505+
this._save_lock = true;
506+
this._saveMachine(0);
507+
await delayExecution(1000);
508+
this._save_lock = false;
509+
}
510+
};
517511

518512
// An input for setting the dissect number
519513
this._dissectNumber = widgetWindow.addInputButton("2");
@@ -639,7 +633,7 @@ class RhythmRuler {
639633
this._startingTime = null;
640634
this._elapsedTimes[id] = 0;
641635
this._offsets[id] = 0;
642-
setTimeout(this._calculateZebraStripes(id), 1000);
636+
setTimeout(() => this._calculateZebraStripes(id), 1000);
643637
}
644638
} else if (this._playingOne === false) {
645639
this._rulerSelected = id;
@@ -946,9 +940,9 @@ class RhythmRuler {
946940
if (this.Drums[this._rulerSelected] === null) {
947941
drum = "snare drum";
948942
} else {
949-
const drumBlockNo = this.activity.blocks.blockList[
950-
this.Drums[this._rulerSelected]
951-
].connections[1];
943+
const drumBlockNo =
944+
this.activity.blocks.blockList[this.Drums[this._rulerSelected]]
945+
.connections[1];
952946
drum = this.activity.blocks.blockList[drumBlockNo].value;
953947
}
954948

@@ -2224,8 +2218,8 @@ class RhythmRuler {
22242218
if (this.Drums[selectedRuler] === null) {
22252219
drum = "snare drum";
22262220
} else {
2227-
const drumBlockNo = this.activity.blocks.blockList[this.Drums[selectedRuler]]
2228-
.connections[1];
2221+
const drumBlockNo =
2222+
this.activity.blocks.blockList[this.Drums[selectedRuler]].connections[1];
22292223
drum = this.activity.blocks.blockList[drumBlockNo].value;
22302224
}
22312225

0 commit comments

Comments
 (0)