Sync podcast state with devices on protocol v4
This client now announces device-sync protocol 4. Podcast operations are applied to the podcast store, kept in the log and relayed; those that arrive before the store opens wait in sync_podcast_pending. Snapshots carry the podcast sections, bounded by the shared limits and by the line budget of each message, and are merged after the operations. Operations, snapshot sections and playback state that need v4 are only sent to peers that reported v4. Periodic progress checkpoints are coalesced in the log, and the Podcasts tab reloads after changes from other devices. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4c3edbcd12
commit
79372e00fc
9 files changed
+1281
-45
No files matched your search
@@ -208,6 +208,13 @@ pub enum AppEvent {
|
||||
JamInvite(Result<String, String>),
|
||||
/// Result of joining a Jam capability.
|
||||
JamJoined(Result<String, String>),
|
||||
/// Received podcast operations or snapshot sections changed the podcast
|
||||
/// store: `progress` lists changed progress registers, `changed` means
|
||||
/// anything else (subscriptions, feeds, sessions).
|
||||
PodcastsSynced {
|
||||
progress: Vec<music_dht::podcast::ProgressRecord>,
|
||||
changed: bool,
|
||||
},
|
||||
/// The podcast store opened (with the feed client), or why it did not.
|
||||
PodcastStoreOpened(Result<crate::podcasts::refresh::Services, String>),
|
||||
/// Subscribed podcasts reloaded from the podcast store.
|
||||
|
||||
@@ -3442,6 +3442,7 @@ fn handle_app_event(state: &mut AppState, runtime: &mut Runtime, event: AppEvent
|
||||
match event {
|
||||
// Folded in by podcast_runtime::handle_event above.
|
||||
AppEvent::PodcastStoreOpened(_)
|
||||
| AppEvent::PodcastsSynced { .. }
|
||||
| AppEvent::PodcastsLoaded(_)
|
||||
| AppEvent::PodcastSeasonsLoaded { .. }
|
||||
| AppEvent::PodcastEpisodesLoaded { .. }
|
||||
|
||||
@@ -261,6 +261,16 @@ pub fn handle_event(
|
||||
})
|
||||
.inspect_err(|err| tracing::warn!("{err:#}"))
|
||||
.ok();
|
||||
// Device sync applies received podcast operations and
|
||||
// snapshot sections to the store from now on (and the ones
|
||||
// that arrived before it opened).
|
||||
let devices = Arc::clone(&runtime.devices);
|
||||
let store = Arc::clone(&services.store);
|
||||
tokio::task::spawn_blocking(move || {
|
||||
if let Err(err) = devices.attach_podcast_store(store) {
|
||||
tracing::warn!("podcast sync: {err:#}");
|
||||
}
|
||||
});
|
||||
runtime.podcasts = Some(PodcastRuntime {
|
||||
services,
|
||||
refresh_slots: Arc::new(tokio::sync::Semaphore::new(MAX_CONCURRENT_REFRESHES)),
|
||||
@@ -302,6 +312,14 @@ pub fn handle_event(
|
||||
.into_iter()
|
||||
.collect(),
|
||||
AppEvent::PodcastProgress(event) => progress_event(state, runtime, event),
|
||||
AppEvent::PodcastsSynced { progress, changed } => {
|
||||
if changed {
|
||||
// A subscription that came from another device has no
|
||||
// episodes here until its feed is fetched: check now.
|
||||
let _ = runtime.event_tx.send(AppEvent::PodcastRefreshTick);
|
||||
}
|
||||
podcasts::synced(state, progress, changed)
|
||||
}
|
||||
AppEvent::PodcastEpisodeStarted {
|
||||
generation,
|
||||
episode,
|
||||
|
||||
@@ -270,7 +270,8 @@ pub fn restart_current(state: &mut AppState) -> Option<Effect> {
|
||||
}
|
||||
|
||||
/// The progress writer saved (or found) a progress register: show it in
|
||||
/// the episode list without reloading it.
|
||||
/// the episode list without reloading it, and carry its generation in the
|
||||
/// queue items of that episode (a handoff passes it on).
|
||||
pub fn progress_saved(state: &mut AppState, record: ProgressRecord) -> Vec<Effect> {
|
||||
if let Some(list) = state.podcasts.episodes.as_mut() {
|
||||
for item in &mut list.items {
|
||||
@@ -279,9 +280,46 @@ pub fn progress_saved(state: &mut AppState, record: ProgressRecord) -> Vec<Effec
|
||||
}
|
||||
}
|
||||
}
|
||||
let player = &mut state.player;
|
||||
for track in player.queue.iter_mut().chain(player.current.as_mut()) {
|
||||
if let Some(episode) = track.podcast.as_mut()
|
||||
&& episode.episode_id == record.episode_id
|
||||
{
|
||||
episode.progress_generation = Some(
|
||||
episode
|
||||
.progress_generation
|
||||
.unwrap_or(0)
|
||||
.max(record.generation),
|
||||
);
|
||||
}
|
||||
}
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
/// Podcast state changed by other devices (received operations or
|
||||
/// snapshot sections): progress registers update the list in place, the
|
||||
/// rest reloads what is on screen.
|
||||
pub fn synced(state: &mut AppState, progress: Vec<ProgressRecord>, changed: bool) -> Vec<Effect> {
|
||||
let mut effects = Vec::new();
|
||||
for record in progress {
|
||||
effects.extend(progress_saved(state, record));
|
||||
}
|
||||
if state.podcasts.store != PodcastStoreStatus::Ready {
|
||||
return effects;
|
||||
}
|
||||
// Counts in the list (unplayed, in progress) follow both.
|
||||
effects.push(Effect::PodcastsReload);
|
||||
if changed {
|
||||
if let Some(opened) = &state.podcasts.opened
|
||||
&& opened.season_cursor.is_some()
|
||||
{
|
||||
effects.push(Effect::PodcastLoadSeasons(opened.podcast_id.clone()));
|
||||
}
|
||||
effects.extend(reload_episodes(state));
|
||||
}
|
||||
effects
|
||||
}
|
||||
|
||||
fn open_podcast(state: &mut AppState, index: usize) -> Option<Effect> {
|
||||
let row = state.podcasts.podcasts().get(index)?;
|
||||
let podcast_id = row.id().clone();
|
||||
|
||||
@@ -1843,6 +1843,7 @@ mod podcast_queue {
|
||||
use crate::app::podcast_fixtures::{self as fx, episode_track};
|
||||
use crate::app::state::{PODCAST_ACTION_ROWS, Popup, RepeatMode, track_content_id};
|
||||
use crate::app::update::PODCAST_NOT_MUSIC;
|
||||
use music_dht::podcast::ProgressStatus;
|
||||
|
||||
fn keys(queue: &[TrackItem]) -> Vec<String> {
|
||||
queue.iter().map(track_key).collect()
|
||||
@@ -2147,4 +2148,49 @@ mod podcast_queue {
|
||||
assert_eq!(items[1].resume_position_ms(), 90_000);
|
||||
assert_eq!(items[1].progress.as_ref().unwrap().generation, 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn saved_progress_raises_the_generation_a_handoff_carries() {
|
||||
let mut state = AppState::default();
|
||||
state.player.queue = vec![episode_track(1), episode_track(2), test_track(3)];
|
||||
state.player.current = Some(episode_track(2));
|
||||
let mut record = fx::episode(2, "Episode 2", Some((ProgressStatus::InProgress, 1)))
|
||||
.progress
|
||||
.unwrap();
|
||||
record.generation = 7;
|
||||
crate::app::update::podcasts::progress_saved(&mut state, record.clone());
|
||||
let generation = |track: &TrackItem| track.podcast.as_ref().unwrap().progress_generation;
|
||||
assert_eq!(generation(&state.player.queue[0]), None);
|
||||
assert_eq!(generation(&state.player.queue[1]), Some(7));
|
||||
assert_eq!(generation(state.player.current.as_ref().unwrap()), Some(7));
|
||||
// Never lowered by an older register.
|
||||
record.generation = 3;
|
||||
crate::app::update::podcasts::progress_saved(&mut state, record);
|
||||
assert_eq!(generation(&state.player.queue[1]), Some(7));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn podcast_changes_from_other_devices_reload_what_is_shown() {
|
||||
let mut state = episodes_tab(&[None, None]);
|
||||
let record = fx::episode(1, "Episode 1", Some((ProgressStatus::InProgress, 5_000)))
|
||||
.progress
|
||||
.unwrap();
|
||||
// A progress register is shown in place; the list counts reload.
|
||||
let effects = crate::app::update::podcasts::synced(&mut state, vec![record], false);
|
||||
assert_eq!(effects, [Effect::PodcastsReload]);
|
||||
let items = &state.podcasts.episodes.as_ref().unwrap().items;
|
||||
assert_eq!(items[0].resume_position_ms(), 5_000);
|
||||
// Anything else reloads the opened podcast too.
|
||||
let effects = crate::app::update::podcasts::synced(&mut state, Vec::new(), true);
|
||||
assert_eq!(effects[0], Effect::PodcastsReload);
|
||||
assert!(
|
||||
effects
|
||||
.iter()
|
||||
.any(|effect| matches!(effect, Effect::PodcastLoadEpisodes { .. })),
|
||||
"{effects:?}"
|
||||
);
|
||||
// Before the store is ready there is nothing to reload.
|
||||
let mut opening = AppState::default();
|
||||
assert!(crate::app::update::podcasts::synced(&mut opening, Vec::new(), true).is_empty());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user