Skip to content

Commit

Permalink
SceneVariableSet: Do not propagate variable value changes when a loca…
Browse files Browse the repository at this point in the history
…l variable has the same name (#729)
  • Loading branch information
torkelo committed May 14, 2024
1 parent ae03cb6 commit 81046dc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/scenes/src/variables/sets/SceneVariableSet.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { sceneGraph } from '../../core/sceneGraph';
import { SceneTimeRange } from '../../core/SceneTimeRange';
import { LocalValueVariable } from '../variants/LocalValueVariable';
import { TestObjectWithVariableDependency, TestScene } from '../TestScene';
import { activateFullSceneTree } from '../../utils/test/activateFullSceneTree';

interface SceneTextItemState extends SceneObjectState {
text: string;
Expand Down Expand Up @@ -681,6 +682,35 @@ describe('SceneVariableList', () => {
A.changeValueTo('AB');
expect(B.state.loading).toBe(true);
});

describe('When local value overrides parent variable changes on top level should not propagate', () => {
const topLevelVar = new TestVariable({
name: 'test',
options: [],
value: 'B',
optionsToReturn: [{ label: 'B', value: 'B' }],
delayMs: 0,
});

const nestedScene = new TestObjectWithVariableDependency({
title: '$test',
$variables: new SceneVariableSet({
variables: [new LocalValueVariable({ name: 'test', value: 'nestedValue' })],
}),
});

const scene = new TestScene({
$variables: new SceneVariableSet({ variables: [topLevelVar] }),
nested: nestedScene,
});

activateFullSceneTree(scene);

topLevelVar.changeValueTo('E');

expect(nestedScene.state.didSomethingCount).toBe(0);
expect(nestedScene.state.variableValueChanged).toBe(0);
});
});

describe('When changing a dependency while variable is loading', () => {
Expand Down
8 changes: 8 additions & 0 deletions packages/scenes/src/variables/sets/SceneVariableSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,14 @@ export class SceneVariableSet extends SceneObjectBase<SceneVariableSetState> imp
return;
}

// If we find a nested SceneVariableSet that has a variable with the same name we stop the traversal
if (sceneObject.state.$variables && sceneObject.state.$variables !== this) {
const localVar = sceneObject.state.$variables.getByName(variable.state.name);
if (localVar) {
return;
}
}

if (sceneObject.variableDependency) {
sceneObject.variableDependency.variableUpdateCompleted(variable, hasChanged);
}
Expand Down

0 comments on commit 81046dc

Please sign in to comment.