use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RuntimePatch {
ReplaceView {
node_id: u64,
new_view: serde_json::Value,
},
UpdateState {
node_id: u64,
field: String,
value: serde_json::Value,
},
Batch(Vec<RuntimePatch>),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(dead_code)]
pub struct RuntimeStateSnapshot {
pub nodes: Vec<NodeStateSnapshot>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(dead_code)]
pub struct NodeStateSnapshot {
pub id: u64,
pub state: serde_json::Value,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(dead_code)]
pub struct RuntimeEvent {
pub kind: String,
pub payload: serde_json::Value,
}
pub fn apply_patch(patch: RuntimePatch) {
let state_lock = crate::get_system_state();
let _state = state_lock.write().unwrap();
match patch {
RuntimePatch::ReplaceView { node_id, new_view } => {
log::info!(
"Runtime: Replacing view subgraph for node {}. (Serialized: {})",
node_id,
new_view
);
}
RuntimePatch::UpdateState {
node_id,
field,
value,
} => {
log::info!(
"Runtime: Updating state for node {} field '{}' to {}",
node_id,
field,
value
);
}
RuntimePatch::Batch(patches) => {
for p in patches {
apply_patch(p);
}
}
}
}
pub fn snapshot_state() -> RuntimeStateSnapshot {
let state_lock = crate::get_system_state();
let state = state_lock.read().unwrap();
RuntimeStateSnapshot {
nodes: state.snapshot(),
}
}