fix: Fire a VarTypeChange event when changing a variable's type. (#9236)

This commit is contained in:
Aaron Dodson
2025-07-14 09:53:17 -07:00
committed by GitHub
parent 52634e4dec
commit 9f66f0c596
2 changed files with 31 additions and 0 deletions

View File

@@ -109,6 +109,9 @@ export class VariableMap
variable: IVariableModel<IVariableState>, variable: IVariableModel<IVariableState>,
newType: string, newType: string,
): IVariableModel<IVariableState> { ): IVariableModel<IVariableState> {
const oldType = variable.getType();
if (oldType === newType) return variable;
this.variableMap.get(variable.getType())?.delete(variable.getId()); this.variableMap.get(variable.getType())?.delete(variable.getId());
variable.setType(newType); variable.setType(newType);
const newTypeVariables = const newTypeVariables =
@@ -118,6 +121,13 @@ export class VariableMap
if (!this.variableMap.has(newType)) { if (!this.variableMap.has(newType)) {
this.variableMap.set(newType, newTypeVariables); this.variableMap.set(newType, newTypeVariables);
} }
eventUtils.fire(
new (eventUtils.get(EventType.VAR_TYPE_CHANGE))(
variable,
oldType,
newType,
),
);
return variable; return variable;
} }

View File

@@ -505,5 +505,26 @@ suite('Variable Map', function () {
}); });
}); });
}); });
suite('variable type change events', function () {
test('are fired when a variable has its type changed', function () {
const variable = this.variableMap.createVariable(
'name1',
'type1',
'id1',
);
this.variableMap.changeVariableType(variable, 'type2');
assertEventFired(
this.eventSpy,
Blockly.Events.VarTypeChange,
{
oldType: 'type1',
newType: 'type2',
varId: 'id1',
},
this.workspace.id,
);
});
});
}); });
}); });