Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions blocks/procedures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,9 @@ const PROCEDURE_DEF_COMMON = {
while (paramBlock && !paramBlock.isInsertionMarker()) {
const varName = paramBlock.getFieldValue('NAME');
this.arguments_.push(varName);
const variable = this.workspace.getVariable(varName, '')!;
const variable = this.workspace
.getVariableMap()
.getVariable(varName, '')!;
this.argumentVarModels_.push(variable);

this.paramIds_.push(paramBlock.id);
Expand Down Expand Up @@ -374,13 +376,13 @@ const PROCEDURE_DEF_COMMON = {
oldId: string,
newId: string,
) {
const oldVariable = this.workspace.getVariableById(oldId)!;
const oldVariable = this.workspace.getVariableMap().getVariableById(oldId)!;
if (oldVariable.getType() !== '') {
// Procedure arguments always have the empty type.
return;
}
const oldName = oldVariable.getName();
const newVar = this.workspace.getVariableById(newId)!;
const newVar = this.workspace.getVariableMap().getVariableById(newId)!;

let change = false;
for (let i = 0; i < this.argumentVarModels_.length; i++) {
Expand Down
6 changes: 3 additions & 3 deletions core/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1161,9 +1161,9 @@ export class Block {
const vars = [];
for (const field of this.getFields()) {
if (field.referencesVariables()) {
const model = this.workspace.getVariableById(
field.getValue() as string,
);
const model = this.workspace
.getVariableMap()
.getVariableById(field.getValue() as string);
// Check if the variable actually exists (and isn't just a potential
// variable).
if (model) {
Expand Down
4 changes: 3 additions & 1 deletion core/flyout_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,9 @@ export abstract class Flyout
createBlock(originalBlock: BlockSvg): BlockSvg {
let newBlock = null;
eventUtils.disable();
const variablesBeforeCreation = this.targetWorkspace.getAllVariables();
const variablesBeforeCreation = this.targetWorkspace
.getVariableMap()
.getAllVariables();
this.targetWorkspace.setResizesEnabled(false);
try {
newBlock = this.placeNewBlock(originalBlock);
Expand Down
2 changes: 1 addition & 1 deletion core/serialization/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ export function appendInternal(
}
eventUtils.disable();

const variablesBeforeCreation = workspace.getAllVariables();
const variablesBeforeCreation = workspace.getVariableMap().getAllVariables();
let block;
try {
block = appendPrivate(state, workspace, {parentConnection, isShadow});
Expand Down
5 changes: 4 additions & 1 deletion core/serialization/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ export class VariableSerializer implements ISerializer {
* variables.
*/
save(workspace: Workspace): IVariableState[] | null {
const variableStates = workspace.getAllVariables().map((v) => v.save());
const variableStates = workspace
.getVariableMap()
.getAllVariables()
.map((v) => v.save());
return variableStates.length ? variableStates : null;
}

Expand Down
6 changes: 3 additions & 3 deletions core/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ export function getVariable(
// Try to just get the variable, by ID if possible.
if (id) {
// Look in the real variable map before checking the potential variable map.
variable = workspace.getVariableById(id);
variable = workspace.getVariableMap().getVariableById(id);
if (!variable && potentialVariableMap) {
variable = potentialVariableMap.getVariableById(id);
}
Expand All @@ -742,7 +742,7 @@ export function getVariable(
throw Error('Tried to look up a variable by name without a type');
}
// Otherwise look up by name and type.
variable = workspace.getVariable(opt_name, opt_type);
variable = workspace.getVariableMap().getVariable(opt_name, opt_type);
if (!variable && potentialVariableMap) {
variable = potentialVariableMap.getVariable(opt_name, opt_type);
}
Expand Down Expand Up @@ -809,7 +809,7 @@ export function getAddedVariables(
workspace: Workspace,
originalVariables: IVariableModel<IVariableState>[],
): IVariableModel<IVariableState>[] {
const allCurrentVariables = workspace.getAllVariables();
const allCurrentVariables = workspace.getVariableMap().getAllVariables();
const addedVariables = [];
if (originalVariables.length !== allCurrentVariables.length) {
for (let i = 0; i < allCurrentVariables.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion core/xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ export function domToBlockInternal(
): Block {
// Create top-level block.
eventUtils.disable();
const variablesBeforeCreation = workspace.getAllVariables();
const variablesBeforeCreation = workspace.getVariableMap().getAllVariables();
let topBlock;
try {
topBlock = domToBlockHeadless(xmlBlock, workspace);
Expand Down
2 changes: 1 addition & 1 deletion tests/mocha/block_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2450,7 +2450,7 @@ suite('Blocks', function () {
const blockA = createRenderedBlock(this.workspace, 'variable_block');

blockA.setCollapsed(true);
const variable = this.workspace.getVariable('x', '');
const variable = this.workspace.getVariableMap().getVariable('x', '');
this.variableMap.renameVariable(variable, 'y');

this.clock.runAll();
Expand Down
38 changes: 25 additions & 13 deletions tests/mocha/blocks/procedures_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ suite('Procedures', function () {
setup(function () {
sharedTestSetup.call(this, {fireEventsNow: false});
this.workspace = Blockly.inject('blocklyDiv', {});
this.workspace.createVariable('preCreatedVar', '', 'preCreatedVarId');
this.workspace.createVariable(
'preCreatedTypedVar',
'type',
'preCreatedTypedVarId',
);
this.workspace
.getVariableMap()
.createVariable('preCreatedVar', '', 'preCreatedVarId');
this.workspace
.getVariableMap()
.createVariable('preCreatedTypedVar', 'type', 'preCreatedTypedVarId');
defineRowBlock();
this.variableMap = this.workspace.getVariableMap();
});
Expand Down Expand Up @@ -433,7 +433,7 @@ suite('Procedures', function () {
this.clock.runAll();

assert.isNotNull(
this.workspace.getVariable('param1', ''),
this.workspace.getVariableMap().getVariable('param1', ''),
'Expected the old variable to continue to exist',
);
});
Expand All @@ -453,7 +453,9 @@ suite('Procedures', function () {
this.clock.runAll();
mutatorIcon.setBubbleVisible(false);

const variable = this.workspace.getVariable('param1', '');
const variable = this.workspace
.getVariableMap()
.getVariable('param1', '');
this.variableMap.renameVariable(variable, 'new name');

assert.isNotNull(
Expand All @@ -480,7 +482,9 @@ suite('Procedures', function () {
.connection.connect(paramBlock.previousConnection);
this.clock.runAll();

const variable = this.workspace.getVariable('param1', '');
const variable = this.workspace
.getVariableMap()
.getVariable('param1', '');
this.variableMap.renameVariable(variable, 'new name');

assert.equal(
Expand All @@ -506,7 +510,9 @@ suite('Procedures', function () {
this.clock.runAll();
mutatorIcon.setBubbleVisible(false);

const variable = this.workspace.getVariable('param1', '');
const variable = this.workspace
.getVariableMap()
.getVariable('param1', '');
this.variableMap.renameVariable(variable, 'new name');

assert.isNotNull(
Expand Down Expand Up @@ -535,7 +541,9 @@ suite('Procedures', function () {
this.clock.runAll();
mutatorIcon.setBubbleVisible(false);

const variable = this.workspace.getVariable('param1', '');
const variable = this.workspace
.getVariableMap()
.getVariable('param1', '');
this.variableMap.renameVariable(variable, 'preCreatedVar');

assert.isNotNull(
Expand All @@ -562,7 +570,9 @@ suite('Procedures', function () {
.connection.connect(paramBlock.previousConnection);
this.clock.runAll();

const variable = this.workspace.getVariable('param1', '');
const variable = this.workspace
.getVariableMap()
.getVariable('param1', '');
this.variableMap.renameVariable(variable, 'preCreatedVar');

assert.equal(
Expand All @@ -588,7 +598,9 @@ suite('Procedures', function () {
this.clock.runAll();
mutatorIcon.setBubbleVisible(false);

const variable = this.workspace.getVariable('param1', '');
const variable = this.workspace
.getVariableMap()
.getVariable('param1', '');
this.variableMap.renameVariable(variable, 'preCreatedVar');

assert.isNotNull(
Expand Down
12 changes: 6 additions & 6 deletions tests/mocha/event_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -833,11 +833,9 @@ suite('Events', function () {
title: 'Variable events',
testCases: variableEventTestCases,
setup: (thisObj) => {
thisObj.variable = thisObj.workspace.createVariable(
'name1',
'type1',
'id1',
);
thisObj.variable = thisObj.workspace
.getVariableMap()
.createVariable('name1', 'type1', 'id1');
},
},
{
Expand Down Expand Up @@ -1550,7 +1548,9 @@ suite('Events', function () {
);

// Expect the workspace to have a variable with ID 'test_var_id'.
assert.isNotNull(this.workspace.getVariableById(TEST_VAR_ID));
assert.isNotNull(
this.workspace.getVariableMap().getVariableById(TEST_VAR_ID),
);
});
});
suite('Disable orphans', function () {
Expand Down
32 changes: 17 additions & 15 deletions tests/mocha/field_variable_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ suite('Variable Fields', function () {

suite('setValue', function () {
setup(function () {
this.workspace.createVariable('name2', null, 'id2');
this.workspace.getVariableMap().createVariable('name2', null, 'id2');
this.field = new Blockly.FieldVariable(null);
initVariableField(this.workspace, this.field);

Expand Down Expand Up @@ -209,8 +209,8 @@ suite('Variable Fields', function () {
assert.include(dropdownOptions[dropdownOptions.length - 1][0], 'Delete');
};
test('Contains variables created before field', function () {
this.workspace.createVariable('name1', '', 'id1');
this.workspace.createVariable('name2', '', 'id2');
this.workspace.getVariableMap().createVariable('name1', '', 'id1');
this.workspace.getVariableMap().createVariable('name2', '', 'id2');
// Expect that the dropdown options will contain the variables that exist
const fieldVariable = initVariableField(
this.workspace,
Expand All @@ -228,22 +228,22 @@ suite('Variable Fields', function () {
new Blockly.FieldVariable('name1'),
);
// Expect that variables created after field creation will show up too.
this.workspace.createVariable('name2', '', 'id2');
this.workspace.getVariableMap().createVariable('name2', '', 'id2');
assertDropdownContents(fieldVariable, [
['name1', 'id1'],
['name2', 'id2'],
]);
});
test('Contains variables created before and after field', function () {
this.workspace.createVariable('name1', '', 'id1');
this.workspace.createVariable('name2', '', 'id2');
this.workspace.getVariableMap().createVariable('name1', '', 'id1');
this.workspace.getVariableMap().createVariable('name2', '', 'id2');
// Expect that the dropdown options will contain the variables that exist
const fieldVariable = initVariableField(
this.workspace,
new Blockly.FieldVariable('name1'),
);
// Expect that variables created after field creation will show up too.
this.workspace.createVariable('name3', '', 'id3');
this.workspace.getVariableMap().createVariable('name3', '', 'id3');
assertDropdownContents(fieldVariable, [
['name1', 'id1'],
['name2', 'id2'],
Expand All @@ -254,9 +254,9 @@ suite('Variable Fields', function () {

suite('Validators', function () {
setup(function () {
this.workspace.createVariable('name1', null, 'id1');
this.workspace.createVariable('name2', null, 'id2');
this.workspace.createVariable('name3', null, 'id3');
this.workspace.getVariableMap().createVariable('name1', null, 'id1');
this.workspace.getVariableMap().createVariable('name2', null, 'id2');
this.workspace.getVariableMap().createVariable('name3', null, 'id3');
this.variableField = initVariableField(
this.workspace,
new Blockly.FieldVariable('name1'),
Expand All @@ -281,7 +281,9 @@ suite('Variable Fields', function () {
});
test('New Value', function () {
// Must create the var so that the field doesn't throw an error.
this.workspace.createVariable('thing2', null, 'other2');
this.workspace
.getVariableMap()
.createVariable('thing2', null, 'other2');
this.variableField.setValue('other2');
assertFieldValue(this.variableField, 'id2', 'name2');
});
Expand Down Expand Up @@ -368,8 +370,8 @@ suite('Variable Fields', function () {
});
suite('Get variable types', function () {
setup(function () {
this.workspace.createVariable('name1', 'type1');
this.workspace.createVariable('name2', 'type2');
this.workspace.getVariableMap().createVariable('name1', 'type1');
this.workspace.getVariableMap().createVariable('name2', 'type2');
});
test('variableTypes is explicit', function () {
// Expect that since variableTypes is defined, it will be the return
Expand Down Expand Up @@ -467,7 +469,7 @@ suite('Variable Fields', function () {
});
suite('Renaming Variables', function () {
setup(function () {
this.workspace.createVariable('name1', null, 'id1');
this.workspace.getVariableMap().createVariable('name1', null, 'id1');
Blockly.defineBlocksWithJsonArray([
{
'type': 'field_variable_test_block',
Expand Down Expand Up @@ -584,7 +586,7 @@ suite('Variable Fields', function () {
});

test('ID', function () {
this.workspace.createVariable('test', '', 'id1');
this.workspace.getVariableMap().createVariable('test', '', 'id1');
const block = Blockly.serialization.blocks.append(
{
'type': 'variables_get',
Expand Down
4 changes: 3 additions & 1 deletion tests/mocha/test_helpers/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import {assert} from '../../../node_modules/chai/index.js';
function assertBlockVarModels(block, varIds) {
const expectedVarModels = [];
for (let i = 0; i < varIds.length; i++) {
expectedVarModels.push(block.workspace.getVariableById(varIds[i]));
expectedVarModels.push(
block.workspace.getVariableMap().getVariableById(varIds[i]),
);
}
assert.sameDeepOrderedMembers(block.getVarModels(), expectedVarModels);
}
Expand Down
6 changes: 5 additions & 1 deletion tests/mocha/test_helpers/variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import {assert} from '../../../node_modules/chai/index.js';
* @param {!string} id The expected id of the variable.
*/
export function assertVariableValues(container, name, type, id) {
const variable = container.getVariableById(id);
const variableMap =
container instanceof Blockly.Workspace
? container.getVariableMap()
: container;
const variable = variableMap.getVariableById(id);
assert.isDefined(variable);
assert.equal(variable.name, name);
assert.equal(variable.type, type);
Expand Down
Loading
Loading