From cb96f46e51161fc45cec154be2f587ec09ec46db Mon Sep 17 00:00:00 2001 From: Thomas Frans Date: Sat, 6 Jan 2024 12:00:05 +0100 Subject: [PATCH] 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 --- src/spotify_api.rs | 7 +++---- src/spotify_worker.rs | 11 ++++------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/spotify_api.rs b/src/spotify_api.rs index 6d0042a..9c306f7 100644 --- a/src/spotify_api.rs +++ b/src/spotify_api.rs @@ -1,4 +1,3 @@ -use crate::application::ASYNC_RUNTIME; use crate::model::album::Album; use crate::model::artist::Artist; use crate::model::category::Category; @@ -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; @@ -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(); @@ -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 @@ -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, diff --git a/src/spotify_worker.rs b/src/spotify_worker.rs index 62ce781..ff92074 100644 --- a/src/spotify_worker.rs +++ b/src/spotify_worker.rs @@ -3,7 +3,6 @@ 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; @@ -11,6 +10,7 @@ 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; @@ -26,7 +26,7 @@ pub(crate) enum WorkerCommand { Stop, Seek(u32), SetVolume(u16), - RequestToken(oneshot::Sender>), + RequestToken(Sender>), Preload(Playable), Shutdown, } @@ -63,10 +63,7 @@ impl Worker { } } - fn get_token( - &self, - sender: oneshot::Sender>, - ) -> Pin + Send>> { + fn get_token(&self, sender: Sender>) -> Pin + 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 = @@ -85,7 +82,7 @@ impl Worker { Some(token) }) }) - .map(|result| sender.send(result).unwrap()), + .map(move |result| sender.send(result).unwrap()), ) }