Konata fix for Unix

This commit is contained in:
Martin Kavík 2024-11-28 18:09:49 +01:00
parent ac484fdea8
commit fdaacaa6f3
3 changed files with 39 additions and 19 deletions

1
Cargo.lock generated
View file

@ -1701,6 +1701,7 @@ dependencies = [
"tauri-build",
"tauri-plugin-dialog",
"tauri-plugin-window-state",
"tokio",
"wasmtime",
"wasmtime-wasi",
"wellen",

View file

@ -26,6 +26,7 @@ tauri-plugin-dialog = "=2.0.0-beta.9"
once_cell = "1.19.0"
futures = "0.3.30"
reqwest = "0.12.9"
tokio = "*"
# wasmtime = "22.0.0"
# wasmtime-wasi = "22.0.0"

View file

@ -5,6 +5,7 @@ use std::sync::{Arc, RwLock as StdRwLock};
use std::time::Duration;
use tauri::{async_runtime::RwLock, AppHandle};
use tauri_plugin_dialog::DialogExt;
use tokio::time::sleep;
use wasmtime::AsContextMut;
use wellen::simple::Waveform;
@ -188,39 +189,56 @@ async fn open_konata_file(app: tauri::AppHandle) {
};
let file_path = file_response.path.into_os_string().into_string().unwrap();
spawn_konata_app();
let port = 30000;
let base_url = format!("http://localhost:{port}");
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.connect_timeout(Duration::from_secs(1))
.build()
.unwrap();
if client
.get(format!("{base_url}/status"))
.send()
.await
.is_ok()
{
let mut konata_server_ready = false;
let is_konata_server_ready = || async {
client
.post(format!("{base_url}/open-konata-file"))
.json(&serde_json::json!({
"file_path": file_path
}))
.get(format!("{base_url}/status"))
.send()
.await
.unwrap()
.error_for_status()
.unwrap();
.is_ok()
};
if is_konata_server_ready().await {
konata_server_ready = true;
} else {
println!("Failed to get Konata server status");
spawn_konata_app();
}
let mut attempts = 1;
while !konata_server_ready {
attempts += 1;
if attempts > 5 {
eprintln!("Failed to get Konata server status (5 attempts)");
return;
}
konata_server_ready = is_konata_server_ready().await;
sleep(Duration::from_secs(1)).await;
}
client
.post(format!("{base_url}/open-konata-file"))
.json(&serde_json::json!({
"file_path": file_path
}))
.send()
.await
.unwrap()
.error_for_status()
.unwrap();
}
#[cfg(target_family = "windows")]
fn spawn_konata_app() {
Command::new("cscript")
.current_dir("../../konata")
.current_dir("../../Konata")
.arg("konata.vbs")
.spawn()
.unwrap();
@ -229,7 +247,7 @@ fn spawn_konata_app() {
#[cfg(target_family = "unix")]
fn spawn_konata_app() {
Command::new("sh")
.current_dir("../../konata")
.current_dir("../../Konata")
.arg("konata.sh")
.spawn()
.unwrap();