mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-07-12 05:54:02 -04:00
Address comments from @kchada review
This commit is contained in:
parent
aa24d118c8
commit
f01558b74e
2 changed files with 10 additions and 16 deletions
|
@ -211,7 +211,7 @@ const globalBroadcastMsgStateGenerator = (function () {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse a single monitor object and create all its in-memory VM objects.
|
* Parse a single monitor object and create all its in-memory VM objects.
|
||||||
* @param {!object} object - From-JSON "Scratch object"
|
* @param {!object} object - From-JSON "Monitor object"
|
||||||
* @param {!Runtime} runtime - (in/out) Runtime object to load monitor info into.
|
* @param {!Runtime} runtime - (in/out) Runtime object to load monitor info into.
|
||||||
* @param {!Array.<Target>} targets - Targets have already been parsed.
|
* @param {!Array.<Target>} targets - Targets have already been parsed.
|
||||||
* @param {ImportedExtensionsInfo} extensions - (in/out) parsed extension information will be stored here.
|
* @param {ImportedExtensionsInfo} extensions - (in/out) parsed extension information will be stored here.
|
||||||
|
@ -234,7 +234,7 @@ const parseMonitorObject = (object, runtime, targets, extensions) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a block for the monitor blocks container
|
// Get the target for this monitor, if not gotten above.
|
||||||
target = target || targets.filter(t => t.getName() === object.target)[0];
|
target = target || targets.filter(t => t.getName() === object.target)[0];
|
||||||
if (!target) throw new Error('Cannot create monitor for target that cannot be found by name');
|
if (!target) throw new Error('Cannot create monitor for target that cannot be found by name');
|
||||||
|
|
||||||
|
@ -248,23 +248,17 @@ const parseMonitorObject = (object, runtime, targets, extensions) => {
|
||||||
extensions
|
extensions
|
||||||
);
|
);
|
||||||
|
|
||||||
let isSpriteLocalVariable;
|
// Monitor blocks have special IDs to match the toolbox obtained from the getId
|
||||||
|
// function in the runtime.monitorBlocksInfo. Variable monitors, however,
|
||||||
|
// get their IDs from the variable id they reference.
|
||||||
if (object.cmd === 'getVar:' || object.cmd === 'contentsOfList:') {
|
if (object.cmd === 'getVar:' || object.cmd === 'contentsOfList:') {
|
||||||
// These monitors are sprite-specific if they are not targetting the stage.
|
|
||||||
isSpriteLocalVariable = !target.isStage;
|
|
||||||
// Variable getters have special block IDs for the toolbox that match the variable ID.
|
|
||||||
block.id = getVariableId(object.param);
|
block.id = getVariableId(object.param);
|
||||||
|
} else if (runtime.monitorBlockInfo.hasOwnProperty(block.opcode)) {
|
||||||
|
block.id = runtime.monitorBlockInfo[block.opcode].getId(target.id, object.param);
|
||||||
}
|
}
|
||||||
|
|
||||||
block.id = runtime.monitorBlockInfo.hasOwnProperty(block.opcode) ?
|
// Block needs a targetId if it is targetting something other than the stage
|
||||||
runtime.monitorBlockInfo[block.opcode].getId(target.id, object.param) : block.id;
|
block.targetId = target.isStage ? null : target.id;
|
||||||
|
|
||||||
// Block needs a targetId if it is sprite specific or a local variable.
|
|
||||||
// Consult the monitorBlockInfo in the runtime for sprite-specificity.
|
|
||||||
const isSpriteSpecific = isSpriteLocalVariable ||
|
|
||||||
(runtime.monitorBlockInfo.hasOwnProperty(block.opcode) &&
|
|
||||||
runtime.monitorBlockInfo[block.opcode].isSpriteSpecific);
|
|
||||||
block.targetId = isSpriteSpecific ? target.id : null;
|
|
||||||
|
|
||||||
// Property required for running monitored blocks.
|
// Property required for running monitored blocks.
|
||||||
block.isMonitored = object.visible;
|
block.isMonitored = object.visible;
|
||||||
|
|
|
@ -7,7 +7,7 @@ const VirtualMachine = require('../../src/index');
|
||||||
const projectUri = path.resolve(__dirname, '../fixtures/monitors.sb2');
|
const projectUri = path.resolve(__dirname, '../fixtures/monitors.sb2');
|
||||||
const project = readFileToBuffer(projectUri);
|
const project = readFileToBuffer(projectUri);
|
||||||
|
|
||||||
test('complex', t => {
|
test('importing sb2 project with monitors', t => {
|
||||||
const vm = new VirtualMachine();
|
const vm = new VirtualMachine();
|
||||||
vm.attachStorage(makeTestStorage());
|
vm.attachStorage(makeTestStorage());
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue