Skip to content

Commit

Permalink
chore: remove unnecessary asynchronous channel
Browse files Browse the repository at this point in the history
* chore: remove unnecessary asynchronous channel

The channel does not need to be asynchronous as the receiving end is
used in a blocking manner and the sending end of a regular channel never
blocks.

* Fix broken merge

---------

Co-authored-by: Henrik Friedrichsen <henrik@affekt.org>
  • Loading branch information
ThomasFrans and hrkfdn authored Jan 6, 2024
1 parent 4eff37a commit cb96f46
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
7 changes: 3 additions & 4 deletions src/spotify_api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::application::ASYNC_RUNTIME;
use crate::model::album::Album;
use crate::model::artist::Artist;
use crate::model::category::Category;
Expand All @@ -9,7 +8,6 @@ use crate::model::track::Track;
use crate::spotify_worker::WorkerCommand;
use crate::ui::pagination::{ApiPage, ApiResult};
use chrono::{DateTime, Duration as ChronoDuration, Utc};
use futures::channel::oneshot;
use log::{debug, error, info};

use rspotify::http::HttpError;
Expand Down Expand Up @@ -71,6 +69,7 @@ impl WebApi {
self.worker_channel = channel;
}

/// Update the authentication token when it expires.
pub fn update_token(&self) {
{
let token_expiration = self.token_expiration.read().unwrap();
Expand All @@ -85,7 +84,7 @@ impl WebApi {
info!("Token will expire in {}, renewing", delta);
}

let (token_tx, token_rx) = oneshot::channel();
let (token_tx, token_rx) = std::sync::mpsc::channel();
let cmd = WorkerCommand::RequestToken(token_tx);
if let Some(channel) = self
.worker_channel
Expand All @@ -94,7 +93,7 @@ impl WebApi {
.as_ref()
{
channel.send(cmd).expect("can't send message to worker");
let token_option = ASYNC_RUNTIME.get().unwrap().block_on(token_rx).unwrap();
let token_option = token_rx.recv().unwrap();
if let Some(token) = token_option {
*self.api.token.lock().expect("can't writelock api token") = Some(Token {
access_token: token.access_token,
Expand Down
11 changes: 4 additions & 7 deletions src/spotify_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use crate::events::{Event, EventManager};
use crate::model::playable::Playable;
use crate::queue::QueueEvent;
use crate::spotify::PlayerEvent;
use futures::channel::oneshot;
use futures::{Future, FutureExt};
use librespot_core::keymaster::Token;
use librespot_core::session::Session;
use librespot_core::spotify_id::{SpotifyAudioType, SpotifyId};
use librespot_playback::mixer::Mixer;
use librespot_playback::player::{Player, PlayerEvent as LibrespotPlayerEvent};
use log::{debug, error, info, warn};
use std::sync::mpsc::Sender;
use std::time::Duration;
use std::{pin::Pin, time::SystemTime};
use tokio::sync::mpsc;
Expand All @@ -26,7 +26,7 @@ pub(crate) enum WorkerCommand {
Stop,
Seek(u32),
SetVolume(u16),
RequestToken(oneshot::Sender<Option<Token>>),
RequestToken(Sender<Option<Token>>),
Preload(Playable),
Shutdown,
}
Expand Down Expand Up @@ -63,10 +63,7 @@ impl Worker {
}
}

fn get_token(
&self,
sender: oneshot::Sender<Option<Token>>,
) -> Pin<Box<dyn Future<Output = ()> + Send>> {
fn get_token(&self, sender: Sender<Option<Token>>) -> Pin<Box<dyn Future<Output = ()> + Send>> {
let client_id = config::CLIENT_ID;
let scopes = "user-read-private,playlist-read-private,playlist-read-collaborative,playlist-modify-public,playlist-modify-private,user-follow-modify,user-follow-read,user-library-read,user-library-modify,user-top-read,user-read-recently-played";
let url =
Expand All @@ -85,7 +82,7 @@ impl Worker {
Some(token)
})
})
.map(|result| sender.send(result).unwrap()),
.map(move |result| sender.send(result).unwrap()),
)
}

Expand Down

0 comments on commit cb96f46

Please sign in to comment.