diff --git a/changelogs/fragments/6920.yml b/changelogs/fragments/6920.yml new file mode 100644 index 000000000000..f688be79b428 --- /dev/null +++ b/changelogs/fragments/6920.yml @@ -0,0 +1,2 @@ +feat: +- [Multi DataSource] Add removedComponentIds for data source selection service ([#6920](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6920)) \ No newline at end of file diff --git a/src/plugins/data_source_management/public/service/data_source_selection_service.test.ts b/src/plugins/data_source_management/public/service/data_source_selection_service.test.ts index 65957379517f..116677704e5f 100644 --- a/src/plugins/data_source_management/public/service/data_source_selection_service.test.ts +++ b/src/plugins/data_source_management/public/service/data_source_selection_service.test.ts @@ -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); + }); }); diff --git a/src/plugins/data_source_management/public/service/data_source_selection_service.ts b/src/plugins/data_source_management/public/service/data_source_selection_service.ts index 560216d88c3a..ced8c8e0c5cc 100644 --- a/src/plugins/data_source_management/public/service/data_source_selection_service.ts +++ b/src/plugins/data_source_management/public/service/data_source_selection_service.ts @@ -8,14 +8,21 @@ import { DataSourceOption } from '../components/data_source_menu/types'; export class DataSourceSelectionService { private selectedDataSource$ = new BehaviorSubject(new Map()); + // 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); diff --git a/src/plugins/navigation/public/top_nav_menu/__snapshots__/top_nav_menu.test.tsx.snap b/src/plugins/navigation/public/top_nav_menu/__snapshots__/top_nav_menu.test.tsx.snap index 6397e6269129..1825c19d2d3d 100644 --- a/src/plugins/navigation/public/top_nav_menu/__snapshots__/top_nav_menu.test.tsx.snap +++ b/src/plugins/navigation/public/top_nav_menu/__snapshots__/top_nav_menu.test.tsx.snap @@ -50,6 +50,7 @@ exports[`TopNavMenu mounts the data source menu as well as top nav menu 1`] = ` "getSelection$": [Function], "getSelectionValue": [Function], "remove": [Function], + "removedComponentIds": Array [], "selectDataSource": [Function], "selectedDataSource$": BehaviorSubject { "_isScalar": false, @@ -93,6 +94,7 @@ exports[`TopNavMenu mounts the data source menu if showDataSourceMenu is true 1` "getSelection$": [Function], "getSelectionValue": [Function], "remove": [Function], + "removedComponentIds": Array [], "selectDataSource": [Function], "selectedDataSource$": BehaviorSubject { "_isScalar": false,