Skip to content

Commit 364af2a

Browse files
Refactor RhythmBlockPaletteBlocks to remove duplicated logic
1 parent d429ce0 commit 364af2a

File tree

2 files changed

+4666
-4679
lines changed

2 files changed

+4666
-4679
lines changed

js/blocks/RhythmBlockPaletteBlocks.js

Lines changed: 37 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2019 Bottersnike
1+
// Copyright (c) 2019 Bottersnike
22
//
33
// This program is free software; you can redistribute it and/or
44
// modify it under the terms of the The GNU Affero General Public
@@ -26,6 +26,22 @@ if (_THIS_IS_TURTLE_BLOCKS_) {
2626
}
2727

2828
function setupRhythmBlockPaletteBlocks(activity) {
29+
/**
30+
* Schedules a note to be played after a timeout.
31+
* @param {object} activity - The activity object.
32+
* @param {number} beat - The beat value.
33+
* @param {string} blk - The block ID.
34+
* @param {string} turtle - The turtle ID.
35+
* @param {function} callback - The callback function.
36+
* @param {number} timeout - The timeout in milliseconds.
37+
*/
38+
const scheduleNote = (activity, beat, blk, turtle, callback, timeout) => {
39+
setTimeout(
40+
() => Singer.processNote(activity, beat, false, blk, turtle, callback),
41+
timeout
42+
);
43+
};
44+
2945
/**
3046
* Represents a block for handling rhythms.
3147
* @extends {FlowBlock}
@@ -47,9 +63,9 @@ function setupRhythmBlockPaletteBlocks(activity) {
4763
name:
4864
this.lang === "ja"
4965
? //.TRANS: rhythm block
50-
_("rhythm1")
66+
_("rhythm1")
5167
: //.TRANS: an arrangement of notes based on duration
52-
_("rhythm"),
68+
_("rhythm"),
5369

5470
/**
5571
* @type {number}
@@ -113,7 +129,7 @@ function setupRhythmBlockPaletteBlocks(activity) {
113129
if (logo.inMatrix) {
114130
logo.phraseMaker.addColBlock(blk, arg0);
115131

116-
// Add individual entries for each beat to avoid extra × blocks
132+
// Add individual entries for each beat to avoid extra × blocks
117133
for (let i = 0; i < arg0; i++) {
118134
logo.tupletRhythms.push(["individual", 1, noteBeatValue]);
119135
}
@@ -171,21 +187,6 @@ function setupRhythmBlockPaletteBlocks(activity) {
171187

172188
const beatValue = bpmFactor == null ? 1 : bpmFactor / noteBeatValue;
173189

174-
/**
175-
* Plays a note in the rhythm.
176-
*
177-
* @param {number} thisBeat - Beat value for the note.
178-
* @param {string} blk - Block identifier.
179-
* @param {string} turtle - Turtle identifier.
180-
* @param {Function} callback - Callback function.
181-
* @param {number} timeout - Timeout value.
182-
*/
183-
const __rhythmPlayNote = (thisBeat, blk, turtle, callback, timeout) => {
184-
setTimeout(
185-
() => Singer.processNote(activity, thisBeat, false, blk, turtle, callback),
186-
timeout
187-
);
188-
};
189190
let __callback;
190191

191192
for (let i = 0; i < arg0; i++) {
@@ -198,9 +199,11 @@ function setupRhythmBlockPaletteBlocks(activity) {
198199
__callback = null;
199200
}
200201

201-
__rhythmPlayNote(noteBeatValue, blk, turtle, __callback, i * beatValue * 1000);
202+
scheduleNote(activity, noteBeatValue, blk, turtle, __callback, i * beatValue * 1000);
202203
}
203204

205+
206+
204207
tur.doWait((arg0 - 1) * beatValue);
205208
}
206209
}
@@ -234,7 +237,7 @@ function setupRhythmBlockPaletteBlocks(activity) {
234237
name:
235238
this.lang === "ja"
236239
? //.TRANS: translate "rhythm1" as rhythm
237-
_("rhythm1")
240+
_("rhythm1")
238241
: _("rhythm"),
239242

240243
/**
@@ -287,7 +290,7 @@ function setupRhythmBlockPaletteBlocks(activity) {
287290
this.appleNoteBlock();
288291
} else {
289292
// TRANS: Do not modify the following line
290-
super("sixtyfourthNote", _("1/64 note") + " 𝅘𝅥𝅱");
293+
super("sixtyfourthNote", _("1/64 note") + " 𝅘𝅥𝅱");
291294
}
292295

293296
this.setPalette(rhythmBlockPalette, activity);
@@ -318,7 +321,7 @@ function setupRhythmBlockPaletteBlocks(activity) {
318321
this.appleNoteBlock();
319322
} else {
320323
// TRANS: Do not modify the following line
321-
super("thirtysecondNote", _("1/32 note") + " 𝅘𝅥𝅰");
324+
super("thirtysecondNote", _("1/32 note") + " 𝅘𝅥𝅰");
322325
}
323326

324327
// Set the palette and activity for the block
@@ -352,7 +355,7 @@ function setupRhythmBlockPaletteBlocks(activity) {
352355
this.appleNoteBlock();
353356
} else {
354357
// TRANS: Do not modify the following line
355-
super("sixteenthNote", _("1/16 note") + " 𝅘𝅥𝅯");
358+
super("sixteenthNote", _("1/16 note") + " 𝅘𝅥𝅯");
356359
}
357360

358361
// Set the palette and activity for the block
@@ -381,7 +384,7 @@ function setupRhythmBlockPaletteBlocks(activity) {
381384
*/
382385
constructor() {
383386
// TRANS: Do not modify the following line
384-
super("eighthNote", _("eighth note") + " ");
387+
super("eighthNote", _("eighth note") + " ΓÖ¬");
385388

386389
// Set the palette and activity for the block
387390
this.setPalette(rhythmBlockPalette, activity);
@@ -409,7 +412,7 @@ function setupRhythmBlockPaletteBlocks(activity) {
409412
*/
410413
constructor() {
411414
// TRANS: Do not modify the following line
412-
super("quarterNote", _("quarter note") + " ");
415+
super("quarterNote", _("quarter note") + " ΓÖ⌐");
413416

414417
// Set the palette and activity for the block
415418
this.setPalette(rhythmBlockPalette, activity);
@@ -442,7 +445,7 @@ function setupRhythmBlockPaletteBlocks(activity) {
442445
this.appleNoteBlock();
443446
} else {
444447
// TRANS: Do not modify the following line
445-
super("halfNote", _("half note") + " 𝅗𝅥");
448+
super("halfNote", _("half note") + " 𝅗𝅥");
446449
}
447450

448451
// Set the palette and activity for the block
@@ -476,7 +479,7 @@ function setupRhythmBlockPaletteBlocks(activity) {
476479
this.appleNoteBlock();
477480
} else {
478481
// TRANS: Do not modify the following line
479-
super("wholeNote", _("whole note") + " 𝅝");
482+
super("wholeNote", _("whole note") + " 𝅝");
480483
}
481484

482485
// Set the palette and activity for the block
@@ -739,23 +742,6 @@ function setupRhythmBlockPaletteBlocks(activity) {
739742
? last(tur.singer.bpm)
740743
: Singer.masterBPM;
741744

742-
let totalBeats = 0;
743-
744-
const __tupletPlayNote = (thisBeat, blk, turtle, callback, timeout) => {
745-
setTimeout(
746-
() =>
747-
Singer.processNote(
748-
activity,
749-
thisBeat,
750-
false,
751-
blk,
752-
turtle,
753-
callback
754-
),
755-
timeout
756-
);
757-
};
758-
759745
let timeout = 0;
760746
let beatValue;
761747
let __callback = null;
@@ -775,12 +761,13 @@ function setupRhythmBlockPaletteBlocks(activity) {
775761
__callback = null;
776762
}
777763

778-
__tupletPlayNote(thisBeat, blk, turtle, __callback, timeout);
764+
scheduleNote(activity, thisBeat, blk, turtle, __callback, timeout);
779765

780766
timeout += beatValue * 1000;
781767
totalBeats += beatValue;
782768
}
783769

770+
784771
tur.doWait(totalBeats - beatValue);
785772
}
786773
};
@@ -895,10 +882,10 @@ function setupRhythmBlockPaletteBlocks(activity) {
895882
// Set the help string for the block
896883
this.setHelpString([
897884
_("Tuplets are a collection of notes that get scaled to a specific duration.") +
898-
" " +
899-
_(
900-
"Using tuplets makes it easy to create groups of notes that are not based on a power of 2."
901-
),
885+
" " +
886+
_(
887+
"Using tuplets makes it easy to create groups of notes that are not based on a power of 2."
888+
),
902889
"documentation",
903890
null,
904891
"matrix"

0 commit comments

Comments
 (0)