From ae0bc8e101063b89d1d4d53e4d2250ab0038f6ac Mon Sep 17 00:00:00 2001 From: Saqib Ansari Date: Wed, 20 Nov 2024 12:24:30 +0530 Subject: [PATCH] fix: reduce redundant fetch request --- frontend/src/dashboard/useDashboard.js | 1 + frontend/src/widgets/useChartData.js | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/frontend/src/dashboard/useDashboard.js b/frontend/src/dashboard/useDashboard.js index b64e3c1a3..fed634110 100644 --- a/frontend/src/dashboard/useDashboard.js +++ b/frontend/src/dashboard/useDashboard.js @@ -306,6 +306,7 @@ function getDashboardResource(name) { return createDocumentResource({ doctype: 'Insights Dashboard', name: name, + auto: false, whitelistedMethods: { fetch_chart_data: 'fetch_chart_data', clear_charts_cache: 'clear_charts_cache', diff --git a/frontend/src/widgets/useChartData.js b/frontend/src/widgets/useChartData.js index fe00f7790..c81251b33 100644 --- a/frontend/src/widgets/useChartData.js +++ b/frontend/src/widgets/useChartData.js @@ -27,11 +27,14 @@ export default function useChartData(options = {}) { error: null, }) + let currentLoadPromise = null function load(query) { if (!query) return state.loading = true - return options - .resultsFetcher() + if (!currentLoadPromise) { + currentLoadPromise = options.resultsFetcher(query) + } + return currentLoadPromise .then((results) => { state.loading = false state.rawData = getFormattedResult(results) @@ -41,6 +44,9 @@ export default function useChartData(options = {}) { state.loading = false state.error = error }) + .finally(() => { + currentLoadPromise = null + }) } if (options.query) {