From 8d08425ef344bb8e70a05fc1be9e8b9a9072be60 Mon Sep 17 00:00:00 2001 From: ashthespy Date: Thu, 28 Mar 2019 15:27:50 +0100 Subject: [PATCH] Refactor adding context tracks to state --- connect/src/spirc.rs | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/connect/src/spirc.rs b/connect/src/spirc.rs index b888461f..6c6504db 100644 --- a/connect/src/spirc.rs +++ b/connect/src/spirc.rs @@ -1,9 +1,9 @@ use std; use std::time::{SystemTime, UNIX_EPOCH}; -use futures::{Async, Future, Poll, Sink, Stream}; use futures::future; use futures::sync::{mpsc, oneshot}; +use futures::{Async, Future, Poll, Sink, Stream}; use protobuf::{self, Message}; use rand; use rand::seq::SliceRandom; @@ -776,26 +776,22 @@ impl SpircTask { self.context_fut = self.resolve_uri(&context.next_page_url); let new_tracks = &context.tracks; - debug!("Adding {:?} tracks from context to playlist", new_tracks.len()); - let current_index = self.state.get_playing_track_index(); - let mut new_index = 0; - { - let mut tracks = self.state.mut_track(); - // Does this need to be optimised - we don't need to actually traverse the len of tracks - let tracks_len = tracks.len(); - if tracks_len > CONTEXT_TRACKS_HISTORY { - tracks.rotate_right(tracks_len - CONTEXT_TRACKS_HISTORY); - tracks.truncate(CONTEXT_TRACKS_HISTORY); - } - // tracks.extend_from_slice(&mut new_tracks); // method doesn't exist for protobuf::RepeatedField - for t in new_tracks { - tracks.push(t.to_owned()); - } - if current_index > CONTEXT_TRACKS_HISTORY as u32 { - new_index = current_index - CONTEXT_TRACKS_HISTORY as u32; - } + debug!("Adding {:?} tracks from context to frame", new_tracks.len()); + let mut track_vec = self.state.take_track().into_vec(); + if let Some(head) = track_vec.len().checked_sub(CONTEXT_TRACKS_HISTORY) { + track_vec.drain(0..head); + } + track_vec.extend_from_slice(&new_tracks); + self.state.set_track(protobuf::RepeatedField::from_vec(track_vec)); + + // Update playing index + if let Some(new_index) = self + .state + .get_playing_track_index() + .checked_sub(CONTEXT_TRACKS_HISTORY as u32) + { + self.state.set_playing_track_index(new_index); } - self.state.set_playing_track_index(new_index); } }