gui: SyncGuarded::current_mut, and one queue edit path

The podcast list's four optimistic row edits each cloned the whole episode
list to change one row and set it back; SyncGuarded::current_mut edits in
place (set_current was already a plain assignment). The queue's move up, move
down and remove were three copies of guard / optimistic local change /
background write / log; PodcastQueueView::edit is that sequence once. Same
behavior and log lines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Greg ShuflinandClaude Opus 5.5 committed 2026-09-28 14:59:30 -07:00
1 parent 3fcac3eaa2
commit 7983eb85be
3 files changed
+57 -65

No files matched your search

+7 -16
View File
@@ -372,13 +372,11 @@ impl PodcastDetailView {
fn refresh_local_marks(&mut self, fetched: &[EpisodeRow]) {
let saved: std::collections::HashMap<&str, bool> =
fetched.iter().map(|r| (r.api_id.as_str(), r.saved_locally)).collect();
let mut rows = self.episodes.current().clone();
for row in &mut rows {
for row in self.episodes.current_mut() {
if let Some(&saved) = saved.get(row.api_id.as_str()) {
row.saved_locally = saved;
}
}
self.episodes.set_current(rows);
}
/// Whether the user is plainly not in the middle of reading something: no
@@ -400,35 +398,28 @@ impl PodcastDetailView {
/// the user's own act on a row they can see — and the re-read that follows
/// their write agrees with it, so it raises no alert of its own.
fn set_in_queue(&mut self, document_id: &DocumentID, in_queue: bool) {
let mut rows = self.episodes.current().clone();
let Some(row) = rows.iter_mut().find(|r| r.document_id.as_ref() == Some(document_id))
else {
return;
};
row.in_queue = in_queue;
self.episodes.set_current(rows);
let rows = self.episodes.current_mut();
if let Some(row) = rows.iter_mut().find(|r| r.document_id.as_ref() == Some(document_id)) {
row.in_queue = in_queue;
}
}
/// Set one row's `saving` flag where it is drawn, so the control it owns
/// says so while the download runs.
fn set_saving(&mut self, api_id: &str, saving: bool) {
let mut rows = self.episodes.current().clone();
if let Some(row) = rows.iter_mut().find(|r| r.api_id == api_id) {
if let Some(row) = self.episodes.current_mut().iter_mut().find(|r| r.api_id == api_id) {
row.saving = saving;
}
self.episodes.set_current(rows);
}
/// Set one row's saved mark where it is drawn — the optimistic half of a
/// finished save or a removal, so the row stops claiming the old answer
/// before the next store read comes round.
fn set_saved(&mut self, api_id: &str, saved: bool) {
let mut rows = self.episodes.current().clone();
if let Some(row) = rows.iter_mut().find(|r| r.api_id == api_id) {
if let Some(row) = self.episodes.current_mut().iter_mut().find(|r| r.api_id == api_id) {
row.saved_locally = saved;
row.saving = false;
}
self.episodes.set_current(rows);
}
/// Download an episode's audio and keep it, as a tracked job — see
+44 -49
View File
@@ -7,6 +7,7 @@
//! another node while this screen is open is held aside and announced
//! rather than silently reshuffling rows the user might be about to click.
use std::fmt::Display;
use std::sync::Arc;
use gpui::{
@@ -144,73 +145,67 @@ impl PodcastQueueView {
cx.notify();
}
/// Move `episode_id` one slot toward the front: an optimistic local swap
/// for instant feedback, then the real write in the background. A no-op
/// while a sync alert is pending — the user must resolve it first,
/// rather than reordering a view that's about to be replaced.
/// Move `episode_id` one slot toward the front — see [`Self::edit`].
fn move_up(&mut self, episode_id: DocumentID, cx: &mut Context<Self>) {
if self.entries.is_pending() {
return;
}
let mut rows = self.entries.current().clone();
if let Some(ix) = rows.iter().position(|r| *r.episode_id() == episode_id)
&& ix > 0
{
rows.swap(ix, ix - 1);
}
self.entries.set_current(rows);
cx.notify();
let store = Arc::clone(&self.store);
let node = self.node;
cx.background_spawn(async move {
if let Err(e) = podcast_queue::move_up(&store, node, &episode_id) {
log::error!("Failed to move podcast queue entry up: {e}");
let local = |rows: &mut Vec<QueuedEpisodeRow>, ix: usize| {
if ix > 0 {
rows.swap(ix, ix - 1);
}
})
.detach();
};
self.edit(episode_id, local, podcast_queue::move_up, "move podcast queue entry up", cx);
}
/// [`Self::move_up`], toward the back.
fn move_down(&mut self, episode_id: DocumentID, cx: &mut Context<Self>) {
if self.entries.is_pending() {
return;
}
let mut rows = self.entries.current().clone();
if let Some(ix) = rows.iter().position(|r| *r.episode_id() == episode_id)
&& ix + 1 < rows.len()
{
rows.swap(ix, ix + 1);
}
self.entries.set_current(rows);
cx.notify();
let store = Arc::clone(&self.store);
let node = self.node;
cx.background_spawn(async move {
if let Err(e) = podcast_queue::move_down(&store, node, &episode_id) {
log::error!("Failed to move podcast queue entry down: {e}");
let local = |rows: &mut Vec<QueuedEpisodeRow>, ix: usize| {
if ix + 1 < rows.len() {
rows.swap(ix, ix + 1);
}
})
.detach();
};
self.edit(episode_id, local, podcast_queue::move_down, "move podcast queue entry down", cx);
}
/// Remove `episode_id` from the queue: optimistic local removal, then the
/// real write in the background. Also a no-op while an alert is pending.
/// Take `episode_id` out of the queue — see [`Self::edit`].
fn remove(&mut self, episode_id: DocumentID, cx: &mut Context<Self>) {
let local = |rows: &mut Vec<QueuedEpisodeRow>, ix: usize| {
rows.remove(ix);
};
self.edit(
episode_id,
local,
podcast_queue::remove_from_queue,
"remove podcast queue entry",
cx,
);
}
/// One queue edit: `local` applies it to the row at its index, optimistically
/// for instant feedback, then `write` makes it real in the background (a
/// failure is logged as "Failed to {what}"). A no-op while a sync alert is
/// pending — the user must resolve it first, rather than reordering a view
/// that's about to be replaced.
fn edit<E: Display + 'static>(
&mut self,
episode_id: DocumentID,
local: impl FnOnce(&mut Vec<QueuedEpisodeRow>, usize),
write: fn(&Store, NodePubkey, &DocumentID) -> Result<(), E>,
what: &'static str,
cx: &mut Context<Self>,
) {
if self.entries.is_pending() {
return;
}
let mut rows = self.entries.current().clone();
rows.retain(|r| *r.episode_id() != episode_id);
self.entries.set_current(rows);
let rows = self.entries.current_mut();
if let Some(ix) = rows.iter().position(|r| *r.episode_id() == episode_id) {
local(rows, ix);
}
cx.notify();
let store = Arc::clone(&self.store);
let node = self.node;
cx.background_spawn(async move {
if let Err(e) = podcast_queue::remove_from_queue(&store, node, &episode_id) {
log::error!("Failed to remove podcast queue entry: {e}");
if let Err(e) = write(&store, node, &episode_id) {
log::error!("Failed to {what}: {e}");
}
})
.detach();
@@ -78,6 +78,12 @@ impl<T: Clone + PartialEq> SyncGuarded<T> {
self.current = value;
}
/// [`set_current`](Self::set_current) in place — for an optimistic edit
/// to one row of a list, without cloning the rest.
pub fn current_mut(&mut self) -> &mut T {
&mut self.current
}
/// "Reload": adopt `incoming` as `current`. Returns `false` if there was
/// nothing pending.
pub fn adopt(&mut self) -> bool {