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

[Multi DataSource] Add removedComponentIds for data source selection service #6920

Merged
merged 5 commits into from
Jun 6, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/6920.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
feat:
- [Multi DataSource] Add removedComponentIds for data source selection service ([#6920](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6920))
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,16 @@ describe('DataSourceSelectionService service', () => {
done();
});
});

it('should not store same id selection after calling remove', () => {
const dataSourceSelection = new DataSourceSelectionService();
const id = generateComponentId();
const dataSource = { id: 'id', label: 'label' };
dataSourceSelection.selectDataSource(id, [dataSource]);
expect(dataSourceSelection.getSelectionValue().get(id)).toStrictEqual([dataSource]);
dataSourceSelection.remove(id);
expect(dataSourceSelection.getSelectionValue().get(id)).toStrictEqual(undefined);
dataSourceSelection.selectDataSource(id, [dataSource]);
expect(dataSourceSelection.getSelectionValue().get(id)).toStrictEqual(undefined);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ import { DataSourceOption } from '../components/data_source_menu/types';

export class DataSourceSelectionService {
private selectedDataSource$ = new BehaviorSubject(new Map<string, DataSourceOption[]>());
// Some components may call onSelect function in promise.then, which could be executed later than componentWillUnmount.
// Use this array to record unmounted component IDs to fallback.
private removedComponentIds: string[] = [];

public selectDataSource = (componentId: string, dataSource: DataSourceOption[]) => {
if (this.removedComponentIds.indexOf(componentId) > -1) {
return;
}
const newMap = new Map(this.selectedDataSource$.value);
newMap.set(componentId, dataSource);
this.selectedDataSource$.next(newMap);
};

public remove = (componentId: string) => {
this.removedComponentIds.push(componentId);
const newMap = new Map(this.selectedDataSource$.value);
newMap.delete(componentId);
this.selectedDataSource$.next(newMap);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading