fix: Don't fire events for changes to potential variables (#9025)

This commit is contained in:
RoboErikG
2025-05-12 10:29:53 -07:00
committed by GitHub
parent 9cf9170815
commit 77bfa5b572
2 changed files with 41 additions and 18 deletions

View File

@@ -46,8 +46,15 @@ export class VariableMap
Map<string, IVariableModel<IVariableState>> Map<string, IVariableModel<IVariableState>>
>(); >();
/** @param workspace The workspace this map belongs to. */ /**
constructor(public workspace: Workspace) {} * @param workspace The workspace this map belongs to.
* @param potentialMap True if this holds variables that don't exist in the
* workspace yet.
*/
constructor(
public workspace: Workspace,
public potentialMap = false,
) {}
/** Clear the variable map. Fires events for every deletion. */ /** Clear the variable map. Fires events for every deletion. */
clear() { clear() {
@@ -77,10 +84,13 @@ export class VariableMap
const type = variable.getType(); const type = variable.getType();
const conflictVar = this.getVariable(newName, type); const conflictVar = this.getVariable(newName, type);
const blocks = this.workspace.getAllBlocks(false); const blocks = this.workspace.getAllBlocks(false);
const existingGroup = eventUtils.getGroup(); let existingGroup = '';
if (!this.potentialMap) {
existingGroup = eventUtils.getGroup();
if (!existingGroup) { if (!existingGroup) {
eventUtils.setGroup(true); eventUtils.setGroup(true);
} }
}
try { try {
// The IDs may match if the rename is a simple case change (name1 -> // The IDs may match if the rename is a simple case change (name1 ->
// Name1). // Name1).
@@ -90,7 +100,7 @@ export class VariableMap
this.renameVariableWithConflict(variable, newName, conflictVar, blocks); this.renameVariableWithConflict(variable, newName, conflictVar, blocks);
} }
} finally { } finally {
eventUtils.setGroup(existingGroup); if (!this.potentialMap) eventUtils.setGroup(existingGroup);
} }
return variable; return variable;
} }
@@ -147,9 +157,11 @@ export class VariableMap
newName: string, newName: string,
blocks: Block[], blocks: Block[],
) { ) {
if (!this.potentialMap) {
eventUtils.fire( eventUtils.fire(
new (eventUtils.get(EventType.VAR_RENAME))(variable, newName), new (eventUtils.get(EventType.VAR_RENAME))(variable, newName),
); );
}
variable.setName(newName); variable.setName(newName);
for (let i = 0; i < blocks.length; i++) { for (let i = 0; i < blocks.length; i++) {
blocks[i].updateVarName(variable); blocks[i].updateVarName(variable);
@@ -186,8 +198,10 @@ export class VariableMap
for (let i = 0; i < blocks.length; i++) { for (let i = 0; i < blocks.length; i++) {
blocks[i].renameVarById(variable.getId(), conflictVar.getId()); blocks[i].renameVarById(variable.getId(), conflictVar.getId());
} }
if (!this.potentialMap) {
// Finally delete the original variable, which is now unreferenced. // Finally delete the original variable, which is now unreferenced.
eventUtils.fire(new (eventUtils.get(EventType.VAR_DELETE))(variable)); eventUtils.fire(new (eventUtils.get(EventType.VAR_DELETE))(variable));
}
// And remove it from the map. // And remove it from the map.
this.variableMap.get(type)?.delete(variable.getId()); this.variableMap.get(type)?.delete(variable.getId());
} }
@@ -248,7 +262,9 @@ export class VariableMap
if (!this.variableMap.has(type)) { if (!this.variableMap.has(type)) {
this.variableMap.set(type, variables); this.variableMap.set(type, variables);
} }
if (!this.potentialMap) {
eventUtils.fire(new (eventUtils.get(EventType.VAR_CREATE))(variable)); eventUtils.fire(new (eventUtils.get(EventType.VAR_CREATE))(variable));
}
return variable; return variable;
} }
@@ -276,10 +292,13 @@ export class VariableMap
*/ */
deleteVariable(variable: IVariableModel<IVariableState>) { deleteVariable(variable: IVariableModel<IVariableState>) {
const uses = getVariableUsesById(this.workspace, variable.getId()); const uses = getVariableUsesById(this.workspace, variable.getId());
const existingGroup = eventUtils.getGroup(); let existingGroup = '';
if (!this.potentialMap) {
existingGroup = eventUtils.getGroup();
if (!existingGroup) { if (!existingGroup) {
eventUtils.setGroup(true); eventUtils.setGroup(true);
} }
}
try { try {
for (let i = 0; i < uses.length; i++) { for (let i = 0; i < uses.length; i++) {
uses[i].dispose(true); uses[i].dispose(true);
@@ -287,14 +306,18 @@ export class VariableMap
const variables = this.variableMap.get(variable.getType()); const variables = this.variableMap.get(variable.getType());
if (!variables || !variables.has(variable.getId())) return; if (!variables || !variables.has(variable.getId())) return;
variables.delete(variable.getId()); variables.delete(variable.getId());
if (!this.potentialMap) {
eventUtils.fire(new (eventUtils.get(EventType.VAR_DELETE))(variable)); eventUtils.fire(new (eventUtils.get(EventType.VAR_DELETE))(variable));
}
if (variables.size === 0) { if (variables.size === 0) {
this.variableMap.delete(variable.getType()); this.variableMap.delete(variable.getType());
} }
} finally { } finally {
if (!this.potentialMap) {
eventUtils.setGroup(existingGroup); eventUtils.setGroup(existingGroup);
} }
} }
}
/** /**
* Delete a variables by the passed in ID and all of its uses from this * Delete a variables by the passed in ID and all of its uses from this

View File

@@ -868,7 +868,7 @@ export class Workspace {
*/ */
createPotentialVariableMap() { createPotentialVariableMap() {
const VariableMap = this.getVariableMapClass(); const VariableMap = this.getVariableMapClass();
this.potentialVariableMap = new VariableMap(this); this.potentialVariableMap = new VariableMap(this, true);
} }
/** /**