Ensured that the initialization is run only once

This commit is contained in:
Thorsten Sommer 2024-09-01 19:42:26 +02:00
parent 13943d2fa3
commit 923ab54061
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -105,6 +105,8 @@ static DATA_DIRECTORY: OnceLock<String> = OnceLock::new();
static CONFIG_DIRECTORY: OnceLock<String> = OnceLock::new(); static CONFIG_DIRECTORY: OnceLock<String> = OnceLock::new();
static DOTNET_INITIALIZED: Lazy<Mutex<bool>> = Lazy::new(|| Mutex::new(false));
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
@ -719,22 +721,25 @@ fn get_config_directory(_token: APIToken) -> String {
#[get("/system/dotnet/ready")] #[get("/system/dotnet/ready")]
async fn dotnet_ready(_token: APIToken) { async fn dotnet_ready(_token: APIToken) {
let main_window_spawn_clone = &MAIN_WINDOW;
let dotnet_server_port = *DOTNET_SERVER_PORT; // We create a manual scope for the lock to be released as soon as possible.
let url = match Url::parse(format!("http://localhost:{dotnet_server_port}").as_str()) // This is necessary because we cannot await any function while the lock is
// held.
{ {
Ok(url) => url, let mut initialized = DOTNET_INITIALIZED.lock().unwrap();
Err(msg) => { if *initialized {
error!("Error while parsing URL for navigating to the app: {msg}"); error!("Anyone tried to initialize the runtime twice. This is not intended.");
return; return;
} }
};
info!("The .NET server was booted successfully."); info!("The .NET server was booted successfully.");
*initialized = true;
}
// Try to get the main window. If it is not available yet, wait for it: // Try to get the main window. If it is not available yet, wait for it:
let mut main_window_ready = false; let mut main_window_ready = false;
let mut main_window_status_reported = false; let mut main_window_status_reported = false;
let main_window_spawn_clone = &MAIN_WINDOW;
while !main_window_ready while !main_window_ready
{ {
main_window_ready = { main_window_ready = {
@ -751,8 +756,18 @@ async fn dotnet_ready(_token: APIToken) {
time::sleep(Duration::from_millis(100)).await; time::sleep(Duration::from_millis(100)).await;
} }
} }
let main_window = main_window_spawn_clone.lock().unwrap(); let main_window = main_window_spawn_clone.lock().unwrap();
let dotnet_server_port = *DOTNET_SERVER_PORT;
let url = match Url::parse(format!("http://localhost:{dotnet_server_port}").as_str())
{
Ok(url) => url,
Err(msg) => {
error!("Error while parsing URL for navigating to the app: {msg}");
return;
}
};
let js_location_change = format!("window.location = '{url}';"); let js_location_change = format!("window.location = '{url}';");
let location_change_result = main_window.as_ref().unwrap().eval(js_location_change.as_str()); let location_change_result = main_window.as_ref().unwrap().eval(js_location_change.as_str());
match location_change_result { match location_change_result {