I am trying to write a "start_timer" command, one that starts a loop and updates state on each tick of the loop:
#[tauri::command]
async fn start_timer(
state: tauri::State<'_, Mutex<AppState>>,
app_handle: tauri::AppHandle,
) -> Result<(), String> {
{
let mut state = state.lock().await;
state.is_active = true;
}
// Spawn a new asynchronous task for the timer
tauri::async_runtime::spawn(async move {
let mut interval = interval(Duration::from_secs(1));
loop {
interval.tick().await;
let state_handle = app_handle.state::<Mutex<AppState>>();
let mut state = state_handle.lock().await;
if !state.is_active {
break;
}
if state.remaining_time > 0 {
state.remaining_time -= 1;
} else {
state.is_active = false;
break;
}
}
});
Ok(())
}
This compiles, but it has a runtime error when invoked:
thread 'tokio-runtime-worker' panicked at C:\Users\quinn\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tauri-1.8.0\src\state.rs:51:7:
state not managed for field `state` on command `start_timer`. You must call `.manage()` before using this command
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
I've tried various things (eg. using arc) but none seem to work or compile.