Skip to content

Commit 9f66f0c

Browse files
authored
fix: Fire a VarTypeChange event when changing a variable's type. (#9236)
1 parent 52634e4 commit 9f66f0c

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

core/variable_map.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ export class VariableMap
109109
variable: IVariableModel<IVariableState>,
110110
newType: string,
111111
): IVariableModel<IVariableState> {
112+
const oldType = variable.getType();
113+
if (oldType === newType) return variable;
114+
112115
this.variableMap.get(variable.getType())?.delete(variable.getId());
113116
variable.setType(newType);
114117
const newTypeVariables =
@@ -118,6 +121,13 @@ export class VariableMap
118121
if (!this.variableMap.has(newType)) {
119122
this.variableMap.set(newType, newTypeVariables);
120123
}
124+
eventUtils.fire(
125+
new (eventUtils.get(EventType.VAR_TYPE_CHANGE))(
126+
variable,
127+
oldType,
128+
newType,
129+
),
130+
);
121131
return variable;
122132
}
123133

tests/mocha/variable_map_test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,5 +505,26 @@ suite('Variable Map', function () {
505505
});
506506
});
507507
});
508+
509+
suite('variable type change events', function () {
510+
test('are fired when a variable has its type changed', function () {
511+
const variable = this.variableMap.createVariable(
512+
'name1',
513+
'type1',
514+
'id1',
515+
);
516+
this.variableMap.changeVariableType(variable, 'type2');
517+
assertEventFired(
518+
this.eventSpy,
519+
Blockly.Events.VarTypeChange,
520+
{
521+
oldType: 'type1',
522+
newType: 'type2',
523+
varId: 'id1',
524+
},
525+
this.workspace.id,
526+
);
527+
});
528+
});
508529
});
509530
});

0 commit comments

Comments
 (0)