@@ -37,7 +37,7 @@ export class SceneVariableSet extends SceneObjectBase<SceneVariableSetState> imp
37
37
) ;
38
38
39
39
// Subscribe to state changes
40
- this . _subs . add ( this . subscribeToState ( ( state ) => this . handleStateChanged ( state ) ) ) ;
40
+ this . _subs . add ( this . subscribeToState ( this . handleStateChanged ) ) ;
41
41
42
42
this . checkForVariablesThatChangedWhileInactive ( ) ;
43
43
@@ -54,25 +54,35 @@ export class SceneVariableSet extends SceneObjectBase<SceneVariableSetState> imp
54
54
/**
55
55
* Look for new variables that need to be initialized
56
56
*/
57
- private handleStateChanged ( state : SceneVariableSetState ) {
57
+ private handleStateChanged = ( newState : SceneVariableSetState , oldState : SceneVariableSetState ) => {
58
58
const variablesToUpdateCountStart = this . _variablesToUpdate . size ;
59
59
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
+ }
68
78
}
69
79
}
70
80
71
81
// Only start a new batch if there was no batch already running
72
82
if ( variablesToUpdateCountStart === 0 && this . _variablesToUpdate . size > 0 ) {
73
83
this . updateNextBatch ( ) ;
74
84
}
75
- }
85
+ } ;
76
86
77
87
/**
78
88
* 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