From 8b50b04bb5bf98d7fed704aa887a4c98b87105e1 Mon Sep 17 00:00:00 2001 From: Jordan Jenkins Date: Tue, 5 May 2026 12:57:43 -0500 Subject: [PATCH 1/5] fix(native-filters): populate granularity_sqla from datasource main_dttm_col for time-range parent cascade --- .../FilterBar/FilterControls/FilterValue.tsx | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/superset-frontend/src/dashboard/components/nativeFilters/FilterBar/FilterControls/FilterValue.tsx b/superset-frontend/src/dashboard/components/nativeFilters/FilterBar/FilterControls/FilterValue.tsx index c23f2578718d..1e4bbe3bfc55 100644 --- a/superset-frontend/src/dashboard/components/nativeFilters/FilterBar/FilterControls/FilterValue.tsx +++ b/superset-frontend/src/dashboard/components/nativeFilters/FilterBar/FilterControls/FilterValue.tsx @@ -53,6 +53,7 @@ import { onFiltersRefreshSuccess, setDirectPathToChild, } from 'src/dashboard/actions/dashboardState'; +import { fetchDatasourceMetadata } from 'src/dashboard/actions/datasources'; import { setHoveredChartCustomization, unsetHoveredChartCustomization, @@ -185,6 +186,28 @@ const FilterValue: FC = ({ const [isRefreshing, setIsRefreshing] = useState(false); const dispatch = useDispatch(); + // When a Date Range parent filter is active, the child filter needs + // granularity_sqla to apply the temporal WHERE clause. Read main_dttm_col + // from the Redux datasource cache as a fallback. + // + // The dashboard datasets API (/api/v1/dashboard/:id/datasets) only returns + // datasets used by chart slices, so native-filter-only datasets are never + // in state.datasources. Dispatch fetchDatasourceMetadata at mount time to + // populate the cache explicitly for those datasets. + const datasourceMainDttmCol = useSelector( + state => + datasetId != null + ? (state.datasources as Record)?.[`${datasetId}__table`] + ?.main_dttm_col + : undefined, + ); + + useEffect(() => { + if (datasetId != null) { + dispatch(fetchDatasourceMetadata(`${datasetId}__table`)); + } + }, [datasetId, dispatch]); + const { outlinedFilterId, lastUpdated } = useFilterOutlined(); const handleFilterLoadFinish = useCallback(() => { @@ -216,7 +239,11 @@ const FilterValue: FC = ({ groupby, adhoc_filters: adhocFilters, time_range: timeRange, - granularity_sqla: granularitySqla, + granularity_sqla: + granularitySqla || + (dependencies.time_range + ? datasourceMainDttmCol ?? undefined + : undefined), dashboardId, }); const filterOwnState = filter.dataMask?.ownState || {}; @@ -347,6 +374,7 @@ const FilterValue: FC = ({ dataMaskSelected, setHasDepsFilterValue, transitiveParentIds, + datasourceMainDttmCol, ]); useEffect(() => { From b582c8afee7d743dee454f827a3c9bbf11aa1216 Mon Sep 17 00:00:00 2001 From: Jordan Jenkins Date: Tue, 5 May 2026 13:11:09 -0500 Subject: [PATCH 2/5] fix(native-filters): deduplicate in-flight fetchDatasourceMetadata requests When multiple FilterValue components share the same datasetId and mount simultaneously, each sees an empty Redux cache and dispatches fetchDatasourceMetadata. The thunk already short-circuits once the datasource is cached, but has no guard for concurrent in-flight requests. Add a module-level Set to track keys currently being fetched so that parallel dispatches for the same key skip the network call. The Set entry is cleared on both success and error so a failed request can be retried. Addresses review feedback on the FilterValue granularity_sqla fix. Co-Authored-By: Claude Sonnet 4.6 --- .../src/dashboard/actions/datasources.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/superset-frontend/src/dashboard/actions/datasources.ts b/superset-frontend/src/dashboard/actions/datasources.ts index f48c12428459..1745bcbb5cc6 100644 --- a/superset-frontend/src/dashboard/actions/datasources.ts +++ b/superset-frontend/src/dashboard/actions/datasources.ts @@ -52,6 +52,8 @@ export function setDatasource(datasource: Datasource, key: string) { }; } +const inFlightDatasourceKeys = new Set(); + export function fetchDatasourceMetadata(key: string) { return (dispatch: Dispatch, getState: () => RootState) => { const { datasources } = getState(); @@ -61,8 +63,21 @@ export function fetchDatasourceMetadata(key: string) { return dispatch(setDatasource(datasource, key)); } + if (inFlightDatasourceKeys.has(key)) { + return undefined; + } + + inFlightDatasourceKeys.add(key); return SupersetClient.get({ endpoint: `/superset/fetch_datasource_metadata?datasourceKey=${key}`, - }).then(({ json }) => dispatch(setDatasource(json as Datasource, key))); + }) + .then(({ json }) => { + inFlightDatasourceKeys.delete(key); + return dispatch(setDatasource(json as Datasource, key)); + }) + .catch((err: unknown) => { + inFlightDatasourceKeys.delete(key); + throw err; + }); }; } From 010f05304f76b92e2fa12fc03f84d8fcdc3a4211 Mon Sep 17 00:00:00 2001 From: Jordan Jenkins Date: Mon, 22 Jun 2026 19:32:22 -0500 Subject: [PATCH 3/5] test(native-filters): pin granularity cascade + dedup, drop any cast Address review feedback on the time-range parent cascade fix: - Remove the `state.datasources as Record` cast in FilterValue; state.datasources is already typed as DatasourcesState, so main_dttm_col resolves to string | undefined without `any`. - Extract the granularity_sqla resolution into the pure, exported resolveGranularitySqla() helper and cover the cascade cases (own value wins, main_dttm_col fallback, no-dependency, no-dttm). - Add datasources.test.ts covering the in-flight dedup Set: cache hit short-circuit, single fetch, concurrent de-duplication, and key cleanup after a failed request so it can retry. - Re-run prettier on FilterValue.tsx to fix the failing format check. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../src/dashboard/actions/datasources.test.ts | 105 ++++++++++++++++++ .../FilterValue.timeGrain.test.ts | 25 ++++- .../FilterBar/FilterControls/FilterValue.tsx | 29 +++-- 3 files changed, 150 insertions(+), 9 deletions(-) create mode 100644 superset-frontend/src/dashboard/actions/datasources.test.ts diff --git a/superset-frontend/src/dashboard/actions/datasources.test.ts b/superset-frontend/src/dashboard/actions/datasources.test.ts new file mode 100644 index 000000000000..b8a1df3e5a91 --- /dev/null +++ b/superset-frontend/src/dashboard/actions/datasources.test.ts @@ -0,0 +1,105 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { SupersetClient } from '@superset-ui/core'; +import { RootState } from 'src/dashboard/types'; +import { + DatasourcesAction, + fetchDatasourceMetadata, + setDatasource, +} from './datasources'; + +const KEY = '7__table'; + +const buildGetState = + (datasources: Record = {}) => + () => + ({ datasources }) as RootState; + +afterEach(() => { + jest.restoreAllMocks(); +}); + +test('fetchDatasourceMetadata uses the cached datasource without a network call', () => { + const getSpy = jest.spyOn(SupersetClient, 'get'); + const dispatch = jest.fn(); + const cached = { uid: KEY, main_dttm_col: 'ds' } as any; + + fetchDatasourceMetadata(KEY)(dispatch, buildGetState({ [KEY]: cached })); + + expect(getSpy).not.toHaveBeenCalled(); + expect(dispatch).toHaveBeenCalledWith(setDatasource(cached, KEY)); +}); + +test('fetchDatasourceMetadata fetches and caches an uncached datasource', async () => { + const json = { uid: KEY, main_dttm_col: 'ds' }; + const getSpy = jest + .spyOn(SupersetClient, 'get') + .mockResolvedValue({ json } as any); + const dispatch = jest.fn(); + + await fetchDatasourceMetadata(KEY)(dispatch, buildGetState()); + + expect(getSpy).toHaveBeenCalledTimes(1); + expect(dispatch).toHaveBeenCalledWith( + expect.objectContaining({ + type: DatasourcesAction.SetDatasource, + key: KEY, + datasource: json, + }), + ); +}); + +test('fetchDatasourceMetadata deduplicates concurrent in-flight requests for the same key', async () => { + let resolveGet: (value: unknown) => void = () => {}; + const getSpy = jest.spyOn(SupersetClient, 'get').mockReturnValue( + new Promise(resolve => { + resolveGet = resolve; + }) as any, + ); + const dispatch = jest.fn(); + const getState = buildGetState(); + + // Two filters using the same dataset mount and dispatch before the first + // request resolves and populates the cache. + fetchDatasourceMetadata(KEY)(dispatch, getState); + fetchDatasourceMetadata(KEY)(dispatch, getState); + + expect(getSpy).toHaveBeenCalledTimes(1); + + resolveGet({ json: { uid: KEY, main_dttm_col: 'ds' } }); + await Promise.resolve(); +}); + +test('fetchDatasourceMetadata clears the in-flight key after a failed request so it can retry', async () => { + const getSpy = jest + .spyOn(SupersetClient, 'get') + .mockRejectedValueOnce(new Error('boom')) + .mockResolvedValueOnce({ json: { uid: KEY } } as any); + const dispatch = jest.fn(); + const getState = buildGetState(); + + await expect( + fetchDatasourceMetadata(KEY)(dispatch, getState), + ).rejects.toThrow('boom'); + + // The failed key is no longer in flight, so a subsequent dispatch retries. + await fetchDatasourceMetadata(KEY)(dispatch, getState); + + expect(getSpy).toHaveBeenCalledTimes(2); +}); diff --git a/superset-frontend/src/dashboard/components/nativeFilters/FilterBar/FilterControls/FilterValue.timeGrain.test.ts b/superset-frontend/src/dashboard/components/nativeFilters/FilterBar/FilterControls/FilterValue.timeGrain.test.ts index 6680e8e70b89..f398a591d300 100644 --- a/superset-frontend/src/dashboard/components/nativeFilters/FilterBar/FilterControls/FilterValue.timeGrain.test.ts +++ b/superset-frontend/src/dashboard/components/nativeFilters/FilterBar/FilterControls/FilterValue.timeGrain.test.ts @@ -18,7 +18,7 @@ */ import { ChartDataResponseResult } from '@superset-ui/core'; -import { applyTimeGrainAllowlist } from './FilterValue'; +import { applyTimeGrainAllowlist, resolveGranularitySqla } from './FilterValue'; const baseResults = [ { @@ -58,3 +58,26 @@ test('applyTimeGrainAllowlist should return unfiltered results when allowlist is const filtered = applyTimeGrainAllowlist('filter_timegrain', [], baseResults); expect(filtered).toEqual(baseResults); }); + +test('resolveGranularitySqla prefers the filter own granularity_sqla', () => { + expect(resolveGranularitySqla('order_date', true, 'main_dttm')).toEqual( + 'order_date', + ); + expect(resolveGranularitySqla('order_date', false, 'main_dttm')).toEqual( + 'order_date', + ); +}); + +test('resolveGranularitySqla falls back to main_dttm_col when a time_range parent cascades', () => { + expect(resolveGranularitySqla(undefined, true, 'main_dttm')).toEqual( + 'main_dttm', + ); +}); + +test('resolveGranularitySqla returns undefined without a time_range dependency', () => { + expect(resolveGranularitySqla(undefined, false, 'main_dttm')).toBeUndefined(); +}); + +test('resolveGranularitySqla returns undefined when the datasource has no main_dttm_col', () => { + expect(resolveGranularitySqla(undefined, true, undefined)).toBeUndefined(); +}); diff --git a/superset-frontend/src/dashboard/components/nativeFilters/FilterBar/FilterControls/FilterValue.tsx b/superset-frontend/src/dashboard/components/nativeFilters/FilterBar/FilterControls/FilterValue.tsx index 1e4bbe3bfc55..b39be94b7b70 100644 --- a/superset-frontend/src/dashboard/components/nativeFilters/FilterBar/FilterControls/FilterValue.tsx +++ b/superset-frontend/src/dashboard/components/nativeFilters/FilterBar/FilterControls/FilterValue.tsx @@ -114,6 +114,20 @@ export const applyTimeGrainAllowlist = ( }); }; +/** + * Resolve the granularity_sqla a filter should send. A filter's own + * granularity_sqla always wins. Otherwise, when a Date Range parent filter is + * cascading a time_range down, fall back to the datasource's main_dttm_col so + * the temporal WHERE clause can be applied. + */ +export const resolveGranularitySqla = ( + granularitySqla: string | undefined, + hasTimeRangeDependency: boolean, + datasourceMainDttmCol: string | undefined, +): string | undefined => + granularitySqla || + (hasTimeRangeDependency ? datasourceMainDttmCol : undefined); + const useShouldFilterRefresh = () => { const isDashboardRefreshing = useSelector( state => state.dashboardState.isRefreshing, @@ -194,11 +208,10 @@ const FilterValue: FC = ({ // datasets used by chart slices, so native-filter-only datasets are never // in state.datasources. Dispatch fetchDatasourceMetadata at mount time to // populate the cache explicitly for those datasets. - const datasourceMainDttmCol = useSelector( + const datasourceMainDttmCol = useSelector( state => datasetId != null - ? (state.datasources as Record)?.[`${datasetId}__table`] - ?.main_dttm_col + ? state.datasources?.[`${datasetId}__table`]?.main_dttm_col : undefined, ); @@ -239,11 +252,11 @@ const FilterValue: FC = ({ groupby, adhoc_filters: adhocFilters, time_range: timeRange, - granularity_sqla: - granularitySqla || - (dependencies.time_range - ? datasourceMainDttmCol ?? undefined - : undefined), + granularity_sqla: resolveGranularitySqla( + granularitySqla, + Boolean(dependencies.time_range), + datasourceMainDttmCol, + ), dashboardId, }); const filterOwnState = filter.dataMask?.ownState || {}; From 3c99fa260878bf792f0611cf18c9bd21d86ead4f Mon Sep 17 00:00:00 2001 From: Jordan Jenkins Date: Tue, 23 Jun 2026 13:04:25 -0500 Subject: [PATCH 4/5] fix(native-filters): handle metadata fetch rejection, drop any from tests Address follow-up review feedback: - FilterValue: the mount-time fetchDatasourceMetadata dispatch returns a promise that can reject (the thunk re-throws to clear its in-flight guard). Wrap the dispatch and log on failure so a failed best-effort cache prime can't surface as an unhandled promise rejection. The fetch is non-fatal, so the filter still works without the cascade fallback. - datasources.test.ts: replace the `any` casts (state helper, cached datasource fixture, and SupersetClient.get response mocks) with proper types (DatasourcesState, Datasource, and the client's resolved response type), honoring the repo's no-`any` rule for changed TypeScript. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../src/dashboard/actions/datasources.test.ts | 31 ++++++++++++------- .../FilterBar/FilterControls/FilterValue.tsx | 11 ++++++- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/superset-frontend/src/dashboard/actions/datasources.test.ts b/superset-frontend/src/dashboard/actions/datasources.test.ts index b8a1df3e5a91..ce7e7c50caa7 100644 --- a/superset-frontend/src/dashboard/actions/datasources.test.ts +++ b/superset-frontend/src/dashboard/actions/datasources.test.ts @@ -16,8 +16,8 @@ * specific language governing permissions and limitations * under the License. */ -import { SupersetClient } from '@superset-ui/core'; -import { RootState } from 'src/dashboard/types'; +import { JsonObject, SupersetClient } from '@superset-ui/core'; +import { Datasource, DatasourcesState, RootState } from 'src/dashboard/types'; import { DatasourcesAction, fetchDatasourceMetadata, @@ -26,10 +26,19 @@ import { const KEY = '7__table'; +type ClientResponse = Awaited>; + +// Minimal typed stand-ins so the tests avoid `any` while exercising the thunk. +const datasource = (json: JsonObject = { uid: KEY, main_dttm_col: 'ds' }) => + json as unknown as Datasource; + +const jsonResponse = (json: JsonObject): ClientResponse => + ({ json }) as unknown as ClientResponse; + const buildGetState = - (datasources: Record = {}) => + (datasources: DatasourcesState = {}) => () => - ({ datasources }) as RootState; + ({ datasources }) as unknown as RootState; afterEach(() => { jest.restoreAllMocks(); @@ -38,7 +47,7 @@ afterEach(() => { test('fetchDatasourceMetadata uses the cached datasource without a network call', () => { const getSpy = jest.spyOn(SupersetClient, 'get'); const dispatch = jest.fn(); - const cached = { uid: KEY, main_dttm_col: 'ds' } as any; + const cached = datasource(); fetchDatasourceMetadata(KEY)(dispatch, buildGetState({ [KEY]: cached })); @@ -50,7 +59,7 @@ test('fetchDatasourceMetadata fetches and caches an uncached datasource', async const json = { uid: KEY, main_dttm_col: 'ds' }; const getSpy = jest .spyOn(SupersetClient, 'get') - .mockResolvedValue({ json } as any); + .mockResolvedValue(jsonResponse(json)); const dispatch = jest.fn(); await fetchDatasourceMetadata(KEY)(dispatch, buildGetState()); @@ -66,11 +75,11 @@ test('fetchDatasourceMetadata fetches and caches an uncached datasource', async }); test('fetchDatasourceMetadata deduplicates concurrent in-flight requests for the same key', async () => { - let resolveGet: (value: unknown) => void = () => {}; + let resolveGet: (value: ClientResponse) => void = () => {}; const getSpy = jest.spyOn(SupersetClient, 'get').mockReturnValue( - new Promise(resolve => { + new Promise(resolve => { resolveGet = resolve; - }) as any, + }), ); const dispatch = jest.fn(); const getState = buildGetState(); @@ -82,7 +91,7 @@ test('fetchDatasourceMetadata deduplicates concurrent in-flight requests for the expect(getSpy).toHaveBeenCalledTimes(1); - resolveGet({ json: { uid: KEY, main_dttm_col: 'ds' } }); + resolveGet(jsonResponse({ uid: KEY, main_dttm_col: 'ds' })); await Promise.resolve(); }); @@ -90,7 +99,7 @@ test('fetchDatasourceMetadata clears the in-flight key after a failed request so const getSpy = jest .spyOn(SupersetClient, 'get') .mockRejectedValueOnce(new Error('boom')) - .mockResolvedValueOnce({ json: { uid: KEY } } as any); + .mockResolvedValueOnce(jsonResponse({ uid: KEY })); const dispatch = jest.fn(); const getState = buildGetState(); diff --git a/superset-frontend/src/dashboard/components/nativeFilters/FilterBar/FilterControls/FilterValue.tsx b/superset-frontend/src/dashboard/components/nativeFilters/FilterBar/FilterControls/FilterValue.tsx index b39be94b7b70..381dd615b0e2 100644 --- a/superset-frontend/src/dashboard/components/nativeFilters/FilterBar/FilterControls/FilterValue.tsx +++ b/superset-frontend/src/dashboard/components/nativeFilters/FilterBar/FilterControls/FilterValue.tsx @@ -42,6 +42,7 @@ import { isChartCustomization, } from '@superset-ui/core'; import { styled } from '@apache-superset/core/theme'; +import { logging } from '@apache-superset/core/utils'; import { useDispatch, useSelector } from 'react-redux'; import { isEqual, isEqualWith } from 'lodash'; import { getChartDataRequest } from 'src/components/Chart/chartAction'; @@ -217,7 +218,15 @@ const FilterValue: FC = ({ useEffect(() => { if (datasetId != null) { - dispatch(fetchDatasourceMetadata(`${datasetId}__table`)); + // Best-effort prime of the datasource cache for the granularity_sqla + // fallback. A failure here is non-fatal — the filter still works, just + // without the time-range cascade fallback — so handle the rejection + // explicitly to avoid an unhandled promise rejection. + Promise.resolve( + dispatch(fetchDatasourceMetadata(`${datasetId}__table`)), + ).catch(error => + logging.warn('Failed to fetch filter datasource metadata', error), + ); } }, [datasetId, dispatch]); From 9130ab39cb92947fcf633f8b67a6e83b2c2ffd65 Mon Sep 17 00:00:00 2001 From: Jordan Jenkins Date: Thu, 25 Jun 2026 10:26:52 -0500 Subject: [PATCH 5/5] fix(native-filters): share in-flight metadata promise across callers Address review feedback on the dedup guard. The Set-based guard returned `undefined` to concurrent duplicate callers, which breaks the thunk contract: a caller awaiting the dispatch (e.g. the Promise.all over addChart + fetchDatasourceMetadata in addSliceToDashboard) would resolve early without the metadata and could not observe a rejection. Replace the Set with a per-key Map of in-flight promises and return the same promise to every concurrent caller, so completion and errors propagate consistently. Cleanup moves to `.finally` (runs on both success and failure). Add tests asserting concurrent callers share one promise and that a rejection propagates to all of them. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../src/dashboard/actions/datasources.test.ts | 31 ++++++++++++++++--- .../src/dashboard/actions/datasources.ts | 27 +++++++++------- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/superset-frontend/src/dashboard/actions/datasources.test.ts b/superset-frontend/src/dashboard/actions/datasources.test.ts index ce7e7c50caa7..545bf17a2e1b 100644 --- a/superset-frontend/src/dashboard/actions/datasources.test.ts +++ b/superset-frontend/src/dashboard/actions/datasources.test.ts @@ -74,7 +74,7 @@ test('fetchDatasourceMetadata fetches and caches an uncached datasource', async ); }); -test('fetchDatasourceMetadata deduplicates concurrent in-flight requests for the same key', async () => { +test('fetchDatasourceMetadata shares one in-flight promise across concurrent callers', async () => { let resolveGet: (value: ClientResponse) => void = () => {}; const getSpy = jest.spyOn(SupersetClient, 'get').mockReturnValue( new Promise(resolve => { @@ -86,13 +86,36 @@ test('fetchDatasourceMetadata deduplicates concurrent in-flight requests for the // Two filters using the same dataset mount and dispatch before the first // request resolves and populates the cache. - fetchDatasourceMetadata(KEY)(dispatch, getState); - fetchDatasourceMetadata(KEY)(dispatch, getState); + const first = fetchDatasourceMetadata(KEY)(dispatch, getState); + const second = fetchDatasourceMetadata(KEY)(dispatch, getState); + // Only one network request fires, and both callers receive the same promise + // so each observes the same completion/rejection. expect(getSpy).toHaveBeenCalledTimes(1); + expect(second).toBe(first); resolveGet(jsonResponse({ uid: KEY, main_dttm_col: 'ds' })); - await Promise.resolve(); + await Promise.all([first, second]); + + expect(dispatch).toHaveBeenCalledWith( + expect.objectContaining({ + type: DatasourcesAction.SetDatasource, + key: KEY, + }), + ); +}); + +test('fetchDatasourceMetadata propagates a rejection to all concurrent callers', async () => { + jest.spyOn(SupersetClient, 'get').mockRejectedValue(new Error('boom')); + const dispatch = jest.fn(); + const getState = buildGetState(); + + const first = fetchDatasourceMetadata(KEY)(dispatch, getState); + const second = fetchDatasourceMetadata(KEY)(dispatch, getState); + + expect(second).toBe(first); + await expect(first).rejects.toThrow('boom'); + await expect(second).rejects.toThrow('boom'); }); test('fetchDatasourceMetadata clears the in-flight key after a failed request so it can retry', async () => { diff --git a/superset-frontend/src/dashboard/actions/datasources.ts b/superset-frontend/src/dashboard/actions/datasources.ts index 1745bcbb5cc6..34b3b0fe351d 100644 --- a/superset-frontend/src/dashboard/actions/datasources.ts +++ b/superset-frontend/src/dashboard/actions/datasources.ts @@ -52,7 +52,7 @@ export function setDatasource(datasource: Datasource, key: string) { }; } -const inFlightDatasourceKeys = new Set(); +const inFlightDatasourceRequests = new Map>(); export function fetchDatasourceMetadata(key: string) { return (dispatch: Dispatch, getState: () => RootState) => { @@ -63,21 +63,24 @@ export function fetchDatasourceMetadata(key: string) { return dispatch(setDatasource(datasource, key)); } - if (inFlightDatasourceKeys.has(key)) { - return undefined; + // Share a single in-flight promise per key across concurrent callers, so + // request deduplication preserves the thunk contract: every awaiter (e.g. + // a Promise.all over multiple dispatches) observes the same completion or + // rejection instead of resolving early on an undefined return. + const inFlight = inFlightDatasourceRequests.get(key); + if (inFlight) { + return inFlight; } - inFlightDatasourceKeys.add(key); - return SupersetClient.get({ + const request = SupersetClient.get({ endpoint: `/superset/fetch_datasource_metadata?datasourceKey=${key}`, }) - .then(({ json }) => { - inFlightDatasourceKeys.delete(key); - return dispatch(setDatasource(json as Datasource, key)); - }) - .catch((err: unknown) => { - inFlightDatasourceKeys.delete(key); - throw err; + .then(({ json }) => dispatch(setDatasource(json as Datasource, key))) + .finally(() => { + inFlightDatasourceRequests.delete(key); }); + + inFlightDatasourceRequests.set(key, request); + return request; }; }