From ccce78af6640de0a5239a37e65d881ecc04952c1 Mon Sep 17 00:00:00 2001 From: Thomas <48214567+ThomasFrans@users.noreply.github.com> Date: Sun, 11 Dec 2022 19:09:22 +0100 Subject: [PATCH] Convert `main` from `async` to `sync` again * Make entry point synchronous and switch to global runtime instead. * Switch all futures::block_on() to global runtime. * Fix formatting. --- src/main.rs | 11 +++++++++-- src/spotify.rs | 31 ++++++++++++++----------------- src/spotify_api.rs | 3 ++- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/main.rs b/src/main.rs index ed79636..707699b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -117,8 +117,15 @@ struct UserDataInner { pub cmd: CommandManager, } -#[tokio::main] -async fn main() -> Result<(), String> { +lazy_static!( + /// The global Tokio runtime for running asynchronous tasks. + static ref ASYNC_RUNTIME: tokio::runtime::Runtime = tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .unwrap(); +); + +fn main() -> Result<(), String> { register_backtrace_panic_handler(); let backends = { diff --git a/src/spotify.rs b/src/spotify.rs index d568de6..edb864f 100644 --- a/src/spotify.rs +++ b/src/spotify.rs @@ -28,6 +28,7 @@ use crate::events::{Event, EventManager}; use crate::model::playable::Playable; use crate::spotify_api::WebApi; use crate::spotify_worker::{Worker, WorkerCommand}; +use crate::ASYNC_RUNTIME; pub const VOLUME_PERCENT: u16 = ((u16::max_value() as f64) * 1.0 / 100.0) as u16; @@ -72,7 +73,7 @@ impl Spotify { let (user_tx, user_rx) = oneshot::channel(); spotify.start_worker(Some(user_tx)); - spotify.user = futures::executor::block_on(user_rx).ok(); + spotify.user = ASYNC_RUNTIME.block_on(user_rx).ok(); let volume = cfg.state().volume; spotify.set_volume(volume); @@ -96,19 +97,15 @@ impl Spotify { let events = self.events.clone(); let volume = self.volume(); let credentials = self.credentials.clone(); - let handle = tokio::runtime::Handle::current(); - handle.spawn(async move { - Self::worker( - worker_channel, - events, - rx, - cfg.clone(), - credentials, - user_tx, - volume, - ) - .await - }); + ASYNC_RUNTIME.spawn(Self::worker( + worker_channel, + events, + rx, + cfg, + credentials, + user_tx, + volume, + )); } } @@ -126,9 +123,9 @@ impl Spotify { pub fn test_credentials(credentials: Credentials) -> Result { let config = Self::session_config(); - let handle = tokio::runtime::Handle::current(); - let jh = handle.spawn(async { Session::connect(config, credentials, None, true).await }); - futures::executor::block_on(jh).unwrap().map(|r| r.0) + ASYNC_RUNTIME + .block_on(Session::connect(config, credentials, None, true)) + .map(|r| r.0) } async fn create_session( diff --git a/src/spotify_api.rs b/src/spotify_api.rs index 50d437c..3fdf7a3 100644 --- a/src/spotify_api.rs +++ b/src/spotify_api.rs @@ -7,6 +7,7 @@ use crate::model::playlist::Playlist; use crate::model::track::Track; use crate::spotify_worker::WorkerCommand; use crate::ui::pagination::{ApiPage, ApiResult}; +use crate::ASYNC_RUNTIME; use chrono::{DateTime, Duration as ChronoDuration, Utc}; use futures::channel::oneshot; use log::{debug, error, info}; @@ -78,7 +79,7 @@ impl WebApi { .as_ref() { channel.send(cmd).expect("can't send message to worker"); - let token_option = futures::executor::block_on(token_rx).unwrap(); + let token_option = ASYNC_RUNTIME.block_on(token_rx).unwrap(); if let Some(token) = token_option { *self.api.token.lock().expect("can't writelock api token") = Some(Token { access_token: token.access_token,