@@ -48,16 +48,66 @@ async function postRefreshInterval(newInterval) {
4848}
4949
5050// Fetch system data from a given API endpoint
51- async function fetchSystemData ( apiEndpoint ) {
51+ // Note: depcrecated in favor of fetchSystemDataWithRetries
52+
53+ // async function fetchSystemData(apiEndpoint) {
54+ // try {
55+ // const response = await fetch(apiEndpoint);
56+ // return await response.json();
57+ // } catch (error) {
58+ // console.error(`Error fetching ${apiEndpoint}:`, error);
59+ // return null;
60+ // }
61+ // }
62+
63+ async function fetchSystemDataWithRetries ( apiEndpoint , retries = 3 , delay = 2000 ) {
64+ for ( let i = 0 ; i < retries ; i ++ ) {
65+ try {
66+ const response = await fetch ( apiEndpoint ) ;
67+ if ( response . ok ) {
68+ return await response . json ( ) ;
69+ } else {
70+ const errorText = await response . text ( ) ;
71+ console . error ( `Error fetching ${ apiEndpoint } - Status: ${ response . status } , Response: ${ errorText } ` ) ;
72+ }
73+ } catch ( error ) {
74+ console . error ( `Fetch attempt ${ i + 1 } failed: ${ error . message } ` ) ;
75+ }
76+ if ( i < retries - 1 ) {
77+ await new Promise ( res => setTimeout ( res , delay ) ) ;
78+ }
79+ }
80+ return null ; // Return null after exhausting retries
81+ }
82+
83+
84+ let requestQueue = [ ] ;
85+ let isRequestInProgress = false ;
86+
87+ async function processQueue ( ) {
88+ if ( isRequestInProgress || requestQueue . length === 0 ) return ;
89+ isRequestInProgress = true ;
90+
91+ const { apiEndpoint, resolve, reject } = requestQueue . shift ( ) ;
5292 try {
53- const response = await fetch ( apiEndpoint ) ;
54- return await response . json ( ) ;
93+ const data = await fetchSystemDataWithRetries ( apiEndpoint ) ;
94+ resolve ( data ) ;
5595 } catch ( error ) {
56- console . error ( `Error fetching ${ apiEndpoint } :` , error ) ;
57- return null ;
96+ reject ( error ) ;
5897 }
98+
99+ isRequestInProgress = false ;
100+ processQueue ( ) ; // Continue processing the next request
59101}
60102
103+ function queueRequest ( apiEndpoint ) {
104+ return new Promise ( ( resolve , reject ) => {
105+ requestQueue . push ( { apiEndpoint, resolve, reject } ) ;
106+ processQueue ( ) ; // Start processing if not already in progress
107+ } ) ;
108+ }
109+
110+
61111// Update card content and bar width based on fetched data
62112function updateCard ( cardSelector , dataKey , data , unit = '' , barSelector = null , maxDataKey = null ) {
63113 const cardElement = document . querySelector ( cardSelector ) ;
@@ -109,7 +159,7 @@ async function refreshCardText(cardSelector, dataKey, data) {
109159
110160// Refresh all card data
111161async function refreshData ( ) {
112- const data = await fetchSystemData ( '/api/system-info' ) ;
162+ const data = await queueRequest ( '/api/system-info' ) ;
113163 if ( ! data ) return ;
114164
115165 updateCard ( '.bg-disk' , 'disk_percent' , data , '%' , '.disk-bar' ) ;
0 commit comments