state recovery, polling optimizations, error rendering
widget states cached to SQLite, loaded on startup to seed DataProjection so server restart preserves last-known data for reconnecting clients. polling: first poll runs immediately, widget list cached per-task with 30s refresh, static text polled once inline instead of looping. poll failures propagate WidgetError::SourceUnavailable to clients. render engine prepends [offline] prefix in accent color, stale data preserved below.
This commit is contained in:
@@ -96,6 +96,15 @@ impl SqliteConfigStore {
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
|
||||
sqlx::query(
|
||||
"CREATE TABLE IF NOT EXISTS widget_state_cache (
|
||||
widget_id INTEGER PRIMARY KEY,
|
||||
state_json TEXT NOT NULL
|
||||
)",
|
||||
)
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
|
||||
// Add alignment columns to widgets (idempotent)
|
||||
let _ = sqlx::query("ALTER TABLE widgets ADD COLUMN h_align TEXT NOT NULL DEFAULT 'left'")
|
||||
.execute(&self.pool)
|
||||
|
||||
@@ -3,13 +3,14 @@ mod layout;
|
||||
mod presets;
|
||||
mod theme;
|
||||
mod users;
|
||||
mod widget_state_cache;
|
||||
mod widgets;
|
||||
|
||||
use crate::SqliteConfigStore;
|
||||
use crate::error::SqliteConfigError;
|
||||
use domain::{
|
||||
ConfigRepository, DataSource, DataSourceId, Layout, LayoutPreset, LayoutPresetId, ThemeConfig,
|
||||
User, WidgetConfig, WidgetId,
|
||||
User, WidgetConfig, WidgetId, WidgetState,
|
||||
};
|
||||
|
||||
impl ConfigRepository for SqliteConfigStore {
|
||||
@@ -90,4 +91,15 @@ impl ConfigRepository for SqliteConfigStore {
|
||||
async fn count_users(&self) -> Result<u32, Self::Error> {
|
||||
self.count_users_impl().await
|
||||
}
|
||||
|
||||
async fn save_widget_states(
|
||||
&self,
|
||||
states: &[(WidgetId, WidgetState)],
|
||||
) -> Result<(), Self::Error> {
|
||||
self.save_widget_states_impl(states).await
|
||||
}
|
||||
|
||||
async fn load_widget_states(&self) -> Result<Vec<(WidgetId, WidgetState)>, Self::Error> {
|
||||
self.load_widget_states_impl().await
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
use crate::SqliteConfigStore;
|
||||
use crate::error::SqliteConfigError;
|
||||
use domain::{Value, WidgetId, WidgetState};
|
||||
use sqlx::Row;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
impl SqliteConfigStore {
|
||||
pub(crate) async fn save_widget_states_impl(
|
||||
&self,
|
||||
states: &[(WidgetId, WidgetState)],
|
||||
) -> Result<(), SqliteConfigError> {
|
||||
for (id, state) in states {
|
||||
let json = domain_state_to_json(state)
|
||||
.map_err(|e| SqliteConfigError::Serialization(e.to_string()))?;
|
||||
sqlx::query(
|
||||
"INSERT OR REPLACE INTO widget_state_cache (widget_id, state_json) VALUES (?, ?)",
|
||||
)
|
||||
.bind(*id as i64)
|
||||
.bind(&json)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(SqliteConfigError::Sql)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn load_widget_states_impl(
|
||||
&self,
|
||||
) -> Result<Vec<(WidgetId, WidgetState)>, SqliteConfigError> {
|
||||
let rows = sqlx::query("SELECT widget_id, state_json FROM widget_state_cache")
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.map_err(SqliteConfigError::Sql)?;
|
||||
|
||||
let mut result = Vec::new();
|
||||
for row in &rows {
|
||||
let id: i64 = row.get("widget_id");
|
||||
let json_str: String = row.get("state_json");
|
||||
let state = json_to_domain_state(&json_str)
|
||||
.map_err(|e| SqliteConfigError::Serialization(e.to_string()))?;
|
||||
result.push((id as WidgetId, state));
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
fn domain_value_to_json(v: &Value) -> serde_json::Value {
|
||||
match v {
|
||||
Value::Null => serde_json::Value::Null,
|
||||
Value::Bool(b) => serde_json::Value::Bool(*b),
|
||||
Value::Number(n) => serde_json::json!(n),
|
||||
Value::String(s) => serde_json::Value::String(s.clone()),
|
||||
Value::Array(arr) => {
|
||||
serde_json::Value::Array(arr.iter().map(domain_value_to_json).collect())
|
||||
}
|
||||
Value::Object(map) => {
|
||||
let obj: serde_json::Map<String, serde_json::Value> = map
|
||||
.iter()
|
||||
.map(|(k, v)| (k.clone(), domain_value_to_json(v)))
|
||||
.collect();
|
||||
serde_json::Value::Object(obj)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn json_value_to_domain(v: &serde_json::Value) -> Value {
|
||||
match v {
|
||||
serde_json::Value::Null => Value::Null,
|
||||
serde_json::Value::Bool(b) => Value::Bool(*b),
|
||||
serde_json::Value::Number(n) => Value::Number(n.as_f64().unwrap_or(0.0)),
|
||||
serde_json::Value::String(s) => Value::String(s.clone()),
|
||||
serde_json::Value::Array(arr) => {
|
||||
Value::Array(arr.iter().map(json_value_to_domain).collect())
|
||||
}
|
||||
serde_json::Value::Object(map) => Value::Object(
|
||||
map.iter()
|
||||
.map(|(k, v)| (k.clone(), json_value_to_domain(v)))
|
||||
.collect(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
fn domain_state_to_json(state: &WidgetState) -> Result<String, serde_json::Error> {
|
||||
let data: serde_json::Map<String, serde_json::Value> = state
|
||||
.data
|
||||
.iter()
|
||||
.map(|(k, v)| (k.clone(), domain_value_to_json(v)))
|
||||
.collect();
|
||||
serde_json::to_string(&data)
|
||||
}
|
||||
|
||||
fn json_to_domain_state(json: &str) -> Result<WidgetState, serde_json::Error> {
|
||||
let map: serde_json::Map<String, serde_json::Value> = serde_json::from_str(json)?;
|
||||
let data: BTreeMap<String, Value> = map
|
||||
.iter()
|
||||
.map(|(k, v)| (k.clone(), json_value_to_domain(v)))
|
||||
.collect();
|
||||
Ok(WidgetState { data, error: None })
|
||||
}
|
||||
Reference in New Issue
Block a user