Merge pull request from ericrosenbaum/bugfix/clones-inherit-instrument

Clones inherit music custom state
This commit is contained in:
Eric Rosenbaum 2018-04-06 13:38:03 -04:00 committed by GitHub
commit 25f1c1a5e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions
src/extensions/scratch3_music
test/unit

View file

@ -74,6 +74,9 @@ class Scratch3MusicBlocks {
this._bufferSources = [];
this._loadAllSounds();
this._onTargetCreated = this._onTargetCreated.bind(this);
this.runtime.on('targetWasCreated', this._onTargetCreated);
}
/**
@ -441,6 +444,22 @@ class Scratch3MusicBlocks {
return musicState;
}
/**
* When a music-playing Target is cloned, clone the music state.
* @param {Target} newTarget - the newly created target.
* @param {Target} [sourceTarget] - the target used as a source for the new clone, if any.
* @listens Runtime#event:targetWasCreated
* @private
*/
_onTargetCreated (newTarget, sourceTarget) {
if (sourceTarget) {
const musicState = sourceTarget.getCustomState(Scratch3MusicBlocks.STATE_KEY);
if (musicState) {
newTarget.setCustomState(Scratch3MusicBlocks.STATE_KEY, Clone.simple(musicState));
}
}
}
/**
* @returns {object} metadata for this extension and its blocks.
*/

View file

@ -2,7 +2,8 @@ const test = require('tap').test;
const Music = require('../../src/extensions/scratch3_music/index.js');
const fakeRuntime = {
getTargetForStage: () => ({tempo: 60})
getTargetForStage: () => ({tempo: 60}),
on: () => {} // Stub out listener methods used in constructor.
};
const blocks = new Music(fakeRuntime);