Skip to content

Commit

Permalink
chore: DRY up setState & updateFilters
Browse files Browse the repository at this point in the history
  • Loading branch information
gtk-grafana committed Dec 19, 2024
1 parent 42661b0 commit cdffe34
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ describe.each(['11.1.2', '11.1.1'])('AdHocFiltersVariable', (v) => {
const evtHandler = jest.fn();
variable.subscribeToEvent(SceneVariableValueChangedEvent, evtHandler);

variable.updateFilters(variable.state.filters.slice(0));
variable.updateFilters({filters: variable.state.filters.slice(0)});

expect(evtHandler).not.toHaveBeenCalled();
});
Expand All @@ -987,7 +987,7 @@ describe.each(['11.1.2', '11.1.1'])('AdHocFiltersVariable', (v) => {
const evtHandler = jest.fn();
variable.subscribeToEvent(SceneVariableValueChangedEvent, evtHandler);

variable.updateFilters([{ key: 'key2', operator: '=', value: 'val1' }]);
variable.updateFilters({filters: [{ key: 'key2', operator: '=', value: 'val1' }]});

expect(evtHandler).toHaveBeenCalled();
expect(variable.state.filterExpression).toEqual(`key2="val1"`);
Expand All @@ -1005,7 +1005,7 @@ describe.each(['11.1.2', '11.1.1'])('AdHocFiltersVariable', (v) => {
const evtHandler = jest.fn();
variable.subscribeToEvent(SceneVariableValueChangedEvent, evtHandler);

variable.updateFilters([{ key: 'key2', operator: '=', value: 'val1' }], { skipPublish: true });
variable.updateFilters({filters: [{ key: 'key2', operator: '=', value: 'val1' }]}, { skipPublish: true });

expect(evtHandler).not.toHaveBeenCalled();
expect(variable.state.filterExpression).toEqual(`key2="val1"`);
Expand Down
27 changes: 6 additions & 21 deletions packages/scenes/src/variables/adhoc/AdHocFiltersVariable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,18 +190,7 @@ export class AdHocFiltersVariable
}

public setState(update: Partial<AdHocFiltersVariableState>): void {
let filterExpressionChanged = false;

if (update.filters && update.filters !== this.state.filters && !update.filterExpression) {
update.filterExpression = renderExpression(this.state.expressionBuilder, update.filters);
filterExpressionChanged = update.filterExpression !== this.state.filterExpression;
}

super.setState(update);

if (filterExpressionChanged) {
this.publishEvent(new SceneVariableValueChangedEvent(this), true);
}
this.updateFilters(update)
}

/**
Expand All @@ -210,23 +199,19 @@ export class AdHocFiltersVariable
* allowing consumers to update the filters without triggering dependent data providers.
*/
public updateFilters(
filters: AdHocFilterWithLabels[],
update: Partial<AdHocFiltersVariableState>,
options?: {
skipPublish?: boolean;
}
): void {
let filterExpressionChanged = false;
let filterExpression = undefined;

if (filters && filters !== this.state.filters) {
filterExpression = renderExpression(this.state.expressionBuilder, filters);
filterExpressionChanged = filterExpression !== this.state.filterExpression;
if (update.filters && update.filters !== this.state.filters) {
update.filterExpression = renderExpression(this.state.expressionBuilder, update.filters);
filterExpressionChanged = update.filterExpression !== this.state.filterExpression;
}

super.setState({
filters,
filterExpression,
});
super.setState(update);

if (filterExpressionChanged && options?.skipPublish !== true) {
this.publishEvent(new SceneVariableValueChangedEvent(this), true);
Expand Down

0 comments on commit cdffe34

Please sign in to comment.