mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-01-09 14:32:07 -05:00
Consolidate collectAssets into getter
This commit is contained in:
parent
df56e615ec
commit
bcd1c5b13a
3 changed files with 7 additions and 35 deletions
|
@ -50,21 +50,7 @@ const serializeCostumes = function (runtime, optTargetId) {
|
||||||
return serializeAssets(runtime, 'costumes', optTargetId);
|
return serializeAssets(runtime, 'costumes', optTargetId);
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
|
||||||
* Return all costumes and sounds in the provided runtime
|
|
||||||
* @param {Runtime} runtime
|
|
||||||
* @returns {Array<object>} An array of costumes and sounds
|
|
||||||
*/
|
|
||||||
const collectAssets = function (runtime) {
|
|
||||||
return runtime.targets.reduce((acc, target) => (
|
|
||||||
acc
|
|
||||||
.concat(target.sprite.sounds.map(sound => sound.asset))
|
|
||||||
.concat(target.sprite.costumes.map(costume => costume.asset))
|
|
||||||
), []);
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
collectAssets,
|
|
||||||
serializeSounds,
|
serializeSounds,
|
||||||
serializeCostumes
|
serializeCostumes
|
||||||
};
|
};
|
||||||
|
|
|
@ -18,7 +18,7 @@ const Variable = require('./engine/variable');
|
||||||
|
|
||||||
const {loadCostume} = require('./import/load-costume.js');
|
const {loadCostume} = require('./import/load-costume.js');
|
||||||
const {loadSound} = require('./import/load-sound.js');
|
const {loadSound} = require('./import/load-sound.js');
|
||||||
const {collectAssets, serializeSounds, serializeCostumes} = require('./serialization/serialize-assets');
|
const {serializeSounds, serializeCostumes} = require('./serialization/serialize-assets');
|
||||||
require('canvas-toBlob');
|
require('canvas-toBlob');
|
||||||
|
|
||||||
const RESERVED_NAMES = ['_mouse_', '_stage_', '_edge_', '_myself_', '_random_'];
|
const RESERVED_NAMES = ['_mouse_', '_stage_', '_edge_', '_myself_', '_random_'];
|
||||||
|
@ -340,7 +340,11 @@ class VirtualMachine extends EventEmitter {
|
||||||
* @type {Array<object>} Array of all costumes and sounds currently in the runtime
|
* @type {Array<object>} Array of all costumes and sounds currently in the runtime
|
||||||
*/
|
*/
|
||||||
get assets () {
|
get assets () {
|
||||||
return collectAssets(this.runtime);
|
return this.runtime.targets.reduce((acc, target) => (
|
||||||
|
acc
|
||||||
|
.concat(target.sprite.sounds.map(sound => sound.asset))
|
||||||
|
.concat(target.sprite.costumes.map(costume => costume.asset))
|
||||||
|
), []);
|
||||||
}
|
}
|
||||||
|
|
||||||
_addFileDescsToZip (fileDescs, zip) {
|
_addFileDescsToZip (fileDescs, zip) {
|
||||||
|
|
|
@ -1,16 +1,9 @@
|
||||||
const test = require('tap').test;
|
const test = require('tap').test;
|
||||||
|
|
||||||
const {collectAssets} = require('../../src/serialization/serialize-assets');
|
|
||||||
|
|
||||||
const RenderedTarget = require('../../src/sprites/rendered-target');
|
const RenderedTarget = require('../../src/sprites/rendered-target');
|
||||||
const Sprite = require('../../src/sprites/sprite');
|
const Sprite = require('../../src/sprites/sprite');
|
||||||
const VirtualMachine = require('../../src/virtual-machine');
|
const VirtualMachine = require('../../src/virtual-machine');
|
||||||
|
|
||||||
test('spec', t => {
|
|
||||||
t.type(collectAssets, 'function');
|
|
||||||
t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('collectAssets', t => {
|
test('collectAssets', t => {
|
||||||
const vm = new VirtualMachine();
|
const vm = new VirtualMachine();
|
||||||
const sprite = new Sprite(null, vm.runtime);
|
const sprite = new Sprite(null, vm.runtime);
|
||||||
|
@ -23,20 +16,9 @@ test('collectAssets', t => {
|
||||||
] = [{assetId: 1}, {assetId: 2}, {assetId: 3}];
|
] = [{assetId: 1}, {assetId: 2}, {assetId: 3}];
|
||||||
sprite.sounds = [{id: 1, asset: soundAsset1}, {id: 2, asset: soundAsset2}];
|
sprite.sounds = [{id: 1, asset: soundAsset1}, {id: 2, asset: soundAsset2}];
|
||||||
sprite.costumes = [{id: 1, asset: costumeAsset1}];
|
sprite.costumes = [{id: 1, asset: costumeAsset1}];
|
||||||
const assets = collectAssets(vm.runtime);
|
|
||||||
t.deepEqual(assets, [soundAsset1, soundAsset2, costumeAsset1]);
|
|
||||||
t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('getter', t => {
|
|
||||||
const vm = new VirtualMachine();
|
|
||||||
const sprite = new Sprite(null, vm.runtime);
|
|
||||||
const target = new RenderedTarget(sprite, vm.runtime);
|
|
||||||
vm.runtime.targets = [target];
|
|
||||||
sprite.sounds = [{id: 1, asset: {}}, {id: 2, asset: {}}];
|
|
||||||
sprite.costumes = [{id: 1, asset: {}}];
|
|
||||||
const assets = vm.assets;
|
const assets = vm.assets;
|
||||||
t.type(assets.length, 'number');
|
t.type(assets.length, 'number');
|
||||||
t.equal(assets.length, 3);
|
t.equal(assets.length, 3);
|
||||||
|
t.deepEqual(assets, [soundAsset1, soundAsset2, costumeAsset1]);
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue