fix: Fire deletion events when clearing variables. (#6827)

https://groups.google.com/g/blockly/c/l_vUnrGSJ0M
This commit is contained in:
Neil Fraser
2023-02-07 21:20:01 +01:00
committed by GitHub
parent 498766b930
commit 90217360c7
2 changed files with 18 additions and 5 deletions

View File

@@ -44,10 +44,18 @@ export class VariableMap {
/** @param workspace The workspace this map belongs to. */
constructor(public workspace: Workspace) {}
/** Clear the variable map. */
/** Clear the variable map. Fires events for every deletion. */
clear() {
this.variableMap.clear();
for (const variables of this.variableMap.values()) {
while (variables.length > 0) {
this.deleteVariable(variables[0]);
}
}
if (this.variableMap.size !== 0) {
throw Error('Non-empty variable map');
}
}
/* Begin functions for renaming variables. */
/**
* Rename the given variable by updating its name in the variable map.
@@ -142,6 +150,7 @@ export class VariableMap {
// And remove it from the list.
arrayUtils.removeElem(this.variableMap.get(type)!, variable);
}
/* End functions for renaming variables. */
/**
* Create a variable with a given name, optional type, and optional ID.
@@ -187,6 +196,7 @@ export class VariableMap {
return variable;
}
/* Begin functions for variable deletion. */
/**
* Delete a variable.
@@ -203,6 +213,9 @@ export class VariableMap {
variableList.splice(i, 1);
eventUtils.fire(
new (eventUtils.get(eventUtils.VAR_DELETE))(variable));
if (variableList.length === 0) {
this.variableMap.delete(variable.type);
}
return;
}
}
@@ -306,7 +319,7 @@ export class VariableMap {
* @returns The variable with the given ID.
*/
getVariableById(id: string): VariableModel|null {
for (const [_key, variables] of this.variableMap) {
for (const variables of this.variableMap.values()) {
for (const variable of variables) {
if (variable.getId() === id) {
return variable;