From 368d5d3db76f81217b1de8d4894b7cea351e93bd Mon Sep 17 00:00:00 2001 From: Alex Clarke Date: Tue, 17 Dec 2024 14:36:49 -0700 Subject: [PATCH] fix: Cancel all requests when switching Servarr tabs to both improve performance and fix issue #15 --- src/app/mod.rs | 2 +- src/handlers/handlers_tests.rs | 4 ++++ src/handlers/mod.rs | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/app/mod.rs b/src/app/mod.rs index e39e2b2..eec1ed7 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -26,7 +26,7 @@ pub mod sonarr; pub struct App<'a> { navigation_stack: Vec, network_tx: Option>, - cancellation_token: CancellationToken, + pub cancellation_token: CancellationToken, pub is_first_render: bool, pub server_tabs: TabState, pub error: HorizontallyScrollableText, diff --git a/src/handlers/handlers_tests.rs b/src/handlers/handlers_tests.rs index 0b0fa44..2115b3d 100644 --- a/src/handlers/handlers_tests.rs +++ b/src/handlers/handlers_tests.rs @@ -4,6 +4,7 @@ mod tests { use crate::models::sonarr_models::Series; use pretty_assertions::assert_eq; use rstest::rstest; + use tokio_util::sync::CancellationToken; use crate::app::key_binding::DEFAULT_KEYBINDINGS; use crate::app::App; @@ -65,10 +66,12 @@ mod tests { assert_eq!(app.get_current_route(), left_block.into()); assert!(app.is_first_render); assert_eq!(app.error, HorizontallyScrollableText::default()); + assert!(app.cancellation_token.is_cancelled()); app.server_tabs.set_index(index); app.is_first_render = false; app.error = "Test".into(); + app.cancellation_token = CancellationToken::new(); handle_events(DEFAULT_KEYBINDINGS.next_servarr.key, &mut app); @@ -76,6 +79,7 @@ mod tests { assert_eq!(app.get_current_route(), right_block.into()); assert!(app.is_first_render); assert_eq!(app.error, HorizontallyScrollableText::default()); + assert!(app.cancellation_token.is_cancelled()); } #[rstest] diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index ed2a906..184000e 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -88,10 +88,12 @@ pub fn handle_events(key: Key, app: &mut App<'_>) { app.reset(); app.server_tabs.next(); app.pop_and_push_navigation_stack(app.server_tabs.get_active_route()); + app.cancellation_token.cancel(); } else if key == DEFAULT_KEYBINDINGS.previous_servarr.key { app.reset(); app.server_tabs.previous(); app.pop_and_push_navigation_stack(app.server_tabs.get_active_route()); + app.cancellation_token.cancel(); } else { match app.get_current_route() { Route::Radarr(active_radarr_block, context) => {