use crate::data_model::{DataModel, WebSocketMessage}; use std::sync::Arc; use tokio::sync::RwLock; use tracing::debug; /// Manages the application state and broadcasts updates to WebSocket clients pub struct StateManager { data: Arc>, revision: Arc>, } impl StateManager { pub fn new() -> Self { Self { data: Arc::new(RwLock::new(DataModel::new())), revision: Arc::new(RwLock::new(0)), } } /// Get the current data model and revision pub async fn get_snapshot(&self) -> (DataModel, u32) { let data = self.data.read().await.clone(); let rev = *self.revision.read().await; (data, rev) } /// Update the data model (will broadcast patches in the future) pub async fn update_data(&self, new_data: DataModel) { let mut data = self.data.write().await; let mut rev = self.revision.write().await; *data = new_data; *rev += 1; debug!("Data model updated to revision {}", *rev); // TODO: In the future, compute JSON patches and broadcast to all connected clients // For now, clients will need to reconnect to get updates } /// Get a WebSocket message with the current state pub async fn get_initial_message(&self) -> WebSocketMessage { let (data, rev) = self.get_snapshot().await; WebSocketMessage { rev, data: Some(data), patch: None, } } } impl Default for StateManager { fn default() -> Self { Self::new() } }