Skip to content

Commit 01a3944

Browse files
committed
Simplify logic
1 parent 7e5f48f commit 01a3944

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

packages/scenes/src/variables/sets/SceneVariableSet.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class SceneVariableSet extends SceneObjectBase<SceneVariableSetState> imp
3737
);
3838

3939
// Subscribe to state changes
40-
this._subs.add(this.subscribeToState((state) => this.handleStateChanged(state)));
40+
this._subs.add(this.subscribeToState(this.handleStateChanged));
4141

4242
this.checkForVariablesThatChangedWhileInactive();
4343

@@ -54,25 +54,35 @@ export class SceneVariableSet extends SceneObjectBase<SceneVariableSetState> imp
5454
/**
5555
* Look for new variables that need to be initialized
5656
*/
57-
private handleStateChanged(state: SceneVariableSetState) {
57+
private handleStateChanged = (newState: SceneVariableSetState, oldState: SceneVariableSetState) => {
5858
const variablesToUpdateCountStart = this._variablesToUpdate.size;
5959

60-
for (const variable of state.variables) {
61-
// If this is a new variable
62-
if (
63-
!this._variablesToUpdate.has(variable) &&
64-
!this._updating.has(variable) &&
65-
!this._variableValueRecorder.hasRecordedValue(variable)
66-
) {
67-
this._variablesToUpdate.add(variable);
60+
// Check for removed variables
61+
for (const variable of oldState.variables) {
62+
if (!newState.variables.includes(variable)) {
63+
const updating = this._updating.get(variable);
64+
if (updating?.subscription) {
65+
updating.subscription.unsubscribe();
66+
}
67+
this._updating.delete(variable);
68+
this._variablesToUpdate.delete(variable);
69+
}
70+
}
71+
72+
// Check for new variables
73+
for (const variable of newState.variables) {
74+
if (!oldState.variables.includes(variable)) {
75+
if (this.variableNeedsUpdate(variable)) {
76+
this._variablesToUpdate.add(variable);
77+
}
6878
}
6979
}
7080

7181
// Only start a new batch if there was no batch already running
7282
if (variablesToUpdateCountStart === 0 && this._variablesToUpdate.size > 0) {
7383
this.updateNextBatch();
7484
}
75-
}
85+
};
7686

7787
/**
7888
* If variables changed while in in-active state we don't get any change events, so we need to check for that here.

0 commit comments

Comments
 (0)