Mountain/ApplicationState/State/FeatureState/LifecyclePhase/
LifecyclePhaseState.rs1use std::sync::{Arc, Mutex as StandardMutex};
2
3use tokio::sync::Notify;
4use CommonLibrary::IPC::SkyEvent::SkyEvent;
5
6use crate::{IPC::SkyEmit::LogSkyEmit, dev_log};
7
8pub type Phase = u8;
11
12#[derive(Clone)]
16pub struct LifecyclePhaseState {
17 CurrentPhase:Arc<StandardMutex<Phase>>,
18
19 pub PhaseNotify:Arc<Notify>,
21}
22
23impl Default for LifecyclePhaseState {
24 fn default() -> Self {
25 dev_log!(
26 "lifecycle",
27 "[LifecyclePhaseState] Initializing default lifecycle state (phase 1: Starting)..."
28 );
29
30 Self {
31 CurrentPhase:Arc::new(StandardMutex::new(1)),
32
33 PhaseNotify:Arc::new(Notify::new()),
34 }
35 }
36}
37
38impl LifecyclePhaseState {
39 pub fn GetPhase(&self) -> Phase { self.CurrentPhase.lock().ok().map(|Guard| *Guard).unwrap_or(1) }
41
42 pub fn SetPhase(&self, NewPhase:Phase) {
44 if let Ok(mut Guard) = self.CurrentPhase.lock() {
45 if NewPhase > *Guard {
46 dev_log!("lifecycle", "[LifecyclePhaseState] Phase advanced: {} → {}", *Guard, NewPhase);
47
48 *Guard = NewPhase;
49
50 self.PhaseNotify.notify_waiters();
52 }
53 }
54 }
55
56 pub fn AdvanceAndBroadcast<R:tauri::Runtime>(&self, NewPhase:Phase, ApplicationHandle:&tauri::AppHandle<R>) {
63 let Previous = self.GetPhase();
66
67 if NewPhase <= Previous {
68 return;
69 }
70
71 self.SetPhase(NewPhase);
72
73 let Label = match NewPhase {
74 1 => "Starting",
75
76 2 => "Ready",
77
78 3 => "Restored",
79
80 4 => "Eventually",
81
82 _ => "Unknown",
83 };
84
85 if let Err(Error) = LogSkyEmit(
86 ApplicationHandle,
87 SkyEvent::LifecyclePhaseChanged.AsStr(),
88 serde_json::json!({
89 "phase": NewPhase,
90 "previous": Previous,
91 "label": Label,
92 }),
93 ) {
94 dev_log!(
95 "lifecycle",
96 "warn: [LifecyclePhaseState] sky://lifecycle/phaseChanged emit failed: {}",
97 Error
98 );
99 }
100 }
101}