Skip to main content

Mountain/IPC/WindServiceHandlers/UI/
Lifecycle.rs

1#![allow(non_snake_case, unused_variables)]
2//! Lifecycle handlers: phase get / wait / shutdown. Tracks Mountain's
3//! four-phase startup (Starting / Ready / Restored / Eventually) so Sky
4//! can gate UI installation on lifecycle progress.
5//!
6//! `LifecycleWhenPhase` currently polls at 100 ms intervals up
7//! to 5 s; TODO to replace with a `tokio::sync::Notify` when the
8//! lifecycle service grows real broadcast semantics.
9
10use std::sync::Arc;
11
12use serde_json::{Value, json};
13use tauri::AppHandle;
14
15use crate::RunTime::ApplicationRunTime::ApplicationRunTime;
16
17pub async fn LifecycleGetPhase(RunTime:Arc<ApplicationRunTime>) -> Result<Value, String> {
18	let Phase = RunTime.Environment.ApplicationState.Feature.Lifecycle.GetPhase();
19	Ok(json!(Phase))
20}
21
22pub async fn LifecycleWhenPhase(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
23	let RequestedPhase = Arguments.first().and_then(|V| V.as_u64()).unwrap_or(1) as u8;
24	let CurrentPhase = RunTime.Environment.ApplicationState.Feature.Lifecycle.GetPhase();
25	if CurrentPhase >= RequestedPhase {
26		return Ok(Value::Null);
27	}
28	let mut Retries = 0u8;
29	loop {
30		tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
31		let Phase = RunTime.Environment.ApplicationState.Feature.Lifecycle.GetPhase();
32		if Phase >= RequestedPhase || Retries >= 50 {
33			break;
34		}
35		Retries += 1;
36	}
37	Ok(Value::Null)
38}
39
40pub async fn LifecycleRequestShutdown(ApplicationHandle:AppHandle) -> Result<Value, String> {
41	ApplicationHandle.exit(0);
42	Ok(Value::Null)
43}