2026-02-08 21:29:17 +01:00
|
|
|
use crate::event_bus::{EventBusConfig, ResourceEvent};
|
2026-01-28 23:06:06 +01:00
|
|
|
use reddwarf_storage::RedbBackend;
|
|
|
|
|
use reddwarf_versioning::VersionStore;
|
|
|
|
|
use std::sync::Arc;
|
2026-02-08 21:29:17 +01:00
|
|
|
use tokio::sync::broadcast;
|
2026-01-28 23:06:06 +01:00
|
|
|
|
|
|
|
|
/// Shared application state
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct AppState {
|
|
|
|
|
/// Storage backend
|
|
|
|
|
pub storage: Arc<RedbBackend>,
|
|
|
|
|
|
|
|
|
|
/// Version store
|
|
|
|
|
pub version_store: Arc<VersionStore>,
|
2026-02-08 21:29:17 +01:00
|
|
|
|
|
|
|
|
/// Event bus sender — broadcast channel for resource mutation events
|
|
|
|
|
pub event_tx: broadcast::Sender<ResourceEvent>,
|
2026-01-28 23:06:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AppState {
|
2026-02-08 21:29:17 +01:00
|
|
|
/// Create a new AppState with default event bus config
|
2026-01-28 23:06:06 +01:00
|
|
|
pub fn new(storage: Arc<RedbBackend>, version_store: Arc<VersionStore>) -> Self {
|
2026-02-08 21:29:17 +01:00
|
|
|
Self::with_event_bus_config(storage, version_store, EventBusConfig::default())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create a new AppState with custom event bus config
|
|
|
|
|
pub fn with_event_bus_config(
|
|
|
|
|
storage: Arc<RedbBackend>,
|
|
|
|
|
version_store: Arc<VersionStore>,
|
|
|
|
|
config: EventBusConfig,
|
|
|
|
|
) -> Self {
|
|
|
|
|
let (event_tx, _) = broadcast::channel(config.capacity);
|
2026-01-28 23:06:06 +01:00
|
|
|
Self {
|
|
|
|
|
storage,
|
|
|
|
|
version_store,
|
2026-02-08 21:29:17 +01:00
|
|
|
event_tx,
|
2026-01-28 23:06:06 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-02-08 21:29:17 +01:00
|
|
|
|
|
|
|
|
/// Subscribe to resource events
|
|
|
|
|
pub fn subscribe(&self) -> broadcast::Receiver<ResourceEvent> {
|
|
|
|
|
self.event_tx.subscribe()
|
|
|
|
|
}
|
2026-01-28 23:06:06 +01:00
|
|
|
}
|