返回提交历史
Modified
src/controllers/custom/configController.ts
+1
-1
Modified
static/webui/script.js
+51
-37
XFEstudio/XFESpaceNinjaServer
chore(webui): cache server config & admin state (#3883)
Closes #3871 Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/3883 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>
247eddb5
代码差异
2 个文件
+52
-38
@@ -15,7 +15,7 @@ export const getConfigController: RequestHandler = async (req, res) => {
15
15
}
16
16
res.json(responseData);
17
17
} else {
18
res.status(401).end();
18
res.json(null);
19
19
}
20
20
};
21
21
@@ -93,10 +93,7 @@ function openWebSocket() {
93
93
if (ws_reconnect) {
94
94
ws_reconnect = false;
95
95
// Anything could've happened during the time we were disconnected and not getting events.
96
if (!refreshServerConfig()) {
97
updateInventory();
98
updateGuild();
99
}
96
single.loadRoute(single.getCurrentPath());
100
97
}
101
98
}
102
99
if ("permissions" in msg) {
@@ -164,6 +161,7 @@ function openWebSocket() {
164
161
window.ws.onclose = function () {
165
162
ws_is_open = false;
166
163
ws_reconnect = true;
164
invalidateCachedData();
167
165
window.subscribedToGuildId = undefined;
168
166
setTimeout(openWebSocket, 3000);
169
167
};
@@ -171,18 +169,23 @@ function openWebSocket() {
171
169
openWebSocket();
172
170
173
171
function refreshServerConfig() {
174
//window.is_admin = undefined;
172
window.is_admin = undefined;
173
config_data = undefined;
175
174
if (single.getCurrentPath() == "/webui/cheats" || single.getCurrentPath() == "/webui/admin") {
176
175
single.loadRoute(single.getCurrentPath());
177
return true;
178
176
}
179
return false;
180
177
}
181
178
182
function doAccountSwitch(to_route) {
183
window.authz = undefined;
179
function invalidateCachedData() {
184
180
inventory_data = undefined;
185
181
guild_data = undefined;
182
window.is_admin = undefined;
183
config_data = undefined;
184
}
185
186
function doAccountSwitch(to_route) {
187
window.authz = undefined;
188
invalidateCachedData();
186
189
single.loadRoute(to_route);
187
190
sendAuth();
188
191
}
@@ -3429,6 +3432,31 @@ function doSaveConfigStringArray(id) {
3429
3432
});
3430
3433
}
3431
3434
3435
let config_data;
3436
// Assumes that caller revalidates authz
3437
function getServerConfig() {
3438
return new Promise((resolve, reject) => {
3439
if (window.is_admin === false) {
3440
config_data = null;
3441
}
3442
if (config_data !== undefined) {
3443
resolve(config_data);
3444
} else {
3445
$.post({
3446
url: "/custom/getConfig?" + window.authz,
3447
contentType: "application/json",
3448
data: JSON.stringify(uiConfigs)
3449
})
3450
.done(json => {
3451
window.is_admin = !!json;
3452
config_data = json;
3453
resolve(config_data);
3454
})
3455
.fail(res => reject(res.responseText));
3456
}
3457
});
3458
}
3459
3432
3460
single.getRoute("/webui/cheats").on("beforeload", function () {
3433
3461
awaitAuthz().then(() => {
3434
3462
getInventoryData().then(data => {
@@ -3441,14 +3469,8 @@ single.getRoute("/webui/cheats").on("beforeload", function () {
3441
3469
}
3442
3470
}
3443
3471
});
3444
3445
$.post({
3446
url: "/custom/getConfig?" + window.authz,
3447
contentType: "application/json",
3448
data: JSON.stringify(uiConfigs)
3449
})
3450
.done(json => {
3451
//window.is_admin = true;
3472
getServerConfig().then(json => {
3473
if (json) {
3452
3474
$(".admin-hide").addClass("d-none");
3453
3475
$(".admin-show").removeClass("d-none");
3454
3476
Object.entries(json).forEach(entry => {
@@ -3463,24 +3485,11 @@ single.getRoute("/webui/cheats").on("beforeload", function () {
3463
3485
elm.value = value ?? elm.getAttribute("data-default");
3464
3486
}
3465
3487
});
3466
})
3467
.fail(res => {
3468
if (res.responseText == "Log-in expired") {
3469
if (ws_is_open && !auth_pending) {
3470
console.warn("Credentials invalidated but the server didn't let us know");
3471
sendAuth();
3472
}
3473
revalidateAuthz().then(() => {
3474
if (single.getCurrentPath() == "/webui/cheats") {
3475
single.loadRoute("/webui/cheats");
3476
}
3477
});
3478
} else {
3479
//window.is_admin = false;
3480
$(".admin-hide").removeClass("d-none");
3481
$(".admin-show").addClass("d-none");
3482
}
3483
});
3488
} else {
3489
$(".admin-hide").removeClass("d-none");
3490
$(".admin-show").addClass("d-none");
3491
}
3492
});
3484
3493
});
3485
3494
});
3486
3495
@@ -5600,9 +5609,14 @@ function removeItems(category) {
5600
5609
5601
5610
single.getRoute("/webui/admin").on("beforeload", function () {
5602
5611
awaitAuthz().then(() => {
5612
if (window.is_admin === false) {
5613
$(".admin-hide").removeClass("d-none");
5614
$(".admin-show").addClass("d-none");
5615
return;
5616
}
5603
5617
$.get("/custom/getRegisteredLosers?" + window.authz)
5604
5618
.done(users => {
5605
//window.is_admin = true;
5619
window.is_admin = true;
5606
5620
$(".admin-hide").addClass("d-none");
5607
5621
$(".admin-show").removeClass("d-none");
5608
5622
document.getElementById("registered-losers").innerHTML = "";
@@ -5648,7 +5662,7 @@ single.getRoute("/webui/admin").on("beforeload", function () {
5648
5662
}
5649
5663
});
5650
5664
} else {
5651
//window.is_admin = false;
5665
window.is_admin = false;
5652
5666
$(".admin-hide").removeClass("d-none");
5653
5667
$(".admin-show").addClass("d-none");
5654
5668
}