Skip to content

Commit 77bfa5b

Browse files
authored
fix: Don't fire events for changes to potential variables (#9025)
1 parent 9cf9170 commit 77bfa5b

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

core/variable_map.ts

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,15 @@ export class VariableMap
4646
Map<string, IVariableModel<IVariableState>>
4747
>();
4848

49-
/** @param workspace The workspace this map belongs to. */
50-
constructor(public workspace: Workspace) {}
49+
/**
50+
* @param workspace The workspace this map belongs to.
51+
* @param potentialMap True if this holds variables that don't exist in the
52+
* workspace yet.
53+
*/
54+
constructor(
55+
public workspace: Workspace,
56+
public potentialMap = false,
57+
) {}
5158

5259
/** Clear the variable map. Fires events for every deletion. */
5360
clear() {
@@ -77,9 +84,12 @@ export class VariableMap
7784
const type = variable.getType();
7885
const conflictVar = this.getVariable(newName, type);
7986
const blocks = this.workspace.getAllBlocks(false);
80-
const existingGroup = eventUtils.getGroup();
81-
if (!existingGroup) {
82-
eventUtils.setGroup(true);
87+
let existingGroup = '';
88+
if (!this.potentialMap) {
89+
existingGroup = eventUtils.getGroup();
90+
if (!existingGroup) {
91+
eventUtils.setGroup(true);
92+
}
8393
}
8494
try {
8595
// The IDs may match if the rename is a simple case change (name1 ->
@@ -90,7 +100,7 @@ export class VariableMap
90100
this.renameVariableWithConflict(variable, newName, conflictVar, blocks);
91101
}
92102
} finally {
93-
eventUtils.setGroup(existingGroup);
103+
if (!this.potentialMap) eventUtils.setGroup(existingGroup);
94104
}
95105
return variable;
96106
}
@@ -147,9 +157,11 @@ export class VariableMap
147157
newName: string,
148158
blocks: Block[],
149159
) {
150-
eventUtils.fire(
151-
new (eventUtils.get(EventType.VAR_RENAME))(variable, newName),
152-
);
160+
if (!this.potentialMap) {
161+
eventUtils.fire(
162+
new (eventUtils.get(EventType.VAR_RENAME))(variable, newName),
163+
);
164+
}
153165
variable.setName(newName);
154166
for (let i = 0; i < blocks.length; i++) {
155167
blocks[i].updateVarName(variable);
@@ -186,8 +198,10 @@ export class VariableMap
186198
for (let i = 0; i < blocks.length; i++) {
187199
blocks[i].renameVarById(variable.getId(), conflictVar.getId());
188200
}
189-
// Finally delete the original variable, which is now unreferenced.
190-
eventUtils.fire(new (eventUtils.get(EventType.VAR_DELETE))(variable));
201+
if (!this.potentialMap) {
202+
// Finally delete the original variable, which is now unreferenced.
203+
eventUtils.fire(new (eventUtils.get(EventType.VAR_DELETE))(variable));
204+
}
191205
// And remove it from the map.
192206
this.variableMap.get(type)?.delete(variable.getId());
193207
}
@@ -248,7 +262,9 @@ export class VariableMap
248262
if (!this.variableMap.has(type)) {
249263
this.variableMap.set(type, variables);
250264
}
251-
eventUtils.fire(new (eventUtils.get(EventType.VAR_CREATE))(variable));
265+
if (!this.potentialMap) {
266+
eventUtils.fire(new (eventUtils.get(EventType.VAR_CREATE))(variable));
267+
}
252268
return variable;
253269
}
254270

@@ -276,9 +292,12 @@ export class VariableMap
276292
*/
277293
deleteVariable(variable: IVariableModel<IVariableState>) {
278294
const uses = getVariableUsesById(this.workspace, variable.getId());
279-
const existingGroup = eventUtils.getGroup();
280-
if (!existingGroup) {
281-
eventUtils.setGroup(true);
295+
let existingGroup = '';
296+
if (!this.potentialMap) {
297+
existingGroup = eventUtils.getGroup();
298+
if (!existingGroup) {
299+
eventUtils.setGroup(true);
300+
}
282301
}
283302
try {
284303
for (let i = 0; i < uses.length; i++) {
@@ -287,12 +306,16 @@ export class VariableMap
287306
const variables = this.variableMap.get(variable.getType());
288307
if (!variables || !variables.has(variable.getId())) return;
289308
variables.delete(variable.getId());
290-
eventUtils.fire(new (eventUtils.get(EventType.VAR_DELETE))(variable));
309+
if (!this.potentialMap) {
310+
eventUtils.fire(new (eventUtils.get(EventType.VAR_DELETE))(variable));
311+
}
291312
if (variables.size === 0) {
292313
this.variableMap.delete(variable.getType());
293314
}
294315
} finally {
295-
eventUtils.setGroup(existingGroup);
316+
if (!this.potentialMap) {
317+
eventUtils.setGroup(existingGroup);
318+
}
296319
}
297320
}
298321

core/workspace.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ export class Workspace {
868868
*/
869869
createPotentialVariableMap() {
870870
const VariableMap = this.getVariableMapClass();
871-
this.potentialVariableMap = new VariableMap(this);
871+
this.potentialVariableMap = new VariableMap(this, true);
872872
}
873873

874874
/**

0 commit comments

Comments
 (0)