From 279a861c0242e885d65b6243ed705c758c10ef31 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Fri, 5 Sep 2025 16:12:52 +0200 Subject: [PATCH 1/2] Remove check for newScopes.length --- .../scenes/src/variables/variants/ScopesVariable.test.tsx | 8 ++++++++ packages/scenes/src/variables/variants/ScopesVariable.tsx | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/scenes/src/variables/variants/ScopesVariable.test.tsx b/packages/scenes/src/variables/variants/ScopesVariable.test.tsx index 6cc17f0cb..527aa44a5 100644 --- a/packages/scenes/src/variables/variants/ScopesVariable.test.tsx +++ b/packages/scenes/src/variables/variants/ScopesVariable.test.tsx @@ -74,6 +74,14 @@ describe('ScopesVariable', () => { const { valueChangedCount } = renderTestScene({ initialScopes: [] }); expect(valueChangedCount.value).toEqual(1); }); + + it('should not emit a change event if the old and new scopes are empty', async () => { + const { scopesContext, valueChangedCount } = renderTestScene({ initialScopes: [] }); + + act(() => scopesContext.changeScopes([])); + + expect(valueChangedCount.value).toEqual(0); + }); }); interface SetupOptions { diff --git a/packages/scenes/src/variables/variants/ScopesVariable.tsx b/packages/scenes/src/variables/variants/ScopesVariable.tsx index d97d1625e..5855b158a 100644 --- a/packages/scenes/src/variables/variants/ScopesVariable.tsx +++ b/packages/scenes/src/variables/variants/ScopesVariable.tsx @@ -100,7 +100,7 @@ export class ScopesVariable extends SceneObjectBase impleme const scopesHaveChanged = !isEqual(oldScopes, newScopes); // Only update scopes value state when loading is false and the scopes have changed - if (!loading && (scopesHaveChanged || newScopes.length === 0)) { + if (!loading && scopesHaveChanged) { const queryController = getQueryController(this); queryController?.startProfile(SCOPES_CHANGED_INTERACTION); this.setState({ scopes: state.value, loading }); From d43e4100b23ab3bd1d94c689a0874f083706d3db Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Fri, 5 Sep 2025 16:24:43 +0200 Subject: [PATCH 2/2] Set flag for initial update --- .../scenes/src/variables/variants/ScopesVariable.test.tsx | 3 ++- packages/scenes/src/variables/variants/ScopesVariable.tsx | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/scenes/src/variables/variants/ScopesVariable.test.tsx b/packages/scenes/src/variables/variants/ScopesVariable.test.tsx index 527aa44a5..3c908fac5 100644 --- a/packages/scenes/src/variables/variants/ScopesVariable.test.tsx +++ b/packages/scenes/src/variables/variants/ScopesVariable.test.tsx @@ -78,9 +78,10 @@ describe('ScopesVariable', () => { it('should not emit a change event if the old and new scopes are empty', async () => { const { scopesContext, valueChangedCount } = renderTestScene({ initialScopes: [] }); + act(() => scopesContext.changeScopes([])); act(() => scopesContext.changeScopes([])); - expect(valueChangedCount.value).toEqual(0); + expect(valueChangedCount.value).toEqual(1); }); }); diff --git a/packages/scenes/src/variables/variants/ScopesVariable.tsx b/packages/scenes/src/variables/variants/ScopesVariable.tsx index 5855b158a..33273c265 100644 --- a/packages/scenes/src/variables/variants/ScopesVariable.tsx +++ b/packages/scenes/src/variables/variants/ScopesVariable.tsx @@ -30,6 +30,8 @@ export interface ScopesVariableState extends SceneVariableState { export class ScopesVariable extends SceneObjectBase implements SceneVariable { protected _renderBeforeActivation = true; protected _context: ScopesContextValue | undefined; + // Used to always emit an update event on initial load + private _isInitialUpdate = true; // Special options that enables variables to be hidden but still render to access react contexts public UNSAFE_renderAsHidden = true; @@ -100,11 +102,13 @@ export class ScopesVariable extends SceneObjectBase impleme const scopesHaveChanged = !isEqual(oldScopes, newScopes); // Only update scopes value state when loading is false and the scopes have changed - if (!loading && scopesHaveChanged) { + // Emit on initial load to ensure that the variable is updated when the scopes are empty + if (!loading && (scopesHaveChanged || this._isInitialUpdate)) { const queryController = getQueryController(this); queryController?.startProfile(SCOPES_CHANGED_INTERACTION); this.setState({ scopes: state.value, loading }); this.publishEvent(new SceneVariableValueChangedEvent(this), true); + this._isInitialUpdate = false; } else { this.setState({ loading }); }