chore: remove unnecessary asynchronous channel

* 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>
This commit is contained in:
Thomas Frans
2024-01-06 12:00:05 +01:00
committed by GitHub
parent 4eff37a3ed
commit cb96f46e51
2 changed files with 7 additions and 11 deletions

View File

@@ -1,4 +1,3 @@
use crate::application::ASYNC_RUNTIME;
use crate::model::album::Album; use crate::model::album::Album;
use crate::model::artist::Artist; use crate::model::artist::Artist;
use crate::model::category::Category; use crate::model::category::Category;
@@ -9,7 +8,6 @@ use crate::model::track::Track;
use crate::spotify_worker::WorkerCommand; use crate::spotify_worker::WorkerCommand;
use crate::ui::pagination::{ApiPage, ApiResult}; use crate::ui::pagination::{ApiPage, ApiResult};
use chrono::{DateTime, Duration as ChronoDuration, Utc}; use chrono::{DateTime, Duration as ChronoDuration, Utc};
use futures::channel::oneshot;
use log::{debug, error, info}; use log::{debug, error, info};
use rspotify::http::HttpError; use rspotify::http::HttpError;
@@ -71,6 +69,7 @@ impl WebApi {
self.worker_channel = channel; self.worker_channel = channel;
} }
/// Update the authentication token when it expires.
pub fn update_token(&self) { pub fn update_token(&self) {
{ {
let token_expiration = self.token_expiration.read().unwrap(); let token_expiration = self.token_expiration.read().unwrap();
@@ -85,7 +84,7 @@ impl WebApi {
info!("Token will expire in {}, renewing", delta); 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); let cmd = WorkerCommand::RequestToken(token_tx);
if let Some(channel) = self if let Some(channel) = self
.worker_channel .worker_channel
@@ -94,7 +93,7 @@ impl WebApi {
.as_ref() .as_ref()
{ {
channel.send(cmd).expect("can't send message to worker"); 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 { if let Some(token) = token_option {
*self.api.token.lock().expect("can't writelock api token") = Some(Token { *self.api.token.lock().expect("can't writelock api token") = Some(Token {
access_token: token.access_token, access_token: token.access_token,

View File

@@ -3,7 +3,6 @@ use crate::events::{Event, EventManager};
use crate::model::playable::Playable; use crate::model::playable::Playable;
use crate::queue::QueueEvent; use crate::queue::QueueEvent;
use crate::spotify::PlayerEvent; use crate::spotify::PlayerEvent;
use futures::channel::oneshot;
use futures::{Future, FutureExt}; use futures::{Future, FutureExt};
use librespot_core::keymaster::Token; use librespot_core::keymaster::Token;
use librespot_core::session::Session; use librespot_core::session::Session;
@@ -11,6 +10,7 @@ use librespot_core::spotify_id::{SpotifyAudioType, SpotifyId};
use librespot_playback::mixer::Mixer; use librespot_playback::mixer::Mixer;
use librespot_playback::player::{Player, PlayerEvent as LibrespotPlayerEvent}; use librespot_playback::player::{Player, PlayerEvent as LibrespotPlayerEvent};
use log::{debug, error, info, warn}; use log::{debug, error, info, warn};
use std::sync::mpsc::Sender;
use std::time::Duration; use std::time::Duration;
use std::{pin::Pin, time::SystemTime}; use std::{pin::Pin, time::SystemTime};
use tokio::sync::mpsc; use tokio::sync::mpsc;
@@ -26,7 +26,7 @@ pub(crate) enum WorkerCommand {
Stop, Stop,
Seek(u32), Seek(u32),
SetVolume(u16), SetVolume(u16),
RequestToken(oneshot::Sender<Option<Token>>), RequestToken(Sender<Option<Token>>),
Preload(Playable), Preload(Playable),
Shutdown, Shutdown,
} }
@@ -63,10 +63,7 @@ impl Worker {
} }
} }
fn get_token( fn get_token(&self, sender: Sender<Option<Token>>) -> Pin<Box<dyn Future<Output = ()> + Send>> {
&self,
sender: oneshot::Sender<Option<Token>>,
) -> Pin<Box<dyn Future<Output = ()> + Send>> {
let client_id = config::CLIENT_ID; 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 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 = let url =
@@ -85,7 +82,7 @@ impl Worker {
Some(token) Some(token)
}) })
}) })
.map(|result| sender.send(result).unwrap()), .map(move |result| sender.send(result).unwrap()),
) )
} }