diff --git a/crates/voelin-gpui/src/shell/tree_model.rs b/crates/voelin-gpui/src/shell/tree_model.rs index c1ac008..3384a64 100644 --- a/crates/voelin-gpui/src/shell/tree_model.rs +++ b/crates/voelin-gpui/src/shell/tree_model.rs @@ -19,7 +19,7 @@ use std::sync::Arc; -use gpui_kit::{Context, Entity, EventEmitter, Subscription}; +use gpui_kit::{App, Context, Entity, EventEmitter, Subscription}; use voelin_core::{ObserveState, VoiceState}; use voelin_ui_vm::bridge::{MemberCard, MemberItem, TreeItem}; use voelin_ui_vm::list::{Changes, diff}; @@ -104,10 +104,25 @@ pub enum TreeEvent { Lists, } +/// The areas the model listens to. +const AREAS: [Area; 3] = [Area::Tree, Area::Toolbar, Area::MemberCard]; + +/// How many times each of [`AREAS`] was raised. +fn generations(app: &Entity, cx: &App) -> [u64; 3] { + let signals = app.read(cx).signals(); + AREAS.map(|area| signals.of(area).read(cx).generation()) +} + /// The rows of the tree and of the members panel, the member card and the /// selection of the server on screen. pub struct TreeModel { app: Entity, + /// The generations of its signals it last built the rows at: a batch that + /// raises several of them (a picture that arrives raises three) has them + /// all raised before the first is heard, so the first refresh builds the + /// rows and the others find nothing new. + seen: [u64; 3], + refreshes: u64, rows: Arc>, people: Arc>, total: i32, @@ -182,6 +197,8 @@ impl TreeModel { ); Self { app: app.clone(), + seen: generations(app, cx), + refreshes: 0, rows: Arc::new(snapshot.rows), people: Arc::new(snapshot.people), total: snapshot.total, @@ -229,8 +246,21 @@ impl TreeModel { self.people_emptiness } - /// Build the rows again, and tell the panes what differs. + /// How many times the rows were built again since the model was made: one + /// for each batch of changes, not one for each signal it raised. + pub fn refreshes(&self) -> u64 { + self.refreshes + } + + /// Build the rows again, and tell the panes what differs. Nothing, when + /// none of the signals was raised since the last time. fn refresh(&mut self, cx: &mut Context) { + let now = generations(&self.app, cx); + if now == self.seen { + return; + } + self.seen = now; + self.refreshes += 1; let new = Snapshot::of(self.app.read(cx)); let rows = diff(&self.rows, &new.rows); let people = diff(&self.people, &new.people); diff --git a/crates/voelin-gpui/src/view.rs b/crates/voelin-gpui/src/view.rs index 748fb09..0835767 100644 --- a/crates/voelin-gpui/src/view.rs +++ b/crates/voelin-gpui/src/view.rs @@ -1710,6 +1710,41 @@ mod tests { assert!(cx.update(|_, cx| crate::pictures::is_cached(&second, cx)), "the new one is drawn"); } + /// A change that raises three of the model's signals (a picture that + /// arrives raises the toolbar, the tree and the member card) builds the + /// rows once: the signals are raised together, and the model's first + /// refresh sees all of them. + #[gpui::test] + fn a_picture_that_arrives_builds_the_rows_once(cx: &mut TestAppContext) { + let dir = TempDir::new("refresh-once"); + std::fs::create_dir_all(&dir.0).unwrap(); + let path = dir.0.join("avatar-demo-3"); + std::fs::write(&path, sample::demo_picture(40, 40, 1)).unwrap(); + let (shell, cx) = open(cx, Backend::in_memory().unwrap(), Switches::default()); + sample_server(&shell, cx); + let refreshes = |shell: &Shell, cx: &mut VisualTestContext| { + shell.view.read_with(cx, |view, cx| view.tree_model().read(cx).refreshes()) + }; + let before = refreshes(&shell, cx); + let arrives = Event::AvatarReady { + session: sample::DEMO as u64, + client_uid: "demo-3".into(), + path, + hash: String::new(), + }; + shell.events.send(arrives).unwrap(); + frame(cx); + assert_eq!(refreshes(&shell, cx), before + 1, "three signals, one build"); + + // A batch of two events that raise the same signals is one build as + // well. + let (a, b) = (presence_changed!(p => p.server.name = "A".into()), connected(sample::DEMO)); + shell.events.send(a).unwrap(); + shell.events.send(b).unwrap(); + frame(cx); + assert_eq!(refreshes(&shell, cx), before + 2, "a batch of events: one build"); + } + /// What the context menus offer that has no slice yet (the channel editor, /// a person's volume dialog, moving a client) is logged and sends nothing. #[gpui::test]