chore: improve readability of code relating to downloader thread

This commit is contained in:
Tal 2024-10-07 17:08:37 +02:00
parent 5b546ea2de
commit 6f7c2dbcba
3 changed files with 40 additions and 19 deletions

View File

@ -221,7 +221,7 @@ impl Player {
// Notify the background downloader that there's an empty spot // Notify the background downloader that there's an empty spot
// in the buffer. // in the buffer.
itx.send(()).await?; Downloader::notify(&itx).await?;
// Notify the audio server that the next song has actually been downloaded. // Notify the audio server that the next song has actually been downloaded.
tx.send(Messages::NewSong).await? tx.send(Messages::NewSong).await?
@ -249,11 +249,11 @@ impl Player {
mut rx: Receiver<Messages>, mut rx: Receiver<Messages>,
) -> eyre::Result<()> { ) -> eyre::Result<()> {
// `itx` is used to notify the `Downloader` when it needs to download new tracks. // `itx` is used to notify the `Downloader` when it needs to download new tracks.
let (downloader, itx) = Downloader::new(player.clone()); let downloader = Downloader::new(player.clone());
let downloader = downloader.start().await; let (itx, downloader) = downloader.start().await;
// Start buffering tracks immediately. // Start buffering tracks immediately.
itx.send(()).await?; Downloader::notify(&itx).await?;
// Set the initial sink volume to the one specified. // Set the initial sink volume to the one specified.
player.sink.set_volume(properties.volume as f32 / 100.0); player.sink.set_volume(properties.volume as f32 / 100.0);

View File

@ -21,20 +21,32 @@ pub struct Downloader {
/// The internal reciever, which is used by the downloader to know /// The internal reciever, which is used by the downloader to know
/// when to begin downloading more tracks. /// when to begin downloading more tracks.
rx: Receiver<()>, rx: Receiver<()>,
/// A copy of the internal sender, which can be useful for keeping
/// track of it.
tx: Sender<()>,
} }
impl Downloader { impl Downloader {
/// Uses a sender recieved from [Sender] to notify the
/// download thread that it should resume downloading.
pub async fn notify(sender: &Sender<()>) -> Result<(), mpsc::error::SendError<()>> {
sender.send(()).await
}
/// Initializes the [Downloader]. /// Initializes the [Downloader].
/// ///
/// This also sends a [`Sender`] which can be used to notify /// This also sends a [`Sender`] which can be used to notify
/// when the downloader needs to begin downloading more tracks. /// when the downloader needs to begin downloading more tracks.
pub fn new(player: Arc<Player>) -> (Self, Sender<()>) { pub fn new(player: Arc<Player>) -> Self {
let (tx, rx) = mpsc::channel(8); let (tx, rx) = mpsc::channel(8);
(Self { player, rx }, tx) Self { player, rx, tx }
} }
/// Actually starts & consumes the [Downloader]. /// Actually starts & consumes the [Downloader].
pub async fn start(mut self) -> JoinHandle<()> { pub async fn start(mut self) -> (Sender<()>, JoinHandle<()>) {
(
self.tx,
task::spawn(async move { task::spawn(async move {
// Loop through each update notification. // Loop through each update notification.
while self.rx.recv().await == Some(()) { while self.rx.recv().await == Some(()) {
@ -47,6 +59,7 @@ impl Downloader {
self.player.tracks.write().await.push_back(track); self.player.tracks.write().await.push_back(track);
} }
} }
}) }),
)
} }
} }

View File

@ -114,7 +114,15 @@ async fn input(sender: Sender<Messages>) -> eyre::Result<()> {
/// has been displayed for, so that it's only displayed for a certain amount of frames. /// has been displayed for, so that it's only displayed for a certain amount of frames.
async fn interface(player: Arc<Player>, minimalist: bool) -> eyre::Result<()> { async fn interface(player: Arc<Player>, minimalist: bool) -> eyre::Result<()> {
loop { loop {
let action = components::action(&player, WIDTH); let action = format!(
"{} {}",
components::action(&player, WIDTH - 2),
player
.tracks
.try_read()
.and_then(|x| Ok(x.len().to_string()))
.unwrap_or(String::from("?"))
);
let timer = VOLUME_TIMER.load(Ordering::Relaxed); let timer = VOLUME_TIMER.load(Ordering::Relaxed);
let volume = player.sink.volume(); let volume = player.sink.volume();