@@ -11,12 +11,14 @@ use std::{
1111 sync:: {
1212 Arc ,
1313 Mutex as StandardMutex ,
14+ PoisonError ,
1415 atomic:: { AtomicBool , AtomicU32 , AtomicU64 , Ordering as AtomicOrdering } ,
1516 } ,
1617} ;
1718
19+ use Common :: Error :: CommonError :: CommonError ;
1820use log:: { error, info, warn} ;
19- use tauri:: { Manager , Wry } ;
21+ use tauri:: Wry ;
2022
2123use super :: {
2224 DTO :: {
@@ -38,10 +40,6 @@ use super::{
3840use crate :: Environment :: CommandProvider :: CommandHandler ;
3941
4042/// The central, shared, thread-safe state for the entire Mountain application.
41- ///
42- /// This struct consolidates all dynamic state required by the backend, from
43- /// workspace information and configuration to the state of active UI components
44- /// like terminals and WebViews.
4543#[ derive( Clone ) ]
4644pub struct ApplicationState {
4745 // --- WorkSpace State ---
@@ -78,16 +76,12 @@ pub struct ApplicationState {
7876 pub ActiveTreeViews : Arc < StandardMutex < HashMap < String , TreeViewStateDTO > > > ,
7977
8078 // --- IPC & User Interface State ---
81- pub PendingUserInterfaceRequests : Arc <
82- StandardMutex <
83- HashMap <
84- String ,
85- tokio:: sync:: oneshot:: Sender < Result < serde_json:: Value , Common :: Error :: CommonError :: CommonError > > ,
86- > ,
87- > ,
88- > ,
79+ pub PendingUserInterfaceRequests :
80+ Arc < StandardMutex < HashMap < String , tokio:: sync:: oneshot:: Sender < Result < serde_json:: Value , CommonError > > > > > ,
8981}
9082
83+ fn map_lock_error < T > ( e : PoisonError < T > ) -> CommonError { CommonError :: StateLockPoisoned { Context : e. to_string ( ) } }
84+
9185impl Default for ApplicationState {
9286 fn default ( ) -> Self {
9387 info ! ( "[ApplicationState] Initializing default application state..." ) ;
@@ -114,8 +108,6 @@ impl Default for ApplicationState {
114108
115109 let GlobalMementoFilePath = Internal :: ResolveMementoStorageFilePath ( & ApplicationDataDirectoryPath , true , "" ) ;
116110 let InitialGlobalMementoMap = Internal :: LoadInitialMementoFromDisk ( & GlobalMementoFilePath ) ;
117- // let InitialCommandRegistryMap =
118- // crate::Handler::Command::RegisterNativeCommands(); // TODO: Re-integrate
119111
120112 info ! ( "[ApplicationState] Default state initialization complete." ) ;
121113 Self {
@@ -128,7 +120,7 @@ impl Default for ApplicationState {
128120 GlobalMementoPath : GlobalMementoFilePath ,
129121 WorkSpaceMemento : Arc :: new ( StandardMutex :: new ( HashMap :: new ( ) ) ) ,
130122 WorkSpaceMementoPath : Arc :: new ( StandardMutex :: new ( None ) ) ,
131- CommandRegistry : Arc :: new ( StandardMutex :: new ( HashMap :: new ( ) ) ) , // TODO: Use InitialCommandRegistryMap
123+ CommandRegistry : Arc :: new ( StandardMutex :: new ( HashMap :: new ( ) ) ) ,
132124 DiagnosticsMap : Arc :: new ( StandardMutex :: new ( HashMap :: new ( ) ) ) ,
133125 OpenDocuments : Arc :: new ( StandardMutex :: new ( HashMap :: new ( ) ) ) ,
134126 OutputChannels : Arc :: new ( StandardMutex :: new ( HashMap :: new ( ) ) ) ,
@@ -149,17 +141,16 @@ impl Default for ApplicationState {
149141}
150142
151143impl ApplicationState {
152- /// Generates a unique, filesystem-safe identifier string for the current
144+ /// Generates a unique, filesystem-safe identifier for the current
153145 /// workspace.
154- pub fn GetWorkSpaceIdentifier ( & self ) -> Result < String , String > {
155- let LockErrorMapper = |e| format ! ( "[AppState] Lock error: {}" , e) ;
156- let ConfigurationPathGuard = self . WorkSpaceConfigurationPath . lock ( ) . map_err ( LockErrorMapper ) ?;
146+ pub fn GetWorkSpaceIdentifier ( & self ) -> Result < String , CommonError > {
147+ let ConfigurationPathGuard = self . WorkSpaceConfigurationPath . lock ( ) . map_err ( map_lock_error) ?;
157148 if let Some ( ConfigurationPath ) = ConfigurationPathGuard . as_ref ( ) {
158149 return Ok ( ConfigurationPath . file_name ( ) . unwrap_or_default ( ) . to_string_lossy ( ) . into_owned ( ) ) ;
159150 }
160151 drop ( ConfigurationPathGuard ) ;
161152
162- let FoldersGuard = self . WorkSpaceFolders . lock ( ) . map_err ( LockErrorMapper ) ?;
153+ let FoldersGuard = self . WorkSpaceFolders . lock ( ) . map_err ( map_lock_error ) ?;
163154 if let Some ( FirstFolder ) = FoldersGuard . first ( ) {
164155 let PathString = FirstFolder . URI . path ( ) ;
165156 // Create a more stable hash for the identifier.
@@ -184,14 +175,13 @@ impl ApplicationState {
184175 /// Updates the path to the workspace memento file and reloads its content
185176 /// from disk.
186177 pub fn UpdateWorkSpaceMementoPathAndReload ( & self , ApplicationDataDirectory : & Path ) -> Result < ( ) , String > {
187- let LockErrorMapper = |e| format ! ( "[AppState] Lock error: {}" , e) ;
188- let WorkSpaceIdentifier = self . GetWorkSpaceIdentifier ( ) ?;
189- let mut PathGuard = self . WorkSpaceMementoPath . lock ( ) . map_err ( LockErrorMapper ) ?;
178+ let WorkSpaceIdentifier = self . GetWorkSpaceIdentifier ( ) . map_err ( |e| e. to_string ( ) ) ?;
179+ let mut PathGuard = self . WorkSpaceMementoPath . lock ( ) . map_err ( |e| e. to_string ( ) ) ?;
190180
191181 if WorkSpaceIdentifier == "NO_WORKSPACE" {
192182 if PathGuard . is_some ( ) {
193183 * PathGuard = None ;
194- self . WorkSpaceMemento . lock ( ) . map_err ( LockErrorMapper ) ?. clear ( ) ;
184+ self . WorkSpaceMemento . lock ( ) . map_err ( |e| e . to_string ( ) ) ?. clear ( ) ;
195185 }
196186 return Ok ( ( ) ) ;
197187 }
@@ -206,7 +196,7 @@ impl ApplicationState {
206196 }
207197 * PathGuard = Some ( NewMementoPath . clone ( ) ) ;
208198 let NewMementoContent = Internal :: LoadInitialMementoFromDisk ( & NewMementoPath ) ;
209- * self . WorkSpaceMemento . lock ( ) . map_err ( LockErrorMapper ) ? = NewMementoContent ;
199+ * self . WorkSpaceMemento . lock ( ) . map_err ( |e| e . to_string ( ) ) ? = NewMementoContent ;
210200 }
211201 Ok ( ( ) )
212202 }
0 commit comments