Skip to content

Commit 172113e

Browse files
Pankajyadav919Walter Bender
andauthored
Refactor RhythmBlockPaletteBlocks: Extract reusable playNoteSequence helper to remove duplicated scheduling logic (#5765)
* Refactor RhythmBlockPaletteBlocks to remove duplicated logic * fix unicode errors --------- Co-authored-by: Walter Bender <walter@sorceo.com>
1 parent 376ccc8 commit 172113e

1 file changed

Lines changed: 23 additions & 35 deletions

File tree

js/blocks/RhythmBlockPaletteBlocks.js

Lines changed: 23 additions & 35 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,19 @@ 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(() => Singer.processNote(activity, beat, false, blk, turtle, callback), timeout);
40+
};
41+
2942
/**
3043
* Represents a block for handling rhythms.
3144
* @extends {FlowBlock}
@@ -171,21 +184,6 @@ function setupRhythmBlockPaletteBlocks(activity) {
171184

172185
const beatValue = bpmFactor == null ? 1 : bpmFactor / noteBeatValue;
173186

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-
};
189187
let __callback;
190188

191189
for (let i = 0; i < arg0; i++) {
@@ -198,7 +196,14 @@ function setupRhythmBlockPaletteBlocks(activity) {
198196
__callback = null;
199197
}
200198

201-
__rhythmPlayNote(noteBeatValue, blk, turtle, __callback, i * beatValue * 1000);
199+
scheduleNote(
200+
activity,
201+
noteBeatValue,
202+
blk,
203+
turtle,
204+
__callback,
205+
i * beatValue * 1000
206+
);
202207
}
203208

204209
tur.doWait((arg0 - 1) * beatValue);
@@ -737,23 +742,6 @@ function setupRhythmBlockPaletteBlocks(activity) {
737742
? last(tur.singer.bpm)
738743
: Singer.masterBPM;
739744

740-
let totalBeats = 0;
741-
742-
const __tupletPlayNote = (thisBeat, blk, turtle, callback, timeout) => {
743-
setTimeout(
744-
() =>
745-
Singer.processNote(
746-
activity,
747-
thisBeat,
748-
false,
749-
blk,
750-
turtle,
751-
callback
752-
),
753-
timeout
754-
);
755-
};
756-
757745
let timeout = 0;
758746
let beatValue;
759747
let __callback = null;
@@ -773,7 +761,7 @@ function setupRhythmBlockPaletteBlocks(activity) {
773761
__callback = null;
774762
}
775763

776-
__tupletPlayNote(thisBeat, blk, turtle, __callback, timeout);
764+
scheduleNote(activity, thisBeat, blk, turtle, __callback, timeout);
777765

778766
timeout += beatValue * 1000;
779767
totalBeats += beatValue;

0 commit comments

Comments
 (0)