Skip to content

Commit

Permalink
Prevented foobar crash when exiting during Biography's AllMusic fetch…
Browse files Browse the repository at this point in the history
…ing - thx @regorxxx =)

Co-authored-by: regorxxx <regorxxx@users.noreply.github.com>
  • Loading branch information
TT-ReBORN and regorxxx committed May 20, 2024
1 parent d17d6fd commit 7032ff7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
3 changes: 2 additions & 1 deletion profile/georgia-reborn/scripts/Base/gr-callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// * Website: https://github.com/TT-ReBORN/Georgia-ReBORN * //
// * Version: 3.0-DEV * //
// * Dev. started: 22-12-2017 * //
// * Last change: 05-05-2024 * //
// * Last change: 20-05-2024 * //
/////////////////////////////////////////////////////////////////////////////////


Expand Down Expand Up @@ -1654,6 +1654,7 @@ function on_playlists_changed() {
*/
function on_script_unload() {
console.log('Unloading Script');
window.unloading = true;
grm.waveBar.on_script_unload();

// It appears we don't need to dispose the images which we loaded using gdi.Image in their declaration for some reason. Attempting to dispose them causes a script error.
Expand Down
2 changes: 1 addition & 1 deletion profile/georgia-reborn/scripts/Biography/bio-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// * Website: https://github.com/TT-ReBORN/Georgia-ReBORN * //
// * Version: 1.4.2 * //
// * Dev. started: 18-10-2016 * //
// * Last change: 13-12-2023 (Mod change 05-05-2024) * //
// * Last change: 13-12-2023 (Mod change 20-05-2024) * //
/////////////////////////////////////////////////////////////////////////////////


Expand Down
33 changes: 26 additions & 7 deletions profile/georgia-reborn/scripts/Biography/scripts/bio-allmusic.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function bioOnStateChange(resolve, reject, func = null) { // credit regorxxx

// May be used to async run a func for the response or as promise
function bioSend({ method = 'GET', URL, body = void (0), func = null, requestHeader = [/*[header, type]*/], bypassCache = false, timeout = 5000 }) { // credit regorxxx
return new Promise(async (resolve, reject) => {
return new Promise((resolve, reject) => {
const xmlhttp = new ActiveXObject('WinHttp.WinHttpRequest.5.1');
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#bypassing_the_cache
// Add ('&' + new Date().getTime()) to URLS to avoid caching
Expand All @@ -20,18 +20,25 @@ function bioSend({ method = 'GET', URL, body = void (0), func = null, requestHea
URL + (bypassCache ? (/\?/.test(URL) ? '&' : '?') + new Date().getTime() : ''),
true
);

requestHeader.forEach((pair) => {
if (!pair[0] || !pair[1]) { console.log(`HTTP Headers missing: ${pair}`); return; }
if (!pair[0] || !pair[1]) {
console.log(`HTTP Headers missing: ${pair}`);
return;
}
xmlhttp.SetRequestHeader(...pair);
});

if (bypassCache) {
xmlhttp.SetRequestHeader('Cache-Control', 'private');
xmlhttp.SetRequestHeader('Pragma', 'no-cache');
xmlhttp.SetRequestHeader('cache', 'no-store');
xmlhttp.SetRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');
}

xmlhttp.SetTimeouts(timeout, timeout, timeout, timeout);
xmlhttp.Send(method === 'POST' ? body : void (0));

// Add a timer for timeout
const timer = setTimeout(() => {
clearInterval(checkResponse);
Expand All @@ -43,15 +50,27 @@ function bioSend({ method = 'GET', URL, body = void (0), func = null, requestHea
if (e.message.indexOf('0x80072ee7') !== -1) { status = 400; } // No network
else if (e.message.indexOf('0x80072ee2') !== -1) { status = 408; } // No response
else if (e.message.indexOf('0x8000000a') !== -1) { status = 408; } // Not finished response
xmlhttp.Abort(); return reject({ status, responseText: e.message });
xmlhttp.Abort();
return reject({ status, responseText: e.message });
}
}, timeout);

// Check for response periodically to not block the UI
const checkResponse = setInterval(() => {
try { xmlhttp.Status && xmlhttp.ResponseText } catch (e) { return; }
clearTimeout(timer);
clearInterval(checkResponse);
bioOnStateChange.call(xmlhttp, resolve, reject, func);
if (window.unloading) {
xmlhttp.Abort();
clearTimeout(timer);
clearInterval(checkResponse);
bioOnStateChange.call(null, resolve, reject, () => void (0));
} else {
let responseComplete;
try { responseComplete = xmlhttp.Status && xmlhttp.ResponseText; } catch (e) {}
if (responseComplete) {
clearTimeout(timer);
clearInterval(checkResponse);
bioOnStateChange.call(xmlhttp, resolve, reject, func);
}
}
}, 30);
});
}
Expand Down

0 comments on commit 7032ff7

Please sign in to comment.