Mountain/ApplicationState/State/FeatureState/Webviews/
WebviewState.rs

1//! # WebviewState Module (ApplicationState)
2//!
3//! ## RESPONSIBILITIES
4//! Manages webview panels state including webview metadata, content, and
5//! presentation state.
6//!
7//! ## ARCHITECTURAL ROLE
8//! WebviewState is part of the **FeatureState** module, representing
9//! webview panels state organized by webview ID.
10//!
11//! ## KEY COMPONENTS
12//! - WebviewState: Main struct containing active webviews map
13//! - Default: Initialization implementation
14//! - Helper methods: Webview manipulation utilities
15//!
16//! ## ERROR HANDLING
17//! - Thread-safe access via `Arc<Mutex<...>>`
18//! - Proper lock error handling with `MapLockError` helpers
19//!
20//! ## LOGGING
21//! State changes are logged at appropriate levels (debug, info, warn, error).
22//!
23//! ## PERFORMANCE CONSIDERATIONS
24//! - Lock mutexes briefly and release immediately
25//! - Avoid nested locks to prevent deadlocks
26//! - Use Arc for shared ownership across threads
27//!
28//! ## TODO
29//! - [ ] Add webview validation invariants
30//! - [ ] Implement webview lifecycle events
31//! - [ ] Add webview metrics collection
32
33use std::{
34	collections::HashMap,
35	sync::{Arc, Mutex as StandardMutex},
36};
37
38use log::debug;
39
40use crate::ApplicationState::DTO::WebviewStateDTO::WebviewStateDTO;
41
42/// Active webviews state containing webviews by ID.
43#[derive(Clone)]
44pub struct WebviewState {
45	/// Active webviews organized by ID.
46	pub ActiveWebviews:Arc<StandardMutex<HashMap<String, WebviewStateDTO>>>,
47}
48
49impl Default for WebviewState {
50	fn default() -> Self {
51		debug!("[WebviewState] Initializing default webview state...");
52
53		Self { ActiveWebviews:Arc::new(StandardMutex::new(HashMap::new())) }
54	}
55}
56
57impl WebviewState {
58	/// Gets all active webviews.
59	pub fn GetAll(&self) -> HashMap<String, WebviewStateDTO> {
60		self.ActiveWebviews.lock().ok().map(|guard| guard.clone()).unwrap_or_default()
61	}
62
63	/// Gets a webview by its ID.
64	pub fn Get(&self, id:&str) -> Option<WebviewStateDTO> {
65		self.ActiveWebviews.lock().ok().and_then(|guard| guard.get(id).cloned())
66	}
67
68	/// Adds or updates a webview.
69	pub fn AddOrUpdate(&self, id:String, webview:WebviewStateDTO) {
70		if let Ok(mut guard) = self.ActiveWebviews.lock() {
71			guard.insert(id, webview);
72			debug!("[WebviewState] Webview added/updated");
73		}
74	}
75
76	/// Removes a webview by its ID.
77	pub fn Remove(&self, id:&str) {
78		if let Ok(mut guard) = self.ActiveWebviews.lock() {
79			guard.remove(id);
80			debug!("[WebviewState] Webview removed: {}", id);
81		}
82	}
83
84	/// Clears all active webviews.
85	pub fn Clear(&self) {
86		if let Ok(mut guard) = self.ActiveWebviews.lock() {
87			guard.clear();
88			debug!("[WebviewState] All webviews cleared");
89		}
90	}
91
92	/// Gets the count of active webviews.
93	pub fn Count(&self) -> usize { self.ActiveWebviews.lock().ok().map(|guard| guard.len()).unwrap_or(0) }
94
95	/// Checks if a webview exists.
96	pub fn Contains(&self, id:&str) -> bool {
97		self.ActiveWebviews
98			.lock()
99			.ok()
100			.map(|guard| guard.contains_key(id))
101			.unwrap_or(false)
102	}
103}