Skip to content

Commit

Permalink
Only allow request cancellation when the selected request is pending
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasPickering committed Dec 6, 2024
1 parent 8cd971c commit 5baccdc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
8 changes: 8 additions & 0 deletions crates/tui/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,14 @@ impl RequestStore {
Ok(())
}

/// Is the given request either building or loading?
pub fn is_in_progress(&self, id: RequestId) -> bool {
matches!(
self.get(id),
Some(RequestState::Building { .. } | RequestState::Loading { .. },)
)
}

/// Replace a request state in the store with new state, by mapping it
/// through a function. This assumes the request state is supposed to be in
/// the state, so it logs a message if it isn't (panics in debug mode). This
Expand Down
23 changes: 13 additions & 10 deletions crates/tui/src/view/component/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,19 @@ impl EventHandler for Root {
}
Action::Cancel => {
if let Some(request_id) = self.selected_request_id.0 {
ViewContext::open_modal(ConfirmModal::new(
"Cancel request?".into(),
move |response| {
if response {
ViewContext::send_message(
Message::HttpCancel(request_id),
);
}
},
))
// 2024 edition: if-let chain
if context.request_store.is_in_progress(request_id) {
ViewContext::open_modal(ConfirmModal::new(
"Cancel request?".into(),
move |response| {
if response {
ViewContext::send_message(
Message::HttpCancel(request_id),
);
}
},
))
}
}
}
Action::Quit => ViewContext::send_message(Message::Quit),
Expand Down

0 comments on commit 5baccdc

Please sign in to comment.