Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SceneVariableSet: Do not propagate variable value changes when a local variable has the same name #729

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}
}
Comment on lines +328 to +333
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@torkelo wouldn't this only work for s single nested SceneVariableSet? I mean, shouldn't be continue up the hierarchy and make sure other objects also ignore updates in such scenario?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait, sorry, nvm. We are traversing down in here to notify objects. So we exit early if there's a local var, so technically variables down the hierarchy will not be affected by the outer scope variable changes, as they now depend on the local variable which does not change. I was thinking about a scenario that the nested object would depend on the global variable, but that's not really possible, as the local variable scope will take precedence.

Copy link
Member Author

@torkelo torkelo May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dprokop yea, there is maybe a scenario where the local variable set (that has the local variable) should be notified, but we can add that later if we need it


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