fix: serverInfo page shows data immediately on load
Eliminated redundant serverinfo RCON polling from the serverInfo page. It now subscribes to the header's serverinfo-update CustomEvent instead. Key changes: - app.js: cache last serverinfo in window._lastServerInfo and dispatch serverinfo-update event so pages can pick up data immediately - serverInfo.js: replaced setInterval-based polling with event listener plus immediate _lastServerInfo cache check, eliminating the initial blank page state and duplicate RCON requests Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -46,6 +46,10 @@
|
|||||||
document.getElementById('header-players').innerHTML = '<i class="bi bi-people-fill"></i> ' + formatNumber(data.Players) + '/' + formatNumber(data.MaxPlayers);
|
document.getElementById('header-players').innerHTML = '<i class="bi bi-people-fill"></i> ' + formatNumber(data.Players) + '/' + formatNumber(data.MaxPlayers);
|
||||||
document.getElementById('header-fps').innerHTML = '<i class="bi bi-speedometer2"></i> ' + formatNumber(data.Framerate) + 'fps';
|
document.getElementById('header-fps').innerHTML = '<i class="bi bi-speedometer2"></i> ' + formatNumber(data.Framerate) + 'fps';
|
||||||
document.getElementById('header-entities').innerHTML = '<i class="bi bi-boxes"></i> ' + formatNumber(data.EntityCount);
|
document.getElementById('header-entities').innerHTML = '<i class="bi bi-boxes"></i> ' + formatNumber(data.EntityCount);
|
||||||
|
|
||||||
|
// Cache for late-arriving pages (e.g. serverInfo) and broadcast to listeners
|
||||||
|
window._lastServerInfo = data;
|
||||||
|
document.dispatchEvent(new CustomEvent('serverinfo-update', { detail: data }));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+6
-19
@@ -45,13 +45,6 @@ function ServerInfoPage(params, container) {
|
|||||||
'</div>';
|
'</div>';
|
||||||
container.innerHTML = html;
|
container.innerHTML = html;
|
||||||
|
|
||||||
// Start data polling immediately — charts will render when ready
|
|
||||||
if (Rcon.isConnected()) startPolling();
|
|
||||||
else {
|
|
||||||
var _c = function () { startPolling(); Rcon.off('connected', _c); };
|
|
||||||
Rcon.on('connected', _c);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---- NVD3 Charts ----
|
// ---- NVD3 Charts ----
|
||||||
nv.addGraph(function () {
|
nv.addGraph(function () {
|
||||||
var perfChart = nv.models.multiChart()
|
var perfChart = nv.models.multiChart()
|
||||||
@@ -109,13 +102,12 @@ function ServerInfoPage(params, container) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// ---- Data polling ----
|
// ---- Data polling ----
|
||||||
var pollTimer = null;
|
// Subscribe to header's serverinfo updates (no redundant RCON request)
|
||||||
|
function onServerInfo(e) { updateData(e.detail); }
|
||||||
|
document.addEventListener('serverinfo-update', onServerInfo);
|
||||||
|
|
||||||
function refresh() {
|
// If header already has cached data, use it immediately
|
||||||
Rcon.request('serverinfo', function (msg) {
|
if (window._lastServerInfo) updateData(window._lastServerInfo);
|
||||||
updateData(JSON.parse(msg.Message));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function updateData(data) {
|
function updateData(data) {
|
||||||
// Guard: element may be gone if navigated away
|
// Guard: element may be gone if navigated away
|
||||||
@@ -185,13 +177,8 @@ function ServerInfoPage(params, container) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function startPolling() {
|
|
||||||
refresh();
|
|
||||||
pollTimer = setInterval(refresh, 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
return function cleanup() {
|
return function cleanup() {
|
||||||
if (pollTimer) clearInterval(pollTimer);
|
document.removeEventListener('serverinfo-update', onServerInfo);
|
||||||
window._perfChart = null;
|
window._perfChart = null;
|
||||||
window._netChart = null;
|
window._netChart = null;
|
||||||
window._playersChart = null;
|
window._playersChart = null;
|
||||||
|
|||||||
Reference in New Issue
Block a user