mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 23:12:24 -05:00
Streamlining broadcast block execution. When arg information is provided to execute function, corresponding broadcast message is looked up by id, if provided, or if not, then name. If neither id nor name is provided, we can't look up the broadcast message and something is wrong, so an error is logged. We no longer need to differentiate between the shadow menu vs. plugged in input cases via the argValue name (e.g. 'BROADCAST_OPTION' vs. 'BROADCAST_INPUT'). This refactor also helps with sb2 import, coming in the next commit.
This commit is contained in:
parent
413d113dda
commit
c0e4ae455c
3 changed files with 15 additions and 28 deletions
|
@ -55,27 +55,9 @@ class Scratch3EventBlocks {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper function to process broadcast block input (whether it's
|
|
||||||
* input from the dropdown menu or from a plugged in input block)
|
|
||||||
* @param {object} args The given arguments for the broadcast blocks
|
|
||||||
* @param {object} util The utility associated with this block.
|
|
||||||
* @return {?Variable} The broadcast message variable that matches
|
|
||||||
* the provided input.
|
|
||||||
*/
|
|
||||||
processBroadcastInput_ (args, util) {
|
|
||||||
let broadcastInput;
|
|
||||||
if (args.BROADCAST_OPTION) {
|
|
||||||
broadcastInput = util.runtime.getTargetForStage().lookupBroadcastMsg(
|
|
||||||
args.BROADCAST_OPTION.id, args.BROADCAST_OPTION.name);
|
|
||||||
} else {
|
|
||||||
broadcastInput = util.runtime.getTargetForStage().lookupBroadcastByInputValue(args.BROADCAST_INPUT.name);
|
|
||||||
}
|
|
||||||
return broadcastInput;
|
|
||||||
}
|
|
||||||
|
|
||||||
broadcast (args, util) {
|
broadcast (args, util) {
|
||||||
const broadcastVar = this.processBroadcastInput_(args, util);
|
const broadcastVar = util.runtime.getTargetForStage().lookupBroadcastMsg(
|
||||||
|
args.BROADCAST_OPTION.id, args.BROADCAST_OPTION.name);
|
||||||
if (broadcastVar) {
|
if (broadcastVar) {
|
||||||
const broadcastOption = broadcastVar.name;
|
const broadcastOption = broadcastVar.name;
|
||||||
util.startHats('event_whenbroadcastreceived', {
|
util.startHats('event_whenbroadcastreceived', {
|
||||||
|
@ -85,7 +67,8 @@ class Scratch3EventBlocks {
|
||||||
}
|
}
|
||||||
|
|
||||||
broadcastAndWait (args, util) {
|
broadcastAndWait (args, util) {
|
||||||
const broadcastVar = this.processBroadcastInput_(args, util);
|
const broadcastVar = util.runtime.getTargetForStage().lookupBroadcastMsg(
|
||||||
|
args.BROADCAST_OPTION.id, args.BROADCAST_OPTION.name);
|
||||||
if (broadcastVar) {
|
if (broadcastVar) {
|
||||||
const broadcastOption = broadcastVar.name;
|
const broadcastOption = broadcastVar.name;
|
||||||
// Have we run before, starting threads?
|
// Have we run before, starting threads?
|
||||||
|
|
|
@ -217,9 +217,6 @@ const execute = function (sequencer, thread) {
|
||||||
const broadcastInput = inputs[inputName];
|
const broadcastInput = inputs[inputName];
|
||||||
// Check if something is plugged into the broadcast block, or
|
// Check if something is plugged into the broadcast block, or
|
||||||
// if the shadow dropdown menu is being used.
|
// if the shadow dropdown menu is being used.
|
||||||
// Differentiate between these two cases by giving argValues
|
|
||||||
// a 'BROADCAST_INPUT' field or a 'BROADCAST_OPTION' field
|
|
||||||
// respectively.
|
|
||||||
if (broadcastInput.block === broadcastInput.shadow) {
|
if (broadcastInput.block === broadcastInput.shadow) {
|
||||||
// Shadow dropdown menu is being used.
|
// Shadow dropdown menu is being used.
|
||||||
// Get the appropriate information out of it.
|
// Get the appropriate information out of it.
|
||||||
|
@ -232,7 +229,7 @@ const execute = function (sequencer, thread) {
|
||||||
} else {
|
} else {
|
||||||
// Something is plugged into the broadcast input.
|
// Something is plugged into the broadcast input.
|
||||||
// Cast it to a string. We don't need an id here.
|
// Cast it to a string. We don't need an id here.
|
||||||
argValues.BROADCAST_INPUT = {
|
argValues.BROADCAST_OPTION = {
|
||||||
name: cast.toString(inputValue)
|
name: cast.toString(inputValue)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -98,12 +98,19 @@ class Target extends EventEmitter {
|
||||||
* if it exists.
|
* if it exists.
|
||||||
* @param {string} id Id of the variable.
|
* @param {string} id Id of the variable.
|
||||||
* @param {string} name Name of the variable.
|
* @param {string} name Name of the variable.
|
||||||
* @return {!Variable} Variable object.
|
* @return {?Variable} Variable object.
|
||||||
*/
|
*/
|
||||||
lookupBroadcastMsg (id, name) {
|
lookupBroadcastMsg (id, name) {
|
||||||
const broadcastMsg = this.lookupVariableById(id);
|
let broadcastMsg;
|
||||||
|
if (id) {
|
||||||
|
broadcastMsg = this.lookupVariableById(id);
|
||||||
|
} else if (name) {
|
||||||
|
broadcastMsg = this.lookupBroadcastByInputValue(name);
|
||||||
|
} else {
|
||||||
|
log.error('Cannot find broadcast message if neither id nor name are provided.');
|
||||||
|
}
|
||||||
if (broadcastMsg) {
|
if (broadcastMsg) {
|
||||||
if (broadcastMsg.name !== name) {
|
if (name && (broadcastMsg.name.toLowerCase() !== name.toLowerCase())) {
|
||||||
log.error(`Found broadcast message with id: ${id}, but` +
|
log.error(`Found broadcast message with id: ${id}, but` +
|
||||||
`its name, ${broadcastMsg.name} did not match expected name ${name}.`);
|
`its name, ${broadcastMsg.name} did not match expected name ${name}.`);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue