From 6b01e25391779628e9b54f77e266d5d94ee09b87 Mon Sep 17 00:00:00 2001 From: faumaray Date: Wed, 28 Oct 2026 07:22:58 +0300 Subject: [PATCH] voelin-gpui: the tree and members events carry the rows their operations lead to TreeEvent::Rows and ::People carried the row operations only, and the panes applied them to the model's rows read when the event was delivered. A model that refreshes twice in one flush (it listens to the tree, toolbar and member card signals, and a batch raises several) holds the second rows by the time a pane hears of the first operations, so an observer that changes the state inside the flush made the first operations index the second rows: the gate's probe, select_server from the observer of the toolbar signal during a Presence flush, panicked in ChannelTree::apply on the shrink. The events now carry the rows (Rows(Changes, Arc>), People the same) and the panes apply the operations to the rows they have and keep the carried ones, so the events lead from the rows a pane started with to the model's last whatever the order of the flush. apply checks the counts in debug builds (debug_check), and "For the next slices" has the rule: a signal's observer reads the state and sets its own fields, a change of the root state is an action's or a task's. The probe is a test (a_state_change_inside_a_flush_does_not_apply_old_operations_to_new_rows); it panicked on the code before ("index out of bounds: the len is 0 but the index is 3", channels.rs) and passes now. Co-Authored-By: Claude Sonnet 5.5 --- crates/voelin-gpui/src/shell/channels.rs | 15 +++-- crates/voelin-gpui/src/shell/members.rs | 15 +++-- crates/voelin-gpui/src/shell/tree_model.rs | 46 +++++++++++---- crates/voelin-gpui/src/view.rs | 67 ++++++++++++++++++++-- docs/ui.md | 26 ++++++++- 5 files changed, 134 insertions(+), 35 deletions(-) diff --git a/crates/voelin-gpui/src/shell/channels.rs b/crates/voelin-gpui/src/shell/channels.rs index 417d4c3..61832ba 100644 --- a/crates/voelin-gpui/src/shell/channels.rs +++ b/crates/voelin-gpui/src/shell/channels.rs @@ -28,7 +28,7 @@ use voelin_ui_vm::tree::ChannelId; use super::actions::{ EditChannel, JoinChannel, MoveClient, OpenClient, OpenMember, SelectChannel, ToggleChannel, }; -use super::tree_model::{Emptiness, TreeEvent, TreeModel}; +use super::tree_model::{Emptiness, TreeEvent, TreeModel, debug_check}; use super::widgets::{Avatar, banner_scrim, icon, live_badge, small_picture}; use crate::meter::Counters; use crate::pictures::{PictureKey, picture}; @@ -58,10 +58,7 @@ impl ChannelTree { }; let subscription = cx.subscribe(model, |this: &mut Self, model, event: &TreeEvent, cx| { match event { - TreeEvent::Rows(changes) => { - let new = model.read(cx).rows().clone(); - this.apply(changes, new); - } + TreeEvent::Rows(changes, rows) => this.apply(changes, rows.clone()), TreeEvent::Selection => this.selected = model.read(cx).selected(), // The count of people is the members panel's. TreeEvent::Lists => { @@ -71,7 +68,7 @@ impl ChannelTree { } this.emptiness = emptiness; } - TreeEvent::People(_) | TreeEvent::Card => return, + TreeEvent::People(..) | TreeEvent::Card => return, } cx.notify(); }); @@ -96,11 +93,13 @@ impl ChannelTree { self.selected } - /// Take the new rows in, telling the list which rows came, went or - /// changed their height (a channel row and a person's differ). + /// Take the new rows in (the rows `changes` lead to, carried by the event), + /// telling the list which rows came, went or changed their height (a + /// channel row and a person's differ). fn apply(&mut self, changes: &voelin_ui_vm::list::Changes, new: Arc>) { use voelin_ui_vm::list::Change; + debug_check(self.rows.len(), changes, new.len()); let mut channels: Vec = self.rows.iter().map(|row| row.is_channel).collect(); for change in &changes.0 { match *change { diff --git a/crates/voelin-gpui/src/shell/members.rs b/crates/voelin-gpui/src/shell/members.rs index 423e13f..d91032b 100644 --- a/crates/voelin-gpui/src/shell/members.rs +++ b/crates/voelin-gpui/src/shell/members.rs @@ -24,7 +24,7 @@ use voelin_ui_vm::bridge::{MemberCard, MemberItem}; use voelin_ui_vm::list::Changes; use super::actions::{OpenMember, SearchMembers}; -use super::tree_model::{Emptiness, TreeEvent, TreeModel}; +use super::tree_model::{Emptiness, TreeEvent, TreeModel, debug_check}; use super::widgets::{ Avatar, IconButton, Presence, chip, icon, live_badge, small_picture, speaking_bars, }; @@ -69,10 +69,7 @@ impl MembersPane { let subscriptions = vec![ cx.subscribe(model, |this: &mut Self, model, event: &TreeEvent, cx| { match event { - TreeEvent::People(changes) => { - let new = model.read(cx).people().clone(); - this.apply(changes, new); - } + TreeEvent::People(changes, rows) => this.apply(changes, rows.clone()), TreeEvent::Lists => { let model = model.read(cx); let now = (model.total(), model.emptiness()); @@ -82,7 +79,7 @@ impl MembersPane { (this.total, this.emptiness) = now; } TreeEvent::Card => this.open = model.read(cx).card().map(|card| card.id), - TreeEvent::Rows(_) | TreeEvent::Selection => return, + TreeEvent::Rows(..) | TreeEvent::Selection => return, } cx.notify(); }), @@ -122,11 +119,13 @@ impl MembersPane { self.searching } - /// Take the new rows in, telling the list which rows came, went or changed - /// their height (the first row of a section has the heading above it). + /// Take the new rows in (the rows `changes` lead to, carried by the event), + /// telling the list which rows came, went or changed their height (the + /// first row of a section has the heading above it). fn apply(&mut self, changes: &Changes, new: Arc>) { use voelin_ui_vm::list::Change; + debug_check(self.rows.len(), changes, new.len()); let mut headed: Vec = self.rows.iter().map(|row| row.first_in_group).collect(); for change in &changes.0 { match *change { diff --git a/crates/voelin-gpui/src/shell/tree_model.rs b/crates/voelin-gpui/src/shell/tree_model.rs index dd881cb..826af69 100644 --- a/crates/voelin-gpui/src/shell/tree_model.rs +++ b/crates/voelin-gpui/src/shell/tree_model.rs @@ -6,9 +6,10 @@ //! ([`crate::signals`]), builds the rows once for both panes, compares them //! with the rows it had ([`voelin_ui_vm::list::diff`], the row operations the //! Slint front end applies to its `VecModel`) and emits what differs -//! ([`TreeEvent`]): a pane applies the operations to its list state, so a -//! person who joins is a row that is inserted, not a list that starts again -//! at the top. A change that moves none of what a pane shows emits nothing. +//! ([`TreeEvent`], with the rows the operations lead to): a pane applies the +//! operations to its list state, so a person who joins is a row that is +//! inserted, not a list that starts again at the top. A change that moves none +//! of what a pane shows emits nothing. //! //! **The tree does not mark who is talking.** Its rows are built with //! `talking` cleared: a talker changes the rows of the members panel (and the @@ -53,14 +54,23 @@ impl Emptiness { } /// What changed in the model, as the panes need it. +/// +/// The list events carry the rows their operations lead to. A model that +/// refreshed twice before the first event is delivered (an observer that +/// changes the state inside a flush makes it) holds the second rows by the +/// time a pane hears of the first operations, and operations applied to rows +/// they were not made for index past their end. A pane applies them to the +/// rows it has and keeps the rows the event carries, so the events, in the +/// order they are delivered, lead from the rows the pane started with to the +/// model's last. #[derive(Clone, Debug, PartialEq)] pub enum TreeEvent { - /// The tree's rows: the operations from the old rows to - /// [`TreeModel::rows`]. - Rows(Changes), - /// The members panel's rows: the operations from the old rows to - /// [`TreeModel::people`]. - People(Changes), + /// The tree's rows: the operations from the rows the pane has to the rows + /// carried (the model's [`TreeModel::rows`] when it made them). + Rows(Changes, Arc>), + /// The members panel's rows: the operations from the rows the pane has to + /// the rows carried (the model's [`TreeModel::people`] when it made them). + People(Changes, Arc>), /// The card over a member row opened, changed or closed /// ([`TreeModel::card`]). Card, @@ -86,6 +96,20 @@ pub struct TreeModel { impl EventEmitter for TreeModel {} +/// Check that `changes` lead from a list of `old` rows to one of `new` rows +/// (debug builds): a pane that applies operations to rows they were not made +/// for finds the counts out of step, where a release build would index past +/// the end or leave its list state out of step with its rows. +pub(super) fn debug_check(old: usize, changes: &Changes, new: usize) { + debug_assert_eq!( + old + changes.inserted(), + new + changes.removed(), + "{changes:?} do not lead from {old} rows to {new}: applied to rows they were not made \ + for? (docs/ui.md, \"For the next slices\": no state change from a signal's observer \ + inside a flush)" + ); +} + /// Everything the model keeps, as the state gives it now. struct Snapshot { rows: Vec, @@ -193,10 +217,10 @@ impl TreeModel { let selection = self.selected != new.selected; self.selected = new.selected; if !rows.is_empty() { - cx.emit(TreeEvent::Rows(rows)); + cx.emit(TreeEvent::Rows(rows, self.rows.clone())); } if !people.is_empty() { - cx.emit(TreeEvent::People(people)); + cx.emit(TreeEvent::People(people, self.people.clone())); } if lists { cx.emit(TreeEvent::Lists); diff --git a/crates/voelin-gpui/src/view.rs b/crates/voelin-gpui/src/view.rs index fe2b4b3..e69e3a4 100644 --- a/crates/voelin-gpui/src/view.rs +++ b/crates/voelin-gpui/src/view.rs @@ -538,6 +538,7 @@ mod tests { use crate::pictures::PictureKey; use crate::shell::actions::{EditChannel, MoveClient, OpenClient}; use crate::shell::{RailView, TreeEvent}; + use crate::signals::Area; use crate::theme::Choice; use crate::window_bounds; @@ -939,12 +940,12 @@ mod tests { (log, subscription) } - /// The row operations the model told the tree. - fn rows_told(log: &Rc>>) -> Vec { + /// The row operations the model told the tree, with the rows they lead to. + fn rows_told(log: &Rc>>) -> Vec<(Changes, Arc>)> { log.borrow() .iter() .filter_map(|event| match event { - TreeEvent::Rows(changes) => Some(changes.clone()), + TreeEvent::Rows(changes, rows) => Some((changes.clone(), rows.clone())), _ => None, }) .collect() @@ -1253,8 +1254,11 @@ mod tests { .unwrap(); frame(cx); let dex = before.iter().position(|r| !r.is_channel && r.id == 5).unwrap(); - assert_eq!(rows_told(&log), [Changes(vec![Change::Update(dex)])]); + let told = rows_told(&log); + let [(changes, carried)] = &told[..] else { panic!("{:?}", log.borrow()) }; + assert_eq!(*changes, Changes(vec![Change::Update(dex)])); assert!(before[dex].muted && !tree_rows(&shell, cx)[dex].muted); + assert_eq!(*carried, tree_rows(&shell, cx), "the event carries the rows it leads to"); // Zeph (7) leaves Raid Night for Gaming: its parent's count, his row // and the row of the sub-channel change; the rest stay. @@ -1271,7 +1275,8 @@ mod tests { ["C0 Gaming", "P1 Talon", "P1 Zeph", "C1 Raid Night", "C0 Music"], ); let told = rows_told(&log); - let [changes] = &told[..] else { panic!("{:?}", log.borrow()) }; + let [(changes, carried)] = &told[..] else { panic!("{:?}", log.borrow()) }; + assert_eq!(*carried, after); let gaming = 8; assert_eq!( changes.0, @@ -1281,6 +1286,58 @@ mod tests { assert_eq!((after[gaming].members, after[gaming + 3].members), (2, 0)); } + /// The gate's probe. A state change made by an observer while a flush is + /// running must not make a pane apply the first row operations to the + /// second rows: the events carry the rows their operations lead to. A + /// Presence event raises the toolbar and the tree; the model makes its + /// first rows on the toolbar, an observer of the toolbar that was made + /// after the model's puts the observed server on screen (it has no rows + /// yet), and the model makes its second rows, none, on the tree before + /// the panes are told of the first: a shrink under operations made for + /// the rows before it. + #[gpui::test] + fn a_state_change_inside_a_flush_does_not_apply_old_operations_to_new_rows( + cx: &mut TestAppContext, + ) { + let (shell, cx) = open(cx, Backend::in_memory().unwrap(), Switches::default()); + sample_server(&shell, cx); + assert!(!tree_rows(&shell, cx).is_empty()); + let toolbar = shell.app.read_with(cx, |app, _| app.signals().of(Area::Toolbar).clone()); + let fired = Rc::new(Cell::new(false)); + let _observer = cx.update(|_, cx| { + let (app, fired) = (shell.app.clone(), fired.clone()); + cx.observe(&toolbar, move |_, cx| { + if !fired.replace(true) { + app.update(cx, |app, cx| app.select_server(sample::DEMO + 1, cx)); + } + }) + }); + let (log, _subscription) = tree_events(&shell, cx); + let unmute = presence_changed!(p => p.clients.get_mut(&5).unwrap().input_muted = false); + shell.events.send(unmute).unwrap(); + frame(cx); + assert!(fired.get(), "the observer ran inside the flush"); + assert_eq!(shell.app.read_with(cx, |app, _| app.vm.current), Some(sample::DEMO + 1)); + + // The model told its panes twice, and each time with the rows its + // operations lead to: the first leaves the server's rows with dex + // unmuted, the second takes them all away. + let told = rows_told(&log); + let [first, second] = &told[..] else { panic!("{:?}", log.borrow()) }; + assert_eq!(first.1.len(), 18, "the first rows are the sample server's"); + assert!(!first.1.iter().any(|r| !r.is_channel && r.id == 5 && r.muted)); + assert_eq!((first.0.updated(), first.0.removed(), first.0.inserted()), (1, 0, 0)); + assert!(second.1.is_empty() && second.0.removed() == 18, "{:?}", second.0); + + // The panes end with the model's rows, in the order they were told. + assert!(tree_rows(&shell, cx).is_empty()); + let (tree, people) = shell.view.read_with(cx, |view, cx| { + (view.channel_tree().read(cx).rows().len(), view.members_pane().read(cx).rows().len()) + }); + assert_eq!((tree, people), (0, 0)); + assert!(at(cx, "tree-empty").is_some(), "the tree says it is loading"); + } + /// Who talks changes the members panel and the sidebar's own card, not the /// tree: its pane is not drawn for a talker, and its rows stay as they /// were. diff --git a/docs/ui.md b/docs/ui.md index f9523cb..c2a095f 100644 --- a/docs/ui.md +++ b/docs/ui.md @@ -1502,9 +1502,12 @@ the tests: entity builds the rows for both panes on the tree, toolbar and member-card signals, compares them with the rows it had (`voelin_ui_vm::list::diff`, the operations the Slint front end applies to its `VecModel`) and emits what - differs. A pane applies the operations to its list state (`gpui::ListState`: - a person who joins is a row inserted and the scroll stays where it was), and a - change that moves none of the rows of a pane emits nothing to it. + differs, with the rows the operations lead to (`TreeEvent::Rows(Changes, + Arc>)`, `People` the same). A pane applies the operations to its + list state (`gpui::ListState`: a person who joins is a row inserted and the + scroll stays where it was) and to the rows it has, and keeps the rows the + event carries, not the model's at the moment it hears of them; a change that + moves none of the rows of a pane emits nothing to it. - **The tree carries no `talking`.** Its rows are built with the flag cleared, so someone who starts or stops talking changes the members panel (and the sidebar's own card when it is us) and not the tree: its pane is not rendered @@ -1766,6 +1769,23 @@ the slices that meet it; none of it is a defect of what exists today. panes (a cached view inside a view that renders again is rendered again), and raises its flag from `Signals::areas`. A new area is one more variant of `signals::Area`. +- **No state change from a signal's observer, inside a flush.** The model + listens to three signals (tree, toolbar, member card) and a batch raises + several of them, so it may refresh twice in one flush of GPUI's effects, and + what it emitted the first time is delivered after the second refresh has + run. An observer that changes the state in that window (`select_server`, + say, in the observer of a signal) makes the second refresh a different list + from the one the first operations were made for. The events carry their rows + for that reason, and the panes check in debug builds that the operations lead + from the rows they have to the rows carried (`debug_check`); a probe in the + tests makes exactly this change, from the observer of the toolbar signal + during a Presence flush, and found the old code indexing past the end of the + rows (`a_state_change_inside_a_flush_does_not_apply_old_operations_to_new_rows`). + The rule for a new part is that an observer of a signal reads the state and + sets its own fields, and a change of the root state belongs to an action's + handler or a task of its own, not to a signal's observer. The events that + carry no rows (`Selection`, `Lists`, `Card`) are read from the model at + delivery and end right whatever the order. - **A click renders every pane; a hover only the pane it is over.** In gpui-pre 0.3.10 pressing a clickable element and releasing it call `window.refresh()` (`elements/div.rs`), which ignores every cache for that frame, so a click on a