From ae090b6073733adebc8c9911ec0eaa6f5fd27383 Mon Sep 17 00:00:00 2001 From: Thomas Frans Date: Tue, 23 May 2023 23:24:29 +0200 Subject: [PATCH] refactor: move async runtime to application module To have a clear distinction between code dealing with OS process characteristics and code of ncspot itself, it makes sense to move the async runtime together with ncspot as it doesn't have anything to do with the OS process. --- src/application.rs | 10 +++++++++- src/main.rs | 10 ++-------- src/mpris.rs | 2 +- src/spotify.rs | 2 +- src/spotify_api.rs | 2 +- 5 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/application.rs b/src/application.rs index 44b95a2..1a5b7a7 100644 --- a/src/application.rs +++ b/src/application.rs @@ -1,4 +1,4 @@ -use crate::{command, ipc, mpris, queue, spotify, ASYNC_RUNTIME}; +use crate::{command, ipc, mpris, queue, spotify}; use std::fs; use std::path::PathBuf; use std::str::FromStr; @@ -67,6 +67,14 @@ pub struct UserDataInner { pub cmd: CommandManager, } +lazy_static!( + /// The global Tokio runtime for running asynchronous tasks. + pub static ref ASYNC_RUNTIME: tokio::runtime::Runtime = tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .unwrap(); +); + /// The representation of an ncspot application. pub struct Application { /// The Spotify library, which is obtained from the Spotify API using rspotify. diff --git a/src/main.rs b/src/main.rs index bb3063a..ab2eecf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,14 +59,8 @@ fn register_backtrace_panic_handler() { })); } -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(); -); - +// Functionality related to the operating system process itself is implemented here. +// Functionality related to ncspot goes into `Application`. fn main() -> Result<(), String> { register_backtrace_panic_handler(); diff --git a/src/mpris.rs b/src/mpris.rs index 89ad514..365ed03 100644 --- a/src/mpris.rs +++ b/src/mpris.rs @@ -8,6 +8,7 @@ use tokio_stream::StreamExt; use zbus::zvariant::{ObjectPath, Value}; use zbus::{dbus_interface, ConnectionBuilder}; +use crate::application::ASYNC_RUNTIME; use crate::library::Library; use crate::model::album::Album; use crate::model::episode::Episode; @@ -19,7 +20,6 @@ use crate::queue::RepeatSetting; use crate::spotify::UriType; use crate::spotify_url::SpotifyUrl; use crate::traits::ListItem; -use crate::ASYNC_RUNTIME; use crate::{ events::EventManager, queue::Queue, diff --git a/src/spotify.rs b/src/spotify.rs index d599e03..231a2c9 100644 --- a/src/spotify.rs +++ b/src/spotify.rs @@ -23,12 +23,12 @@ use std::str::FromStr; use std::sync::{Arc, RwLock}; use std::time::{Duration, SystemTime}; +use crate::application::ASYNC_RUNTIME; use crate::config; 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; diff --git a/src/spotify_api.rs b/src/spotify_api.rs index e6a5e86..b94abf0 100644 --- a/src/spotify_api.rs +++ b/src/spotify_api.rs @@ -1,3 +1,4 @@ +use crate::application::ASYNC_RUNTIME; use crate::model::album::Album; use crate::model::artist::Artist; use crate::model::category::Category; @@ -7,7 +8,6 @@ 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};