Dorian 34fc06726e Enhance development workflow and deployment practices for Archipelago
- Updated the Development-Workflow documentation to clarify deployment strategy, emphasizing direct deployment to the live system for testing.
- Added detailed instructions for the deployment command, including syncing code, building frontend and backend, and restarting services.
- Improved SSH key management section to assist with authentication issues.
- Expanded the testing workflow to include steps for checking logs and syncing changes back to the ISO build.
- Updated the ISO build integration section to ensure system-level changes are captured for future builds.
- Refactored various sections for clarity and completeness, including deployment paths and system configuration files.
2026-02-01 13:24:03 +00:00

73 lines
2.1 KiB
Rust

use crate::data_model::{DataModel, WebSocketMessage};
use std::sync::Arc;
use tokio::sync::{broadcast, RwLock};
use tracing::debug;
/// Manages the application state and broadcasts updates to WebSocket clients
pub struct StateManager {
data: Arc<RwLock<DataModel>>,
revision: Arc<RwLock<u32>>,
broadcast_tx: broadcast::Sender<WebSocketMessage>,
}
impl StateManager {
pub fn new() -> Self {
let (broadcast_tx, _) = broadcast::channel(100);
Self {
data: Arc::new(RwLock::new(DataModel::new())),
revision: Arc::new(RwLock::new(0)),
broadcast_tx,
}
}
/// 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)
}
/// Subscribe to state updates
pub fn subscribe(&self) -> broadcast::Receiver<WebSocketMessage> {
self.broadcast_tx.subscribe()
}
/// Update the data model and broadcast to all connected clients
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.clone();
*rev += 1;
debug!("Data model updated to revision {}", *rev);
// Broadcast full data dump to all connected clients
// In the future, we can optimize this by computing and sending JSON patches
let message = WebSocketMessage {
rev: *rev,
data: Some(new_data),
patch: None,
};
// Ignore errors if no receivers are connected
let _ = self.broadcast_tx.send(message);
}
/// 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()
}
}