gui: hold Tasks instead of generation counters in podcasts and pomodoro

Podcast search (debounce + in-flight search), the podcast detail's episode
fetch, and Pomodoro's tick loop each bumped a u64 and had the stale task
check it before applying. Replacing a held Option<Task<()>> drops, and so
cancels, the task it supersedes, which is the same thing with less
bookkeeping (audit P8). Documents' page fetches keep their counter: load_more
shares it without superseding, which one Task slot can't express.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Greg ShuflinandClaude Opus 5.5 committed 2026-09-28 13:36:49 -07:00
1 parent 8736a5c9ee
commit 71948f5db2
3 files changed
+32 -58

No files matched your search

+5 -11
View File
@@ -207,8 +207,8 @@ pub struct PodcastDetailView {
episodes_error: Option<SharedString>,
/// `Some` when an episode detail screen is open, drawn over this screen.
episode: Option<Entity<EpisodeDetailView>>,
/// Bumped on each episode fetch; a stale in-flight fetch discards itself.
generation: u64,
/// The in-flight episode fetch; replacing it drops (cancels) a stale one.
load_episodes: Option<Task<()>>,
/// Reads the body's scroll offset, which is half of "is the user in the
/// middle of something" — see [`Self::settled`].
scroll: ScrollHandle,
@@ -283,7 +283,7 @@ impl PodcastDetailView {
episodes_loading: false,
episodes_error: None,
episode: None,
generation: 0,
load_episodes: None,
scroll: ScrollHandle::new(),
nav_sidebar: nav_sidebar.clone(),
job_runner,
@@ -596,21 +596,16 @@ impl PodcastDetailView {
/// Load the feed's episodes on the background executor, superseding any
/// earlier in-flight load.
fn load_episodes(&mut self, cx: &mut Context<Self>) {
self.generation += 1;
let generation = self.generation;
self.episodes_loading = true;
self.episodes_error = None;
cx.notify();
let store = Arc::clone(&self.store);
let id = self.meta.id;
spawn_then(
self.load_episodes = Some(spawn_then(
cx,
move || episodes_for_feed(&store, id),
move |this, result, _| {
if this.generation != generation {
return;
}
this.episodes_loading = false;
match result {
// A fresh guard rather than `set_current`: this list was
@@ -623,8 +618,7 @@ impl PodcastDetailView {
}
}
},
)
.detach();
));
}
}
+13 -28
View File
@@ -13,8 +13,8 @@ use std::sync::Arc;
use std::time::Duration;
use gpui::{
Context, Entity, EventEmitter, FontWeight, IntoElement, Render, SharedString, Window, div,
prelude::*, px,
Context, Entity, EventEmitter, FontWeight, IntoElement, Render, SharedString, Task, Window,
div, prelude::*, px,
};
use synchronicity::agents::podcast::{PodcastSearchResponse, search_podcasts};
use synchronicity::node::NodePubkey;
@@ -86,10 +86,10 @@ pub struct PodcastSearchView {
subscribed: HashSet<i64>,
/// Ids with a subscribe in flight, so the button reads "Subscribing…".
subscribing: HashSet<i64>,
/// Bumped on every query change and every search launch; a debounced or
/// in-flight search whose generation no longer matches has been superseded
/// and discards itself. This is how typing cancels an earlier pending search.
generation: u64,
/// The pending debounce or in-flight search. Replaced on every query
/// change and every search launch, which drops (cancels) the one it
/// supersedes — this is how typing cancels an earlier pending search.
search: Option<Task<()>>,
}
impl EventEmitter<PodcastSearchEvent> for PodcastSearchView {}
@@ -115,7 +115,7 @@ impl PodcastSearchView {
results: Vec::new(),
subscribed: HashSet::new(),
subscribing: HashSet::new(),
generation: 0,
search: None,
}
}
@@ -168,25 +168,17 @@ impl PodcastSearchView {
/// A keystroke landed: cancel any pending search, then schedule a fresh
/// debounced one if the query is long enough.
fn on_query_changed(&mut self, cx: &mut Context<Self>) {
self.generation += 1;
let generation = self.generation;
self.search = None;
cx.notify();
if self.query(cx).chars().count() < MIN_QUERY_LEN {
return;
}
cx.spawn(async move |this, cx| {
self.search = Some(cx.spawn(async move |this, cx| {
cx.background_executor().timer(DEBOUNCE).await;
this.update(cx, |this, cx| {
// Only fire if no newer keystroke or search superseded us.
if this.generation == generation {
this.search_now(cx);
}
})
.ok();
})
.detach();
this.update(cx, |this, cx| this.search_now(cx)).ok();
}));
}
/// Run a search immediately (Search button / Enter), canceling any pending
@@ -197,8 +189,6 @@ impl PodcastSearchView {
return;
}
self.generation += 1;
let generation = self.generation;
self.searching = true;
self.error = None;
cx.notify();
@@ -208,7 +198,7 @@ impl PodcastSearchView {
// background executor with the rest rather than to the tokio runtime.
let store = Arc::clone(&self.store);
spawn_then(
self.search = Some(spawn_then(
cx,
move || {
search_podcasts(&store, &query).map_err(|e| e.to_string()).and_then(|json| {
@@ -217,10 +207,6 @@ impl PodcastSearchView {
})
},
move |this, result, cx| {
// A newer search was launched while this one was in flight; drop it.
if this.generation != generation {
return;
}
this.searching = false;
match result {
Ok(response) => {
@@ -248,8 +234,7 @@ impl PodcastSearchView {
}
}
},
)
.detach();
));
}
}
+14 -19
View File
@@ -15,7 +15,8 @@
use std::time::{Duration, Instant};
use gpui::{
Context, Entity, FontWeight, IntoElement, Render, SharedString, Window, div, prelude::*, px,
Context, Entity, FontWeight, IntoElement, Render, SharedString, Task, Window, div, prelude::*,
px,
};
use synchronicity::agents::AgentId;
use synchronicity::agents::pomodoro::{PomodoroPhase, PomodoroTimer};
@@ -55,10 +56,10 @@ pub struct PomodoroView {
/// something the user is sitting and watching would interrupt the break it
/// is announcing.
just_completed: Option<PomodoroPhase>,
/// Identifies the currently-live tick loop. Bumped whenever the timer is
/// started or stopped, so a loop from a previous run exits at its next wake
/// instead of double-advancing the clock alongside the new one.
tick_generation: u64,
/// The live tick loop. Replaced whenever the timer is started or stopped,
/// which drops (cancels) a loop from a previous run instead of letting it
/// double-advance the clock alongside the new one.
tick: Option<Task<()>>,
/// The rail's right-hand slot for this agent: the countdown, so a running
/// timer is legible from any other screen (ADR 0032 §6).
nav_status: Entity<NavStatus>,
@@ -70,7 +71,7 @@ impl PomodoroView {
timer: PomodoroTimer::default(),
last_tick: None,
just_completed: None,
tick_generation: 0,
tick: None,
nav_status,
}
}
@@ -87,37 +88,31 @@ impl PomodoroView {
/// rail slot, and repaint.
fn sync_clock(&mut self, cx: &mut Context<Self>) {
// Any in-flight loop belongs to the previous run; retire it either way.
self.tick_generation += 1;
if self.timer.running {
self.last_tick = Some(Instant::now());
self.spawn_tick_loop(self.tick_generation, cx);
} else {
self.last_tick = None;
}
self.last_tick = self.timer.running.then(Instant::now);
self.tick = self.timer.running.then(|| Self::spawn_tick_loop(cx));
self.publish_slot(cx);
cx.notify();
}
/// Wake every [`TICK_INTERVAL`] and advance the clock until the timer stops
/// or a newer loop supersedes this one.
fn spawn_tick_loop(&self, generation: u64, cx: &mut Context<Self>) {
/// or the loop is dropped.
fn spawn_tick_loop(cx: &mut Context<Self>) -> Task<()> {
cx.spawn(async move |this, cx| {
loop {
cx.background_executor().timer(TICK_INTERVAL).await;
let still_running =
this.update(cx, |this, cx| this.advance_clock(generation, cx)).unwrap_or(false);
this.update(cx, |this, cx| this.advance_clock(cx)).unwrap_or(false);
if !still_running {
return;
}
}
})
.detach();
}
/// Charge the timer for the real time since the last wake. Returns whether
/// this loop should keep going.
fn advance_clock(&mut self, generation: u64, cx: &mut Context<Self>) -> bool {
if generation != self.tick_generation || !self.timer.running {
fn advance_clock(&mut self, cx: &mut Context<Self>) -> bool {
if !self.timer.running {
return false;
}
let now = Instant::now();