forked from jellyfin-archive/jellyfin-desktop-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnectivityHelper.js
More file actions
94 lines (81 loc) · 2.93 KB
/
connectivityHelper.js
File metadata and controls
94 lines (81 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
window.jmpCheckServerConnectivity = (() => {
let activeController = null;
const checkFunc = async function(url) {
// Abort any in-progress check
if (activeController) {
activeController.abort();
}
// Wait for API
let attempts = 0;
while (!window.api && attempts < 50) {
await new Promise(resolve => setTimeout(resolve, 100));
attempts++;
}
if (!window.api) {
throw new Error('WebChannel not available');
}
// Create abort controller for this check
const controller = new AbortController();
activeController = controller;
return new Promise((resolve, reject) => {
// Handle abort
controller.signal.addEventListener('abort', () => {
if (handler) {
window.api.system.serverConnectivityResult.disconnect(handler);
}
reject(new Error('Connection cancelled'));
});
let handler = (resultUrl, success, resolvedUrl) => {
if (resultUrl === url && !controller.signal.aborted) {
window.api.system.serverConnectivityResult.disconnect(handler);
handler = null;
if (activeController === controller) {
activeController = null;
}
if (success) {
resolve(resolvedUrl);
} else {
reject(new Error('Connection failed'));
}
}
};
window.api.system.serverConnectivityResult.connect(handler);
window.api.system.checkServerConnectivity(url);
});
};
// Expose abort function for cancellation
checkFunc.abort = () => {
if (activeController) {
activeController.abort();
activeController = null;
}
};
return checkFunc;
})();
window.jmpFetchPage = (() => {
let fetchInProgress = false;
return async function(url) {
if (fetchInProgress) {
throw new Error('Page fetch already in progress');
}
// Wait for API
let attempts = 0;
while (!window.api && attempts < 50) {
await new Promise(resolve => setTimeout(resolve, 100));
attempts++;
}
if (!window.api) {
throw new Error('WebChannel not available');
}
fetchInProgress = true;
return new Promise((resolve, reject) => {
const handler = (html, finalUrl, hadCSP) => {
window.api.system.pageContentReady.disconnect(handler);
fetchInProgress = false;
resolve({ html, finalUrl, hadCSP });
};
window.api.system.pageContentReady.connect(handler);
window.api.system.fetchPageForCSPWorkaround(url);
});
};
})();