Skip to content

Commit 08ee4ad

Browse files
committed
Better first-load handling 🔄
1 parent 1a221fa commit 08ee4ad

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

front/js/pialert_common.js

+20-8
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ function cacheSettings()
155155
setCache(`pia_set_${set.Code_Name}`, set.Value)
156156
setCache(`pia_set_opt_${set.Code_Name}`, resolvedOptions)
157157
});
158-
}).then(() => handleSuccess('cacheSettings')).catch(() => handleFailure('cacheSettings')); // handle AJAX synchronization
158+
}).then(() => handleSuccess('cacheSettings')).catch(() => handleFailure('cacheSettings', cacheSettings)); // handle AJAX synchronization
159159
})
160160
}
161161

@@ -221,7 +221,7 @@ function cacheStrings()
221221
data.forEach((langString) => {
222222
setCache(`pia_lang_${langString.String_Key}_${langString.Language_Code}`, langString.String_Value)
223223
});
224-
}).then(() => handleSuccess('cacheStrings')).catch(() => handleFailure('cacheStrings')); // handle AJAX synchronization
224+
}).then(() => handleSuccess('cacheStrings')).catch(() => handleFailure('cacheStrings', cacheStrings)); // handle AJAX synchronization
225225

226226
}
227227

@@ -891,7 +891,7 @@ function initDeviceListAll_JSON()
891891
setCache('devicesListAll_JSON', devicesListAll_JSON_str)
892892

893893
// console.log(getCache('devicesListAll_JSON'))
894-
}).then(() => handleSuccess('initDeviceListAll_JSON')).catch(() => handleFailure('initDeviceListAll_JSON')); // handle AJAX synchronization
894+
}).then(() => handleSuccess('initDeviceListAll_JSON')).catch(() => handleFailure('initDeviceListAll_JSON', initDeviceListAll_JSON)); // handle AJAX synchronization
895895

896896
}
897897

@@ -1074,6 +1074,13 @@ function resolveParams(params, template) {
10741074
return template;
10751075
}
10761076

1077+
// -----------------------------------------------------------------------------
1078+
// check if two arrays contain same values even if out of order
1079+
function arraysContainSameValues(arr1, arr2) {
1080+
// Sort and stringify arrays, then compare
1081+
return JSON.stringify(arr1.slice().sort()) === JSON.stringify(arr2.slice().sort());
1082+
}
1083+
10771084
// -----------------------------------------------------------------------------
10781085
// initialize
10791086
// -----------------------------------------------------------------------------
@@ -1147,8 +1154,9 @@ function executeOnce() {
11471154

11481155
showSpinner()
11491156

1150-
// Counter to keep track of completed AJAX calls
1151-
completedCalls = 0;
1157+
// to keep track of completed AJAX calls
1158+
completedCalls = []
1159+
completedCalls_final = ['cacheSettings', 'cacheStrings', 'initDeviceListAll_JSON'];
11521160

11531161
// Your initialization code here
11541162
cacheSettings();
@@ -1161,22 +1169,26 @@ function executeOnce() {
11611169
// Function to handle successful completion of an AJAX call
11621170
const handleSuccess = (callName) => {
11631171
console.log(`AJAX call ${callName} successful`);
1164-
completedCalls++;
1172+
// store completed call
1173+
completedCalls.push(callName)
11651174
onAllCallsComplete();
11661175
};
11671176

11681177
// -----------------------------------------------------------------------------
11691178
// Function to handle failure of an AJAX call
1170-
const handleFailure = (callName) => {
1179+
const handleFailure = (callName, callback) => {
11711180
// Handle AJAX call failure here
11721181
console.error(`AJAX call ${callName} failed`);
1182+
1183+
// try until successful
1184+
callback()
11731185
};
11741186

11751187
// -----------------------------------------------------------------------------
11761188
// Function to execute when all AJAX calls have completed
11771189
const onAllCallsComplete = () => {
11781190
// Check if all three AJAX calls have completed
1179-
if (completedCalls === 3) {
1191+
if (arraysContainSameValues(completedCalls,completedCalls_final)) {
11801192

11811193
// Set the flag in sessionStorage to indicate that the code has been executed
11821194
// and save time when last time the page was initialized

front/php/templates/language/de_de.json

100644100755
File mode changed.

front/php/templates/language/es_es.json

100644100755
File mode changed.

0 commit comments

Comments
 (0)