voelin-gpui: the tree model builds its rows once for a change that raises three signals
The model listens to the tree, toolbar and member card signals, and one batch can raise all three (a picture that arrives raises them all): every observer call built the whole tree_view again, O(people) with two stats per picture, so an AvatarReady or PictureReady cost three builds, two of them to find the rows the first had made. The signals of a batch are raised before the first is heard, so the model now keeps the generations it last built at and a refresh finds nothing new when it is the second or third of the batch. A test counts the builds (TreeModel::refreshes): a picture that arrives is one build, and so is a batch of two events that raise the same signals (the picture was three builds with the check disabled). The probe of the first commit of the round still has its two refreshes in one flush (its observer raises the signals again), and passes. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5a5fd9978d
commit
0352d67651
2 files changed
+67
-2
No files matched your search
@@ -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<AppState>, 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<AppState>,
|
||||
/// 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<Vec<TreeItem>>,
|
||||
people: Arc<Vec<MemberItem>>,
|
||||
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<Self>) {
|
||||
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);
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in new issue
Block a user