Mountain/IPC/WindServiceHandler/Lifecycle.rs
1#![allow(non_snake_case)]
2
3//! Lifecycle domain handlers for Wind IPC.
4
5use std::sync::Arc;
6
7use serde_json::{Value, json};
8use tauri::AppHandle;
9
10use crate::RunTime::ApplicationRunTime::ApplicationRunTime;
11
12/// Return the current application lifecycle phase (1-4).
13pub async fn handle_lifecycle_get_phase(Runtime:Arc<ApplicationRunTime>) -> Result<Value, String> {
14 let Phase = Runtime.Environment.ApplicationState.Feature.Lifecycle.GetPhase();
15 Ok(json!(Phase))
16}
17
18/// Wait (poll) until the application reaches at least the requested phase.
19/// Returns immediately if the phase has already been reached.
20pub async fn handle_lifecycle_when_phase(Runtime:Arc<ApplicationRunTime>, Args:Vec<Value>) -> Result<Value, String> {
21 let RequestedPhase = Args.first().and_then(|V| V.as_u64()).unwrap_or(1) as u8;
22 let CurrentPhase = Runtime.Environment.ApplicationState.Feature.Lifecycle.GetPhase();
23 if CurrentPhase >= RequestedPhase {
24 return Ok(Value::Null);
25 }
26 // Simple poll with short sleep - production should use a channel/notify
27 let mut Retries = 0u8;
28 loop {
29 tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
30 let Phase = Runtime.Environment.ApplicationState.Feature.Lifecycle.GetPhase();
31 if Phase >= RequestedPhase || Retries >= 50 {
32 break;
33 }
34 Retries += 1;
35 }
36 Ok(Value::Null)
37}
38
39/// Initiate a graceful application shutdown via Tauri.
40pub async fn handle_lifecycle_request_shutdown(AppHandle:AppHandle) -> Result<Value, String> {
41 AppHandle.exit(0);
42 Ok(Value::Null)
43}