mirror of
https://github.com/talwat/lowfi
synced 2024-12-26 03:01:55 +00:00
chore: improve readability of code relating to downloader thread
This commit is contained in:
parent
5b546ea2de
commit
6f7c2dbcba
@ -221,7 +221,7 @@ impl Player {
|
||||
|
||||
// Notify the background downloader that there's an empty spot
|
||||
// in the buffer.
|
||||
itx.send(()).await?;
|
||||
Downloader::notify(&itx).await?;
|
||||
|
||||
// Notify the audio server that the next song has actually been downloaded.
|
||||
tx.send(Messages::NewSong).await?
|
||||
@ -249,11 +249,11 @@ impl Player {
|
||||
mut rx: Receiver<Messages>,
|
||||
) -> eyre::Result<()> {
|
||||
// `itx` is used to notify the `Downloader` when it needs to download new tracks.
|
||||
let (downloader, itx) = Downloader::new(player.clone());
|
||||
let downloader = downloader.start().await;
|
||||
let downloader = Downloader::new(player.clone());
|
||||
let (itx, downloader) = downloader.start().await;
|
||||
|
||||
// Start buffering tracks immediately.
|
||||
itx.send(()).await?;
|
||||
Downloader::notify(&itx).await?;
|
||||
|
||||
// Set the initial sink volume to the one specified.
|
||||
player.sink.set_volume(properties.volume as f32 / 100.0);
|
||||
|
@ -21,32 +21,45 @@ pub struct Downloader {
|
||||
/// The internal reciever, which is used by the downloader to know
|
||||
/// when to begin downloading more tracks.
|
||||
rx: Receiver<()>,
|
||||
|
||||
/// A copy of the internal sender, which can be useful for keeping
|
||||
/// track of it.
|
||||
tx: Sender<()>,
|
||||
}
|
||||
|
||||
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].
|
||||
///
|
||||
/// This also sends a [`Sender`] which can be used to notify
|
||||
/// 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);
|
||||
(Self { player, rx }, tx)
|
||||
Self { player, rx, tx }
|
||||
}
|
||||
|
||||
/// Actually starts & consumes the [Downloader].
|
||||
pub async fn start(mut self) -> JoinHandle<()> {
|
||||
task::spawn(async move {
|
||||
// Loop through each update notification.
|
||||
while self.rx.recv().await == Some(()) {
|
||||
// For each update notification, we'll push tracks until the buffer is completely full.
|
||||
while self.player.tracks.read().await.len() < BUFFER_SIZE {
|
||||
let Ok(track) = Track::random(&self.player.client).await else {
|
||||
continue;
|
||||
};
|
||||
pub async fn start(mut self) -> (Sender<()>, JoinHandle<()>) {
|
||||
(
|
||||
self.tx,
|
||||
task::spawn(async move {
|
||||
// Loop through each update notification.
|
||||
while self.rx.recv().await == Some(()) {
|
||||
// For each update notification, we'll push tracks until the buffer is completely full.
|
||||
while self.player.tracks.read().await.len() < BUFFER_SIZE {
|
||||
let Ok(track) = Track::random(&self.player.client).await else {
|
||||
continue;
|
||||
};
|
||||
|
||||
self.player.tracks.write().await.push_back(track);
|
||||
self.player.tracks.write().await.push_back(track);
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -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.
|
||||
async fn interface(player: Arc<Player>, minimalist: bool) -> eyre::Result<()> {
|
||||
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 volume = player.sink.volume();
|
||||
|
Loading…
Reference in New Issue
Block a user