Skip to content

Commit

Permalink
make free_space mutexless
Browse files Browse the repository at this point in the history
  • Loading branch information
micielski committed Jul 15, 2024
1 parent 7bf5a18 commit 7b86b17
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
7 changes: 3 additions & 4 deletions rm-main/src/transmission/fetchers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub async fn stats(ctx: app::Ctx) {
}
}

pub async fn free_space(ctx: app::Ctx, free_space: Arc<Mutex<Option<FreeSpace>>>) {
pub async fn free_space(ctx: app::Ctx) {
let (sess_tx, sess_rx) = oneshot::channel();
ctx.send_torrent_action(TorrentAction::GetSessionGet(sess_tx));
let download_dir = sess_rx.await.unwrap().download_dir.leak();
Expand All @@ -37,10 +37,9 @@ pub async fn free_space(ctx: app::Ctx, free_space: Arc<Mutex<Option<FreeSpace>>>
download_dir.to_string(),
space_tx,
));
let new_free_space = space_rx.await.unwrap();
let free_space = space_rx.await.unwrap();

*free_space.lock().unwrap() = Some(new_free_space);
ctx.send_action(Action::Render);
ctx.send_update_action(UpdateAction::FreeSpace(Arc::new(free_space)));
tokio::time::sleep(Duration::from_secs(
ctx.config.connection.free_space_refresh,
))
Expand Down
16 changes: 9 additions & 7 deletions rm-main/src/ui/tabs/torrents/bottom_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,28 @@ use super::table_manager::TableManager;

pub(super) struct BottomStats {
// TODO: get rid of the Option (requires changes in transmission-rpc so SessionStats impls Default
// TODO: ^ The same thing with FreeSpace
pub(super) stats: Option<Arc<SessionStats>>,
pub(super) free_space: Arc<Mutex<Option<FreeSpace>>>,
pub(super) free_space: Option<Arc<FreeSpace>>,
pub(super) table_manager: Arc<Mutex<TableManager>>,
}

impl BottomStats {
pub fn new(
free_space: Arc<Mutex<Option<FreeSpace>>>,
table_manager: Arc<Mutex<TableManager>>,
) -> Self {
pub fn new(table_manager: Arc<Mutex<TableManager>>) -> Self {
Self {
stats: None,
free_space,
free_space: None,
table_manager,
}
}

pub fn set_stats(&mut self, stats: Arc<SessionStats>) {
self.stats = Some(stats);
}

pub fn set_free_space(&mut self, free_space: Arc<FreeSpace>) {
self.free_space = Some(free_space);
}
}
impl Component for BottomStats {
fn render(&mut self, f: &mut Frame, rect: Rect) {
Expand All @@ -45,7 +47,7 @@ impl Component for BottomStats {

let mut text = format!(" {download} |  {upload}");

if let Some(free_space) = &*self.free_space.lock().unwrap() {
if let Some(free_space) = &self.free_space {
let free_space = bytes_to_human_format(free_space.size_bytes);
text = format!("󰋊 {free_space} | {text}")
}
Expand Down
17 changes: 10 additions & 7 deletions rm-main/src/ui/tabs/torrents/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ impl TorrentsTab {
pub fn new(ctx: app::Ctx) -> Self {
let table = GenericTable::new(vec![]);
let table_manager = Arc::new(Mutex::new(TableManager::new(ctx.clone(), table)));
let free_space = Arc::new(Mutex::new(None));
let bottom_stats = BottomStats::new(free_space, Arc::clone(&table_manager));
let bottom_stats = BottomStats::new(Arc::clone(&table_manager));

tokio::spawn(transmission::fetchers::stats(ctx.clone()));

Expand All @@ -48,10 +47,7 @@ impl TorrentsTab {
Arc::clone(&bottom_stats.table_manager),
));

tokio::spawn(transmission::fetchers::free_space(
ctx.clone(),
Arc::clone(&bottom_stats.free_space),
));
tokio::spawn(transmission::fetchers::free_space(ctx.clone()));

Self {
bottom_stats,
Expand Down Expand Up @@ -110,7 +106,14 @@ impl Component for TorrentsTab {

fn handle_update_action(&mut self, action: UpdateAction) {
match action {
UpdateAction::SessionStats(stats) => self.bottom_stats.set_stats(stats),
UpdateAction::SessionStats(stats) => {
self.bottom_stats.set_stats(stats);
self.ctx.send_action(Action::Render);
}
UpdateAction::FreeSpace(free_space) => {
self.bottom_stats.set_free_space(free_space);
self.ctx.send_action(Action::Render);
}
_ => (),
}
}
Expand Down
3 changes: 2 additions & 1 deletion rm-shared/src/action.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{collections::HashMap, sync::Arc};

use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
use transmission_rpc::types::SessionStats;
use transmission_rpc::types::{FreeSpace, SessionStats};

use crate::status_task::StatusTask;

Expand Down Expand Up @@ -44,6 +44,7 @@ pub enum UpdateAction {
SwitchToNormalMode,
TaskClear,
SessionStats(Arc<SessionStats>),
FreeSpace(Arc<FreeSpace>),
}

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down

0 comments on commit 7b86b17

Please sign in to comment.