Cleanup/refactoring.

This commit is contained in:
Karishma Chadha 2017-12-15 14:00:53 -05:00
parent bcbe910643
commit 720c22db0e

View file

@ -170,22 +170,22 @@ const generateVariableIdGetter = (function () {
const globalBroadcastMsgStateGenerator = (function () { const globalBroadcastMsgStateGenerator = (function () {
let broadcastMsgNameMap = {}; let broadcastMsgNameMap = {};
const allBroadcastInputsAndFields = []; const allBroadcastFields = [];
const emptyStringName = uid(); const emptyStringName = uid();
return function (topLevel) { return function (topLevel) {
if (topLevel) broadcastMsgNameMap = {}; if (topLevel) broadcastMsgNameMap = {};
return { return {
broadcastMsgMapUpdater: function (name, block) { broadcastMsgMapUpdater: function (name, field) {
name = name.toLowerCase(); name = name.toLowerCase();
if (name === '') { if (name === '') {
name = emptyStringName; name = emptyStringName;
} }
broadcastMsgNameMap[name] = `broadcastMsgId-${name}`; broadcastMsgNameMap[name] = `broadcastMsgId-${name}`;
allBroadcastInputsAndFields.push(block); allBroadcastFields.push(field);
return {name: name, id: broadcastMsgNameMap[name]}; return broadcastMsgNameMap[name];
}, },
globalBroadcastMsgs: broadcastMsgNameMap, globalBroadcastMsgs: broadcastMsgNameMap,
allBroadcastInputsAndFields: allBroadcastInputsAndFields, allBroadcastFields: allBroadcastFields,
emptyMsgName: emptyStringName emptyMsgName: emptyStringName
}; };
}; };
@ -349,23 +349,31 @@ const parseScratchObject = function (object, runtime, extensions, topLevel) {
// all other targets have finished processing. // all other targets have finished processing.
if (target.isStage) { if (target.isStage) {
const allBroadcastMsgs = globalBroadcastMsgObj.globalBroadcastMsgs; const allBroadcastMsgs = globalBroadcastMsgObj.globalBroadcastMsgs;
const allBroadcastMsgInputsAndFields = globalBroadcastMsgObj.allBroadcastInputsAndFields; const allBroadcastMsgFields = globalBroadcastMsgObj.allBroadcastFields;
const emptyName = globalBroadcastMsgObj.emptyMsgName; const oldEmptyMsgName = globalBroadcastMsgObj.emptyMsgName;
if (allBroadcastMsgs[emptyName]) { if (allBroadcastMsgs[oldEmptyMsgName]) {
// look through allBroadcastMsgs to see if 'messageN' is used // Find a fresh 'messageN'
let currIndex = 1; let currIndex = 1;
while (allBroadcastMsgs[`message${currIndex}`]) { while (allBroadcastMsgs[`message${currIndex}`]) {
currIndex += 1; currIndex += 1;
} }
const newEmptyMsgName = `message${currIndex}`; const newEmptyMsgName = `message${currIndex}`;
allBroadcastMsgs[newEmptyMsgName] = allBroadcastMsgs[emptyName]; // Add the new empty message name to the broadcast message
delete allBroadcastMsgs[emptyName]; // name map, and assign it the old id.
for (let i = 0; i < allBroadcastMsgInputsAndFields.length; i++) { // Then, delete the old entry in map.
if (allBroadcastMsgInputsAndFields[i].value === emptyName) { allBroadcastMsgs[newEmptyMsgName] = allBroadcastMsgs[oldEmptyMsgName];
allBroadcastMsgInputsAndFields[i].value = newEmptyMsgName; delete allBroadcastMsgs[oldEmptyMsgName];
// Now update all the broadcast message fields with
// the new empty message name.
for (let i = 0; i < allBroadcastMsgFields.length; i++) {
if (allBroadcastMsgFields[i].value === '') {
allBroadcastMsgFields[i].value = newEmptyMsgName;
} }
} }
} }
// Traverse the broadcast message name map and create
// broadcast messages as variables on the stage (which is this
// target).
for (const msgName in allBroadcastMsgs) { for (const msgName in allBroadcastMsgs) {
const msgId = allBroadcastMsgs[msgName]; const msgId = allBroadcastMsgs[msgName];
const newMsg = new Variable( const newMsg = new Variable(
@ -531,21 +539,25 @@ const parseBlock = function (sb2block, addBroadcastMsg, getVariableId, extension
} }
const fields = {}; const fields = {};
fields[fieldName] = { fields[fieldName] = {
name: fieldName name: fieldName,
value: fieldValue
}; };
// event_broadcast_menus have some extra properties to add to the // event_broadcast_menus have some extra properties to add to the
// field and a different value than the rest // field and a different value than the rest
if (expectedArg.inputOp === 'event_broadcast_menu') { if (expectedArg.inputOp === 'event_broadcast_menu') {
if (!shadowObscured) { if (!shadowObscured) {
const broadcastInfo = addBroadcastMsg(fieldValue, fields[fieldName]); // Need to update the broadcast message name map with
fields[fieldName].id = broadcastInfo.id; // the value of this field.
// Re-assign the value, because the name could have changed // Also need to provide the fields[fieldName] object,
// if the scratch2 message was an empty string // so that we can later update its value property, e.g.
fields[fieldName].value = broadcastInfo.name; // if sb2 message name is empty string, we will later
// replace this field's value with messageN
// once we can traverse through all the existing message names
// and come up with a fresh messageN.
const broadcastId = addBroadcastMsg(fieldValue, fields[fieldName]);
fields[fieldName].id = broadcastId;
} }
fields[fieldName].variableType = expectedArg.variableType; fields[fieldName].variableType = expectedArg.variableType;
} else {
fields[fieldName].value = fieldValue;
} }
activeBlock.children.push({ activeBlock.children.push({
id: inputUid, id: inputUid,
@ -573,13 +585,15 @@ const parseBlock = function (sb2block, addBroadcastMsg, getVariableId, extension
// Add `id` property to variable fields // Add `id` property to variable fields
activeBlock.fields[expectedArg.fieldName].id = getVariableId(providedArg); activeBlock.fields[expectedArg.fieldName].id = getVariableId(providedArg);
} else if (expectedArg.fieldName === 'BROADCAST_OPTION') { } else if (expectedArg.fieldName === 'BROADCAST_OPTION') {
// add the name in this field to the broadcast msg name map // Add the name in this field to the broadcast msg name map.
const broadcastInfo = addBroadcastMsg(providedArg, activeBlock.fields[expectedArg.fieldName]); // Also need to provide the fields[fieldName] object,
activeBlock.fields[expectedArg.fieldName].id = broadcastInfo.id; // so that we can later update its value property, e.g.
// Need to reassign field value using the sb3 name from broadcastInfo // if sb2 message name is empty string, we will later
// because the sb2 message name (e.g. providedArg) could have changed // replace this field's value with messageN
// if the original (providedArg) was an empty string // once we can traverse through all the existing message names
activeBlock.fields[expectedArg.fieldName].value = broadcastInfo.name; // and come up with a fresh messageN.
const broadcastId = addBroadcastMsg(providedArg, activeBlock.fields[expectedArg.fieldName]);
activeBlock.fields[expectedArg.fieldName].id = broadcastId;
} }
const varType = expectedArg.variableType; const varType = expectedArg.variableType;
if (typeof varType === 'string') { if (typeof varType === 'string') {