@@ -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 ) {
0 commit comments