Skip to content

Commit 21e7353

Browse files
authored
Fix stale strong beats persisting in beatList across meter changes (sugarlabs#5730)
* Fix stale strong beats persisting in beatList across meter changes setMeter() pushed default strong beats without clearing previous entries, causing incorrect beat events after a mid-piece meter change. Signed-off-by: Ady0333 <adityashinde1525@gmail.com> * Refactor: extract _clearDefaultStrongBeats helper using Array.filter() Replace duplicated splice-in-loop cleanup logic in setMeter() and onStrongBeatDo() with a shared _clearDefaultStrongBeats() helper that uses Array.filter() to rebuild beatList without mutation during iteration. Behavior is preserved exactly: only numeric default strong beats are removed; user-defined "everybeat" and "offbeat" entries are kept. * chore(lint): fix ESLint violations --------- Signed-off-by: Ady0333 <adityashinde1525@gmail.com>
1 parent 0019021 commit 21e7353

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

js/turtleactions/MeterActions.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@
4343
* @returns {void}
4444
*/
4545
function setupMeterActions(activity) {
46+
/**
47+
* Removes default numeric strong beats from the singer's beatList,
48+
* preserving user-defined "everybeat" and "offbeat" entries.
49+
* Resets the defaultStrongBeats flag to false.
50+
*
51+
* @param {Object} singer - The turtle's singer object
52+
*/
53+
function _clearDefaultStrongBeats(singer) {
54+
if (singer.defaultStrongBeats) {
55+
singer.beatList = singer.beatList.filter(
56+
beat => beat === "everybeat" || beat === "offbeat"
57+
);
58+
singer.defaultStrongBeats = false;
59+
}
60+
}
61+
4662
Singer.MeterActions = class {
4763
/**
4864
* @param {Number} beatCount
@@ -56,6 +72,9 @@ function setupMeterActions(activity) {
5672
tur.singer.beatsPerMeasure = beatCount <= 0 ? 4 : beatCount;
5773
tur.singer.noteValuePerBeat = noteValue <= 0 ? 4 : 1 / noteValue;
5874

75+
// Clear previous default strong beats before setting new ones
76+
_clearDefaultStrongBeats(tur.singer);
77+
5978
// setup default strong / weak beats until any strong beat block is used
6079
if (tur.singer.noteValuePerBeat == 4 && tur.singer.beatsPerMeasure == 4) {
6180
tur.singer.beatList.push(1);
@@ -255,19 +274,8 @@ function setupMeterActions(activity) {
255274
const eventName = "__beat_" + beat + "_" + turtleID + "__";
256275
activity.logo.setTurtleListener(turtle, eventName, __listener);
257276

258-
//remove any default strong beats other than "everybeat " or "offbeat"
259-
if (tur.singer.defaultStrongBeats) {
260-
for (let i = 0; i < tur.singer.beatList.length; i++) {
261-
if (
262-
tur.singer.beatList[i] !== "everybeat" &&
263-
tur.singer.beatList[i] !== "offbeat"
264-
) {
265-
tur.singer.beatList.splice(i, 1);
266-
i--;
267-
}
268-
}
269-
tur.singer.defaultStrongBeats = false;
270-
}
277+
//remove any default strong beats other than "everybeat" or "offbeat"
278+
_clearDefaultStrongBeats(tur.singer);
271279

272280
if (beat > tur.singer.beatsPerMeasure) {
273281
tur.singer.factorList.push(beat);

0 commit comments

Comments
 (0)