Skip to content

Commit 61bc204

Browse files
committed
Update initial instance load
1 parent 4f770bf commit 61bc204

File tree

4 files changed

+82
-60
lines changed

4 files changed

+82
-60
lines changed

assets/webconfig/js/content_index.js

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -134,43 +134,14 @@ $(document).ready(function () {
134134

135135
$(window.hyperion).on("cmd-config-getconfig", function (event) {
136136

137-
// Function to reverse the transformed config into the legacy format
138-
function reverseTransformConfig(serverConfig, instanceId) {
139-
const { global, instances } = serverConfig;
140-
141-
// Initialize the resulting legacy config
142-
const legacyConfig = {};
143-
144-
// Add global settings to the legacy config
145-
if (global?.settings) {
146-
Object.assign(legacyConfig, global.settings);
147-
}
148-
149-
// Find the instance with the matching id and add its settings
150-
const instance = instances?.find(inst => inst.id === instanceId);
151-
if (instance?.settings) {
152-
Object.assign(legacyConfig, instance.settings);
153-
}
154-
155-
return legacyConfig;
156-
}
157-
158137
let instanceId = window.currentHyperionInstance;
159138
const config = event.response.info;
160139
const { instanceIds } = config;
161140

162141
if (Array.isArray(instanceIds) && instanceIds.length !== 0) {
163-
if (!instanceIds.includes(window.currentHyperionInstance)) {
164-
// If instanceID is not valid try to switch to the first enabled or fall back to the first instance configured
165-
const { instances } = config;
166-
167-
const firstEnabledInstanceId = instances.find((instance) => instance.enabled)?.id;
168-
if (firstEnabledInstanceId) {
169-
instanceId = firstEnabledInstanceId;
170-
instanceSwitch(instanceId);
171-
} else {
172-
instanceId = window.currentHyperionInstance = instanceIds[0];
173-
}
142+
if (!instanceIds.includes(instanceId)) {
143+
// Will be switching and triggering a new event soon — skip rest
144+
return;
174145
}
175146
}
176147

@@ -354,23 +325,66 @@ $(document).ready(function () {
354325

355326
async function getServerInformation(instance) {
356327
try {
357-
// Step 1: Request server config
358-
await requestServerConfig.sync([], [instance], []);
328+
// Step 1: Start waiting for the config-getconfig event before sending the request
329+
const configEventPromise = new Promise((resolve) => {
330+
const handler = function(event) {
331+
$(window.hyperion).off("cmd-config-getconfig", handler);
332+
resolve(event);
333+
};
334+
$(window.hyperion).on("cmd-config-getconfig", handler);
335+
});
336+
337+
// Step 2: Send config request
338+
const configResponsePromise = requestServerConfig.async([], [instance], []);
339+
340+
// Step 3: Wait for both response and event
341+
const [configResponse, configEvent] = await Promise.all([
342+
configResponsePromise,
343+
configEventPromise
344+
]);
345+
346+
const config = configEvent.response.info;
347+
let instanceId = instance;
348+
const { instanceIds, instances } = config;
349+
350+
// Step 4: Validate instance
351+
if (!instanceIds.includes(instance)) {
352+
// Choose a fallback: enabled instance or first
353+
const fallback = instances.find((inst) => inst.enabled)?.id || instanceIds[0];
354+
if (fallback !== undefined) {
355+
instanceId = fallback;
356+
window.currentHyperionInstance = fallback;
357+
setStorage('lastSelectedInstance', fallback);
358+
} else {
359+
console.warn("No valid instance found");
360+
instanceId = [];
361+
}
362+
}
359363

360-
// Step 2: Request server info
361-
await requestServerInfo(instance);
364+
// Step 5: Update server config for the (corrected) instance
365+
const legacyConfig = reverseTransformConfig(config, instanceId);
366+
window.serverConfig = legacyConfig;
367+
window.showOptHelp = window.serverConfig.general?.showOptHelp;
368+
$(window.hyperion).trigger("serverConfig_updated");
369+
370+
// Step 6: Load additional info
371+
await requestServerInfo(instanceId);
362372

363-
// Step 3: Request token info
364373
await requestTokenInfo();
365374

366375
} catch (error) {
367-
console.error("An error occurred during getting server information:", error);
376+
console.error("Error in getServerInformation:", error);
368377
}
369378
}
370379

371380
$(window.hyperion).on("cmd-instance-switchTo", function (event) {
381+
const newInstance = window.currentHyperionInstance;
372382

373-
getServerInformation(window.currentHyperionInstance);
383+
if (typeof newInstance === "number" && !isNaN(newInstance)) {
384+
getServerInformation(newInstance);
385+
} else {
386+
console.warn("Invalid instance ID during switch");
387+
}
374388
});
375389

376390
$(window.hyperion).on("cmd-effects-update", function (event) {

assets/webconfig/js/hyperion.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,10 @@ function requestServerInfo(instance = null) {
344344
];
345345

346346
const data = { subscribe: subscriptions };
347-
348347
const targetInstance = instance !== null ? Number(instance) : null;
349348

350-
return sendToHyperion("serverinfo", "getInfo", data, targetInstance);
349+
sendToHyperion("serverinfo", "getInfo", data, targetInstance);
350+
return Promise.resolve();
351351
}
352352

353353
function requestSysInfo() {

assets/webconfig/js/ui_utils.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,3 +1570,23 @@ function loadScript(src, callback, ...params) {
15701570
document.head.appendChild(script);
15711571
}
15721572

1573+
// Function to reverse the transformed config into the legacy format
1574+
function reverseTransformConfig(serverConfig, instanceId) {
1575+
const { global, instances } = serverConfig;
1576+
1577+
// Initialize the resulting legacy config
1578+
const legacyConfig = {};
1579+
1580+
// Add global settings to the legacy config
1581+
if (global?.settings) {
1582+
Object.assign(legacyConfig, global.settings);
1583+
}
1584+
1585+
// Find the instance with the matching id and add its settings
1586+
const instance = instances?.find(inst => inst.id === instanceId);
1587+
if (instance?.settings) {
1588+
Object.assign(legacyConfig, instance.settings);
1589+
}
1590+
1591+
return legacyConfig;
1592+
}

libsrc/api/JsonAPI.cpp

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,7 +1527,7 @@ void JsonAPI::handleInstanceCommand(const QJsonObject &message, const JsonApiCom
15271527

15281528
QJsonValue const instanceValue = message["instance"];
15291529

1530-
quint8 instanceID = static_cast<quint8>(instanceValue.toInt());
1530+
const quint8 instanceID = static_cast<quint8>(instanceValue.toInt());
15311531
if(cmd.subCommand != SubCommand::CreateInstance)
15321532
{
15331533
QString errorText;
@@ -1538,24 +1538,12 @@ void JsonAPI::handleInstanceCommand(const QJsonObject &message, const JsonApiCom
15381538
} else if (!_instanceManager->doesInstanceExist(instanceID))
15391539
{
15401540
errorText = QString("Hyperion instance [%1] does not exist.").arg(instanceID);
1541-
Error(_log, "%s", QSTRING_CSTR(errorText));
1542-
1543-
if (cmd.subCommand != SubCommand::SwitchTo)
1544-
{
1545-
sendErrorReply(errorText, cmd);
1546-
return;
1547-
}
1548-
1549-
//Use first running instance (backward compatability)
1550-
quint8 const firstRunningInstanceID = _instanceManager->getFirstRunningInstanceIdx();
1551-
if (firstRunningInstanceID == NO_INSTANCE_ID)
1552-
{
1553-
sendErrorReply(errorText, cmd);
1554-
return;
1555-
}
1541+
}
15561542

1557-
errorDetails.append(QString("Hyperion instance [%1] does not exist, using first running instance [%2]").arg(instanceID).arg(firstRunningInstanceID));
1558-
instanceID = firstRunningInstanceID;
1543+
if (!errorText.isEmpty())
1544+
{
1545+
sendErrorReply( errorText, cmd);
1546+
return;
15591547
}
15601548
}
15611549

@@ -1567,7 +1555,7 @@ void JsonAPI::handleInstanceCommand(const QJsonObject &message, const JsonApiCom
15671555
if (handleInstanceSwitch(instanceID))
15681556
{
15691557
QJsonObject const response { { "instance", instanceID } };
1570-
sendSuccessDataReplyWithError(response, cmd, errorDetails);
1558+
sendSuccessDataReply(response, cmd);
15711559
}
15721560
else
15731561
{

0 commit comments

Comments
 (0)