Konata fix for Unix

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

1
Cargo.lock generated
View file

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

View file

@ -26,6 +26,7 @@ tauri-plugin-dialog = "=2.0.0-beta.9"
once_cell = "1.19.0" once_cell = "1.19.0"
futures = "0.3.30" futures = "0.3.30"
reqwest = "0.12.9" reqwest = "0.12.9"
tokio = "*"
# wasmtime = "22.0.0" # wasmtime = "22.0.0"
# wasmtime-wasi = "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 std::time::Duration;
use tauri::{async_runtime::RwLock, AppHandle}; use tauri::{async_runtime::RwLock, AppHandle};
use tauri_plugin_dialog::DialogExt; use tauri_plugin_dialog::DialogExt;
use tokio::time::sleep;
use wasmtime::AsContextMut; use wasmtime::AsContextMut;
use wellen::simple::Waveform; 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(); let file_path = file_response.path.into_os_string().into_string().unwrap();
spawn_konata_app();
let port = 30000; let port = 30000;
let base_url = format!("http://localhost:{port}"); let base_url = format!("http://localhost:{port}");
let client = reqwest::Client::builder() let client = reqwest::Client::builder()
.timeout(Duration::from_secs(30)) .connect_timeout(Duration::from_secs(1))
.build() .build()
.unwrap(); .unwrap();
if client
.get(format!("{base_url}/status")) let mut konata_server_ready = false;
.send()
.await let is_konata_server_ready = || async {
.is_ok()
{
client client
.post(format!("{base_url}/open-konata-file")) .get(format!("{base_url}/status"))
.json(&serde_json::json!({
"file_path": file_path
}))
.send() .send()
.await .await
.unwrap() .is_ok()
.error_for_status() };
.unwrap();
if is_konata_server_ready().await {
konata_server_ready = true;
} else { } 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")] #[cfg(target_family = "windows")]
fn spawn_konata_app() { fn spawn_konata_app() {
Command::new("cscript") Command::new("cscript")
.current_dir("../../konata") .current_dir("../../Konata")
.arg("konata.vbs") .arg("konata.vbs")
.spawn() .spawn()
.unwrap(); .unwrap();
@ -229,7 +247,7 @@ fn spawn_konata_app() {
#[cfg(target_family = "unix")] #[cfg(target_family = "unix")]
fn spawn_konata_app() { fn spawn_konata_app() {
Command::new("sh") Command::new("sh")
.current_dir("../../konata") .current_dir("../../Konata")
.arg("konata.sh") .arg("konata.sh")
.spawn() .spawn()
.unwrap(); .unwrap();