| 1 |
/** |
| 2 |
* VersionChecker polls the /v1/config endpoint to detect new server versions |
| 3 |
* or configuration changes, prompting users to refresh the page. |
| 4 |
*/ |
| 5 |
|
| 6 |
const intervalMillis = 5 * 60 * 1000; // 5 minutes |
| 7 |
|
| 8 |
class VersionChecker { |
| 9 |
constructor() { |
| 10 |
this.initialConfigHash = null; |
| 11 |
this.listener = null; |
| 12 |
this.timer = null; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Starts the version checker worker. It stores the initial config hash |
| 17 |
* from the config.js and polls the server every 5 minutes. |
| 18 |
*/ |
| 19 |
startWorker() { |
| 20 |
// Store initial config hash from the config loaded at page load |
| 21 |
this.initialConfigHash = window.config?.config_hash || ""; |
| 22 |
console.log("[VersionChecker] Starting version checker"); |
| 23 |
this.timer = setInterval(() => this.checkVersion(), intervalMillis); |
| 24 |
} |
| 25 |
|
| 26 |
stopWorker() { |
| 27 |
if (this.timer) { |
| 28 |
clearInterval(this.timer); |
| 29 |
this.timer = null; |
| 30 |
} |
| 31 |
console.log("[VersionChecker] Stopped version checker"); |
| 32 |
} |
| 33 |
|
| 34 |
registerListener(listener) { |
| 35 |
this.listener = listener; |
| 36 |
} |
| 37 |
|
| 38 |
resetListener() { |
| 39 |
this.listener = null; |
| 40 |
} |
| 41 |
|
| 42 |
async checkVersion() { |
| 43 |
if (!this.initialConfigHash) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
try { |
| 48 |
const response = await fetch(`${window.config?.base_url || ""}/v1/config`); |
| 49 |
if (!response.ok) { |
| 50 |
console.log("[VersionChecker] Failed to fetch config:", response.status); |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
const data = await response.json(); |
| 55 |
const currentHash = data.config_hash; |
| 56 |
|
| 57 |
if (currentHash && currentHash !== this.initialConfigHash) { |
| 58 |
console.log("[VersionChecker] Version or config changed, showing banner"); |
| 59 |
if (this.listener) { |
| 60 |
this.listener(); |
| 61 |
} |
| 62 |
} else { |
| 63 |
console.log("[VersionChecker] No version change detected"); |
| 64 |
} |
| 65 |
} catch (error) { |
| 66 |
console.log("[VersionChecker] Error checking config:", error); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
const versionChecker = new VersionChecker(); |
| 72 |
export default versionChecker; |