From d995d562ffe044aa60d95a637051fb2627630d33 Mon Sep 17 00:00:00 2001 From: difemaro Date: Wed, 30 Oct 2024 13:23:42 -0500 Subject: [PATCH] Update AutoRefresh.js --- .../AutoRefresh_seconds/AutoRefresh.js | 61 +++++++++++-------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/Extensiones/Samples/AutoRefresh_seconds/AutoRefresh.js b/Extensiones/Samples/AutoRefresh_seconds/AutoRefresh.js index 924d057..a99dff2 100644 --- a/Extensiones/Samples/AutoRefresh_seconds/AutoRefresh.js +++ b/Extensiones/Samples/AutoRefresh_seconds/AutoRefresh.js @@ -99,6 +99,8 @@ * by the user. This interval will refresh all selected datasources. */ + let uniqueDataSources = []; // Store unique data sources globally + function setupRefreshInterval(interval) { // Clear any existing timeout to prevent overlapping refreshes if (refreshInterval) { @@ -112,44 +114,49 @@ $('#nextrefresh').text(formattedTime); // Display the next refresh time } - // Function to refresh data sources and set the next refresh interval - function refreshDataSources() { - if (refreshInterval) { - clearTimeout(refreshInterval); - } - + // Function to collect unique data sources only once + function collectUniqueDataSources() { let dashboard = tableau.extensions.dashboardContent.dashboard; - let uniqueDataSources = new Set(); // Use a Set to store unique data sources + let collectedDataSources = new Set(); // Use a Set to store unique data sources - // Collect all data sources from each worksheet - let worksheetPromises = dashboard.worksheets.map((worksheet) => { - return worksheet.getDataSourcesAsync().then((datasources) => { + // Collect promises for retrieving data sources from each worksheet + let dataSourcePromises = dashboard.worksheets.map((worksheet) => + worksheet.getDataSourcesAsync().then((datasources) => { datasources.forEach((datasource) => { - if (activeDatasourceIdList.indexOf(datasource.id) >= 0) { - uniqueDataSources.add(datasource); // Add each unique datasource to the Set + if (activeDatasourceIdList.includes(datasource.id)) { + collectedDataSources.add(datasource); // Add each unique datasource to the Set } }); - }); + }) + ); + + // Wait until all data sources are gathered and set the uniqueDataSources variable + return Promise.all(dataSourcePromises).then(() => { + uniqueDataSources = Array.from(collectedDataSources); }); + } - // Wait until all worksheets have been processed - Promise.all(worksheetPromises).then(() => { - // Refresh each unique data source only once - const refreshPromises = Array.from(uniqueDataSources).map((datasource) => { - return datasource.refreshAsync(); - }); + // Function to refresh the previously collected unique data sources + function refreshDataSources() { + if (refreshInterval) { + clearTimeout(refreshInterval); + } + + // Refresh each unique data source + const refreshPromises = uniqueDataSources.map((datasource) => datasource.refreshAsync()); - // Wait until all selected unique data sources are refreshed - Promise.all(refreshPromises).then(() => { - updateNextRefreshTime(interval); - refreshInterval = setTimeout(refreshDataSources, interval * 1000); - }); + // Wait until all selected unique data sources are refreshed + Promise.all(refreshPromises).then(() => { + updateNextRefreshTime(interval); + refreshInterval = setTimeout(refreshDataSources, interval * 1000); // Schedule next refresh }); } - // Initial setup of next refresh time and start refreshing data sources - refreshDataSources(); - updateNextRefreshTime(interval); + // Initial collection of unique data sources and setup of next refresh time + collectUniqueDataSources().then(() => { + refreshDataSources(); // Start the refresh cycle after collecting data sources + updateNextRefreshTime(interval); + }); } /**