Skip to content

Commit

Permalink
[Multi DataSource] Add removedComponentIds for data source selection …
Browse files Browse the repository at this point in the history
…service (opensearch-project#6920)

* add removedComponentIds for data source selection service to fallback

Signed-off-by: tygao <[email protected]>

* update comment

Signed-off-by: tygao <[email protected]>

* update component variable snapshot in navigation plugin

Signed-off-by: tygao <[email protected]>

* Changeset file for PR opensearch-project#6920 created/updated

---------

Signed-off-by: tygao <[email protected]>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
Co-authored-by: Lu Yu <[email protected]>
  • Loading branch information
3 people authored Jun 6, 2024
1 parent 61e29b5 commit efab1a8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
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.

0 comments on commit efab1a8

Please sign in to comment.