Implicit deletion behavior for broadcast blocks.

This commit is contained in:
Karishma Chadha 2017-12-21 18:34:19 -05:00
parent 720c22db0e
commit 6fc554daa5

View file

@ -8,6 +8,7 @@ const sb2 = require('./serialization/sb2');
const sb3 = require('./serialization/sb3');
const StringUtil = require('./util/string-util');
const formatMessage = require('format-message');
const Variable = require('./engine/variable');
const {loadCostume} = require('./import/load-costume.js');
const {loadSound} = require('./import/load-sound.js');
@ -677,6 +678,35 @@ class VirtualMachine extends EventEmitter {
* of the current editing target's blocks.
*/
emitWorkspaceUpdate () {
// Create a list of broadcast message Ids according to the stage variables
const stageVariables = this.runtime.getTargetForStage().variables;
let messageIds = [];
for (const varId in stageVariables) {
if (stageVariables[varId].type === Variable.BROADCAST_MESSAGE_TYPE) {
messageIds.push(varId);
}
}
// Go through all blocks on all targets, removing referenced
// broadcast ids from the list.
for (let i = 0; i < this.runtime.targets.length; i++) {
const currTarget = this.runtime.targets[i];
const currBlocks = currTarget.blocks._blocks;
for (const blockId in currBlocks) {
if (currBlocks[blockId].fields.BROADCAST_OPTION) {
const id = currBlocks[blockId].fields.BROADCAST_OPTION.id;
const index = messageIds.indexOf(id);
if (index !== -1) {
messageIds = messageIds.slice(0, index)
.concat(messageIds.slice(index + 1));
}
}
}
}
// Anything left in messageIds is not referenced by a block, so delete it.
for (let i = 0; i < messageIds.length; i++) {
const id = messageIds[i];
delete this.runtime.getTargetForStage().variables[id];
}
const variableMap = Object.assign({},
this.runtime.getTargetForStage().variables,
this.editingTarget.variables