Mountain/ApplicationState/State/UIState/
UIState.rs1use std::{
36 collections::HashMap,
37 sync::{Arc, Mutex as StandardMutex},
38};
39
40use CommonLibrary::Error::CommonError::CommonError;
41
42use crate::dev_log;
43
44#[derive(Clone)]
46pub struct State {
47 pub PendingUserInterfaceRequest:
51 Arc<StandardMutex<HashMap<String, tokio::sync::oneshot::Sender<Result<serde_json::Value, CommonError>>>>>,
52}
53
54impl Default for State {
55 fn default() -> Self {
56 dev_log!("window", "[UIState] Initializing default UI state...");
57
58 Self { PendingUserInterfaceRequest:Arc::new(StandardMutex::new(HashMap::new())) }
59 }
60}
61
62impl State {
63 pub fn GetPendingRequests(&self) -> Vec<String> {
66 self.PendingUserInterfaceRequest
67 .lock()
68 .ok()
69 .map(|guard| guard.keys().cloned().collect())
70 .unwrap_or_default()
71 }
72
73 pub fn AddPendingRequest(
75 &self,
76
77 id:String,
78
79 sender:tokio::sync::oneshot::Sender<Result<serde_json::Value, CommonError>>,
80 ) {
81 if let Ok(mut guard) = self.PendingUserInterfaceRequest.lock() {
82 guard.insert(id, sender);
83
84 dev_log!("window", "[UIState] Pending UI request added");
85 }
86 }
87
88 pub fn RemovePendingRequest(
90 &self,
91
92 id:&str,
93 ) -> Option<tokio::sync::oneshot::Sender<Result<serde_json::Value, CommonError>>> {
94 if let Ok(mut guard) = self.PendingUserInterfaceRequest.lock() {
95 let sender = guard.remove(id);
96
97 dev_log!("window", "[UIState] Pending UI request removed: {}", id);
98
99 sender
100 } else {
101 None
102 }
103 }
104
105 pub fn ClearAll(&self) {
107 if let Ok(mut guard) = self.PendingUserInterfaceRequest.lock() {
108 guard.clear();
109
110 dev_log!("window", "[UIState] All pending UI requests cleared");
111 }
112 }
113
114 pub fn Count(&self) -> usize {
116 self.PendingUserInterfaceRequest
117 .lock()
118 .ok()
119 .map(|guard| guard.len())
120 .unwrap_or(0)
121 }
122
123 pub fn Contains(&self, id:&str) -> bool {
125 self.PendingUserInterfaceRequest
126 .lock()
127 .ok()
128 .map(|guard| guard.contains_key(id))
129 .unwrap_or(false)
130 }
131}