resolve live data on initial load

This commit is contained in:
2026-08-31 01:15:32 +08:00
parent c68f8f0bf7
commit 25eb21755f
10 changed files with 293 additions and 50 deletions
+41
View File
@@ -1,4 +1,8 @@
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use specta::Type;
use tauri::{AppHandle, Manager};
use crate::cursor::CursorPositions;
use crate::ufa::AppMeta;
@@ -10,6 +14,43 @@ pub enum LiveData {
ForegroundApp { meta: AppMeta },
}
#[derive(Debug, Clone, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct LiveDataSnapshot {
pub cursor_positions: HashMap<String, CursorPositions>,
pub foreground_apps: HashMap<String, AppMeta>,
}
#[tauri::command]
#[specta::specta]
pub fn list_live_data(handle: AppHandle) -> Result<LiveDataSnapshot, String> {
Ok(LiveDataSnapshot {
cursor_positions: handle.state::<crate::cursor::CursorState>().snapshot()?,
foreground_apps: handle
.state::<crate::ufa::ForegroundAppState>()
.snapshot()?,
})
}
pub(crate) fn publish_current(handle: &AppHandle) -> Result<(), String> {
let user_id = handle
.state::<crate::keypair::AppKeypair>()
.public_key()
.to_owned();
let network = handle.state::<crate::network::Network>();
if let Some(positions) = handle.state::<crate::cursor::CursorState>().get(&user_id)? {
network.send_live_data(LiveData::Cursor { positions });
}
if let Some(meta) = handle
.state::<crate::ufa::ForegroundAppState>()
.get(&user_id)?
{
network.send_live_data(LiveData::ForegroundApp { meta });
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;