@@ -39,6 +39,9 @@ export class SceneVariableSet extends SceneObjectBase<SceneVariableSetState> imp
39
39
this . subscribeToEvent ( SceneVariableValueChangedEvent , ( event ) => this . handleVariableValueChanged ( event . payload ) )
40
40
) ;
41
41
42
+ // Subscribe to state changes
43
+ this . _subs . add ( this . subscribeToState ( this . handleStateChanged ) ) ;
44
+
42
45
this . checkForVariablesThatChangedWhileInactive ( ) ;
43
46
44
47
// Add all variables that need updating to queue
@@ -74,6 +77,39 @@ export class SceneVariableSet extends SceneObjectBase<SceneVariableSetState> imp
74
77
this . _updating . clear ( ) ;
75
78
} ;
76
79
80
+ /**
81
+ * Look for new variables that need to be initialized
82
+ */
83
+ private handleStateChanged = ( newState : SceneVariableSetState , oldState : SceneVariableSetState ) => {
84
+ const variablesToUpdateCountStart = this . _variablesToUpdate . size ;
85
+
86
+ // Check for removed variables
87
+ for ( const variable of oldState . variables ) {
88
+ if ( ! newState . variables . includes ( variable ) ) {
89
+ const updating = this . _updating . get ( variable ) ;
90
+ if ( updating ?. subscription ) {
91
+ updating . subscription . unsubscribe ( ) ;
92
+ }
93
+ this . _updating . delete ( variable ) ;
94
+ this . _variablesToUpdate . delete ( variable ) ;
95
+ }
96
+ }
97
+
98
+ // Check for new variables
99
+ for ( const variable of newState . variables ) {
100
+ if ( ! oldState . variables . includes ( variable ) ) {
101
+ if ( this . variableNeedsUpdate ( variable ) ) {
102
+ this . _variablesToUpdate . add ( variable ) ;
103
+ }
104
+ }
105
+ }
106
+
107
+ // Only start a new batch if there was no batch already running
108
+ if ( variablesToUpdateCountStart === 0 && this . _variablesToUpdate . size > 0 ) {
109
+ this . updateNextBatch ( ) ;
110
+ }
111
+ } ;
112
+
77
113
/**
78
114
* If variables changed while in in-active state we don't get any change events, so we need to check for that here.
79
115
*/
0 commit comments