| 3723 | | 3725 | |
| 3724 | const switchToProfile = async (profileId) => { | 3726 | const switchToProfile = async (profileId) => { |
| 3725 | const loaded = new Promise(resolve => eventSource.once(event_types.CONNECTION_PROFILE_LOADED, resolve)); | 3727 | const loaded = new Promise(resolve => eventSource.once(event_types.CONNECTION_PROFILE_LOADED, resolve)); |
| 3726 | const timeout = new Promise(resolve => setTimeout(resolve, 10000)); | | |
| 3727 | const index = Array.from(select.options).findIndex(o => o.value === profileId); | 3728 | const index = Array.from(select.options).findIndex(o => o.value === profileId); |
| 3728 | select.selectedIndex = index >= 0 ? index : 0; | 3729 | select.selectedIndex = index >= 0 ? index : 0; |
| 3729 | select.dispatchEvent(new Event('change')); | 3730 | select.dispatchEvent(new Event('change')); |
| 3730 | // Don't hang the image generation if the event never fires. | 3731 | // Wait for the profile's commands to finish applying (don't hang forever if the event never fires). |
| 3731 | await Promise.race([loaded, timeout]); | 3732 | await Promise.race([loaded, delay(10000)]); |
| | 3733 | // Applying a profile reconnects the API, which is asynchronous. Generating before the |
| | 3734 | // connection is re-established fails instantly, so wait for it to come back up |
| | 3735 | // (mirrors the built-in /profile command). rejectOnTimeout:false -> proceed anyway after the timeout. |
| | 3736 | await waitUntilCondition(() => online_status !== 'no_connection', 10000, 100, { rejectOnTimeout: false }); |
| 3732 | }; | 3737 | }; |
| 3733 | | 3738 | |
| 3734 | await switchToProfile(targetProfileId); | 3739 | await switchToProfile(targetProfileId); |