Skip to content

Commit 04a31f9

Browse files
authored
fix: Wrap toolbox refreshes in a timeout when modifying variables (#8980)
1 parent 233604a commit 04a31f9

5 files changed

Lines changed: 21 additions & 27 deletions

File tree

blocks/variables.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import '../core/field_label.js';
2121
import {FieldVariable} from '../core/field_variable.js';
2222
import {Msg} from '../core/msg.js';
2323
import * as Variables from '../core/variables.js';
24-
import type {WorkspaceSvg} from '../core/workspace_svg.js';
2524

2625
/**
2726
* A dictionary of the block definitions provided by this module.
@@ -170,7 +169,6 @@ const deleteOptionCallbackFactory = function (
170169
if (variable) {
171170
Variables.deleteVariable(variable.getWorkspace(), variable, block);
172171
}
173-
(block.workspace as WorkspaceSvg).refreshToolboxSelection();
174172
};
175173
};
176174

blocks/variables_dynamic.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import '../core/field_label.js';
2222
import {FieldVariable} from '../core/field_variable.js';
2323
import {Msg} from '../core/msg.js';
2424
import * as Variables from '../core/variables.js';
25-
import type {WorkspaceSvg} from '../core/workspace_svg.js';
2625

2726
/**
2827
* A dictionary of the block definitions provided by this module.
@@ -181,7 +180,6 @@ const deleteOptionCallbackFactory = function (block: VariableBlock) {
181180
if (variable) {
182181
Variables.deleteVariable(variable.getWorkspace(), variable, block);
183182
}
184-
(block.workspace as WorkspaceSvg).refreshToolboxSelection();
185183
};
186184
};
187185

core/field_variable.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import * as dom from './utils/dom.js';
3232
import * as parsing from './utils/parsing.js';
3333
import {Size} from './utils/size.js';
3434
import * as Variables from './variables.js';
35-
import {WorkspaceSvg} from './workspace_svg.js';
3635
import * as Xml from './xml.js';
3736

3837
/**
@@ -523,9 +522,6 @@ export class FieldVariable extends FieldDropdown {
523522
// Delete variable.
524523
const workspace = this.variable.getWorkspace();
525524
Variables.deleteVariable(workspace, this.variable, this.sourceBlock_);
526-
if (workspace instanceof WorkspaceSvg) {
527-
workspace.refreshToolboxSelection();
528-
}
529525
return;
530526
}
531527
}

core/variable_map.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import * as deprecation from './utils/deprecation.js';
2727
import * as idGenerator from './utils/idgenerator.js';
2828
import {deleteVariable, getVariableUsesById} from './variables.js';
2929
import type {Workspace} from './workspace.js';
30-
import {WorkspaceSvg} from './workspace_svg.js';
3130

3231
/**
3332
* Class for a variable map. This contains a dictionary data structure with
@@ -93,9 +92,6 @@ export class VariableMap
9392
} finally {
9493
eventUtils.setGroup(existingGroup);
9594
}
96-
if (this.workspace instanceof WorkspaceSvg) {
97-
this.workspace.refreshToolboxSelection();
98-
}
9995
return variable;
10096
}
10197

@@ -112,9 +108,6 @@ export class VariableMap
112108
if (!this.variableMap.has(newType)) {
113109
this.variableMap.set(newType, newTypeVariables);
114110
}
115-
if (this.workspace instanceof WorkspaceSvg) {
116-
this.workspace.refreshToolboxSelection();
117-
}
118111
return variable;
119112
}
120113

@@ -161,9 +154,6 @@ export class VariableMap
161154
for (let i = 0; i < blocks.length; i++) {
162155
blocks[i].updateVarName(variable);
163156
}
164-
if (this.workspace instanceof WorkspaceSvg) {
165-
this.workspace.refreshToolboxSelection();
166-
}
167157
}
168158

169159
/**
@@ -259,9 +249,6 @@ export class VariableMap
259249
this.variableMap.set(type, variables);
260250
}
261251
eventUtils.fire(new (eventUtils.get(EventType.VAR_CREATE))(variable));
262-
if (this.workspace instanceof WorkspaceSvg) {
263-
this.workspace.refreshToolboxSelection();
264-
}
265252
return variable;
266253
}
267254

@@ -279,9 +266,6 @@ export class VariableMap
279266
);
280267
}
281268
this.variableMap.get(type)?.set(variable.getId(), variable);
282-
if (this.workspace instanceof WorkspaceSvg) {
283-
this.workspace.refreshToolboxSelection();
284-
}
285269
}
286270

287271
/* Begin functions for variable deletion. */
@@ -310,9 +294,6 @@ export class VariableMap
310294
} finally {
311295
eventUtils.setGroup(existingGroup);
312296
}
313-
if (this.workspace instanceof WorkspaceSvg) {
314-
this.workspace.refreshToolboxSelection();
315-
}
316297
}
317298

318299
/**

core/workspace_svg.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
ContextMenuRegistry,
3434
} from './contextmenu_registry.js';
3535
import * as dropDownDiv from './dropdowndiv.js';
36+
import {Abstract as AbstractEvent} from './events/events.js';
3637
import {EventType} from './events/type.js';
3738
import * as eventUtils from './events/utils.js';
3839
import {Flyout} from './flyout_base.js';
@@ -399,6 +400,9 @@ export class WorkspaceSvg
399400
this.addChangeListener(Procedures.mutatorOpenListener);
400401
}
401402

403+
// Set up callbacks to refresh the toolbox when variables change
404+
this.addChangeListener(this.variableChangeCallback.bind(this));
405+
402406
/** Object in charge of storing and updating the workspace theme. */
403407
this.themeManager_ = this.options.parentWorkspace
404408
? this.options.parentWorkspace.getThemeManager()
@@ -1361,6 +1365,23 @@ export class WorkspaceSvg
13611365
}
13621366
}
13631367

1368+
/**
1369+
* Handles any necessary updates when a variable changes.
1370+
*
1371+
* @internal
1372+
*/
1373+
variableChangeCallback(event: AbstractEvent) {
1374+
switch (event.type) {
1375+
case EventType.VAR_CREATE:
1376+
case EventType.VAR_DELETE:
1377+
case EventType.VAR_RENAME:
1378+
case EventType.VAR_TYPE_CHANGE:
1379+
this.refreshToolboxSelection();
1380+
break;
1381+
default:
1382+
}
1383+
}
1384+
13641385
/**
13651386
* Refresh the toolbox unless there's a drag in progress.
13661387
*

0 commit comments

Comments
 (0)