From 653556307a1004f189f079fff12e31e6c1b3ce57 Mon Sep 17 00:00:00 2001 From: tygao Date: Wed, 5 Jun 2024 13:37:42 +0800 Subject: [PATCH 1/4] add removedComponentIds for data source selection service to fallback Signed-off-by: tygao --- .../service/data_source_selection_service.test.ts | 12 ++++++++++++ .../public/service/data_source_selection_service.ts | 7 +++++++ 2 files changed, 19 insertions(+) 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..c593aa764919 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 ID 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); From 8a8f629899772623449c90cea986ab12c957144d Mon Sep 17 00:00:00 2001 From: tygao Date: Wed, 5 Jun 2024 13:39:27 +0800 Subject: [PATCH 2/4] update comment Signed-off-by: tygao --- .../public/service/data_source_selection_service.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 c593aa764919..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,8 +8,8 @@ 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 ID to fallback. + // 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[]) => { From ff8b9332b83e458d2e317b2c896c57392c81857a Mon Sep 17 00:00:00 2001 From: tygao Date: Wed, 5 Jun 2024 18:30:29 +0800 Subject: [PATCH 3/4] update component variable snapshot in navigation plugin Signed-off-by: tygao --- .../top_nav_menu/__snapshots__/top_nav_menu.test.tsx.snap | 2 ++ 1 file changed, 2 insertions(+) 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, From ae15acfacdd0fa7e9f9cbded23f694fa27e51c91 Mon Sep 17 00:00:00 2001 From: "opensearch-changeset-bot[bot]" <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com> Date: Thu, 6 Jun 2024 00:59:44 +0000 Subject: [PATCH 4/4] Changeset file for PR #6920 created/updated --- changelogs/fragments/6920.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/6920.yml 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