Skip to content

Commit

Permalink
Update AutoRefresh.js
Browse files Browse the repository at this point in the history
  • Loading branch information
difemaro committed Oct 30, 2024
1 parent bea8961 commit d995d56
Showing 1 changed file with 34 additions and 27 deletions.
61 changes: 34 additions & 27 deletions Extensiones/Samples/AutoRefresh_seconds/AutoRefresh.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
});
}

/**
Expand Down

0 comments on commit d995d56

Please sign in to comment.