Skip to content

Commit f7452af

Browse files
author
Andrew Rabert
committed
Fix base URL determination
Makes it consistent with how jellyfin-web determines base URL
1 parent c0a64a9 commit f7452af

4 files changed

Lines changed: 253 additions & 100 deletions

File tree

native/connectivityHelper.js

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
window.jmpCheckServerConnectivity = (() => {
2-
let checkInProgress = false;
2+
let activeController = null;
33

4-
return async function(url) {
5-
if (checkInProgress) {
6-
throw new Error('Connectivity check already in progress');
4+
const checkFunc = async function(url) {
5+
// Abort any in-progress check
6+
if (activeController) {
7+
activeController.abort();
78
}
89

910
// Wait for API
@@ -16,24 +17,48 @@ window.jmpCheckServerConnectivity = (() => {
1617
throw new Error('WebChannel not available');
1718
}
1819

19-
checkInProgress = true;
20+
// Create abort controller for this check
21+
const controller = new AbortController();
22+
activeController = controller;
2023

2124
return new Promise((resolve, reject) => {
22-
const handler = (resultUrl, success) => {
23-
if (resultUrl === url) {
25+
// Handle abort
26+
controller.signal.addEventListener('abort', () => {
27+
if (handler) {
2428
window.api.system.serverConnectivityResult.disconnect(handler);
25-
checkInProgress = false;
29+
}
30+
reject(new Error('Connection cancelled'));
31+
});
32+
33+
let handler = (resultUrl, success, resolvedUrl) => {
34+
if (resultUrl === url && !controller.signal.aborted) {
35+
window.api.system.serverConnectivityResult.disconnect(handler);
36+
handler = null;
37+
if (activeController === controller) {
38+
activeController = null;
39+
}
2640
if (success) {
27-
resolve();
41+
resolve(resolvedUrl);
2842
} else {
2943
reject(new Error('Connection failed'));
3044
}
3145
}
3246
};
47+
3348
window.api.system.serverConnectivityResult.connect(handler);
3449
window.api.system.checkServerConnectivity(url);
3550
});
3651
};
52+
53+
// Expose abort function for cancellation
54+
checkFunc.abort = () => {
55+
if (activeController) {
56+
activeController.abort();
57+
activeController = null;
58+
}
59+
};
60+
61+
return checkFunc;
3762
})();
3863

3964
window.jmpFetchPage = (() => {

native/find-webclient.js

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ async function tryConnect(server) {
33
if (!server.startsWith("http")) {
44
server = "http://" + server;
55
}
6-
serverBaseURL = server.replace(/\/+$/, "");
76

87
console.log("Checking connectivity to:", server);
98

10-
await window.jmpCheckServerConnectivity(server);
9+
const resolvedUrl = await window.jmpCheckServerConnectivity(server);
1110
console.log("Server connectivity check passed");
11+
console.log("Resolved URL:", resolvedUrl);
12+
13+
// Save original URL but navigate to fully-resolved redirect
1214
window.jmpInfo.settings.main.userWebClient = server;
13-
window.location = server;
15+
16+
// Navigation will clean up handlers, but do it explicitly
17+
window.location = resolvedUrl;
1418

1519
return true;
1620
} catch (e) {
@@ -54,16 +58,8 @@ const startConnecting = async () => {
5458
button.style.visibility = 'hidden';
5559
document.addEventListener('keydown', cancelOnEscape);
5660

57-
let connected = false;
58-
59-
while (!connected && isConnecting) {
60-
connected = await tryConnect(server);
61-
62-
if (!connected && isConnecting) {
63-
// Wait 5 seconds before retrying
64-
await new Promise(resolve => setTimeout(resolve, 5000));
65-
}
66-
}
61+
// C++ handles retries, just wait for result
62+
const connected = await tryConnect(server);
6763

6864
if (!connected) {
6965
isConnecting = false;
@@ -82,8 +78,17 @@ const startConnecting = async () => {
8278
const cancelConnection = () => {
8379
if (!isConnecting) return;
8480

81+
console.log("Cancelling connection");
8582
isConnecting = false;
8683

84+
// Cancel C++ connectivity check and abort JS promise
85+
if (window.api && window.api.system) {
86+
window.api.system.cancelServerConnectivity();
87+
}
88+
if (window.jmpCheckServerConnectivity.abort) {
89+
window.jmpCheckServerConnectivity.abort();
90+
}
91+
8792
const address = document.getElementById('address');
8893
const title = document.getElementById('title');
8994
const spinner = document.getElementById('spinner');
@@ -160,16 +165,8 @@ document.addEventListener('keydown', (e) => {
160165
button.style.visibility = 'hidden';
161166
document.addEventListener('keydown', cancelOnEscape);
162167

163-
let connected = false;
164-
165-
while (!connected && isConnecting) {
166-
connected = await tryConnect(savedServer);
167-
168-
if (!connected && isConnecting) {
169-
// Wait 5 seconds before retrying
170-
await new Promise(resolve => setTimeout(resolve, 5000));
171-
}
172-
}
168+
// C++ handles retries, just wait for result
169+
const connected = await tryConnect(savedServer);
173170

174171
if (!connected) {
175172
// User cancelled or error - show UI

0 commit comments

Comments
 (0)