diff --git a/crates/weft-servo-shell/src/appd_ws.rs b/crates/weft-servo-shell/src/appd_ws.rs index ee4608d..091b535 100644 --- a/crates/weft-servo-shell/src/appd_ws.rs +++ b/crates/weft-servo-shell/src/appd_ws.rs @@ -5,6 +5,7 @@ use std::sync::mpsc; pub enum AppdCmd { Launch { session_id: u64, app_id: String }, Stop { session_id: u64 }, + SyncSessions { active: Vec<(u64, String)> }, } pub fn spawn_appd_listener( @@ -76,16 +77,17 @@ fn process_message(text: &str, tx: &mpsc::SyncSender, wake: &dyn Fn()) let Some(sessions) = v["sessions"].as_array() else { return; }; - for s in sessions { - let Some(session_id) = s["session_id"].as_u64() else { - continue; - }; - let Some(app_id) = s["app_id"].as_str().map(str::to_string) else { - continue; - }; - let _ = tx.try_send(AppdCmd::Launch { session_id, app_id }); + let active: Vec<(u64, String)> = sessions + .iter() + .filter_map(|s| { + let sid = s["session_id"].as_u64()?; + let aid = s["app_id"].as_str()?.to_string(); + Some((sid, aid)) + }) + .collect(); + if tx.try_send(AppdCmd::SyncSessions { active }).is_ok() { + wake(); } - wake(); } Some("APP_STATE") if v["state"].as_str() == Some("stopped") => { let Some(session_id) = v["session_id"].as_u64() else { diff --git a/crates/weft-servo-shell/src/embedder.rs b/crates/weft-servo-shell/src/embedder.rs index 0ea5f2f..0a5dc53 100644 --- a/crates/weft-servo-shell/src/embedder.rs +++ b/crates/weft-servo-shell/src/embedder.rs @@ -257,6 +257,19 @@ impl ApplicationHandler for App { self.active_session = None; } } + crate::appd_ws::AppdCmd::SyncSessions { active } => { + let active_ids: std::collections::HashSet = + active.iter().map(|(sid, _)| *sid).collect(); + self.app_webviews.retain(|sid, _| active_ids.contains(sid)); + if let Some(cur) = self.active_session { + if !active_ids.contains(&cur) { + self.active_session = None; + } + } + for (session_id, app_id) in active { + self.create_app_webview(session_id, &app_id); + } + } } } if let Some(servo) = &self.servo {