mail/view: cancel previous jobs on MailView drop/update

When MailView loads a new thread or gets dropped, cancel all pending
jobs.

Signed-off-by: Manos Pitsidianakis <manos@pitsidianak.is>
pull/284/head
Manos Pitsidianakis 2023-08-21 10:18:30 +03:00
parent b3858de2f4
commit 52874f9a97
Signed by: Manos Pitsidianakis
GPG Key ID: 7729C7707F7E09D0
3 changed files with 36 additions and 5 deletions

View File

@ -40,7 +40,7 @@ pub use futures::channel::oneshot;
use indexmap::IndexMap;
use melib::{log, smol, utils::datetime, uuid::Uuid, UnixTimestamp};
use crate::types::{ThreadEvent, UIEvent};
use crate::types::{StatusEvent, ThreadEvent, UIEvent};
type AsyncTask = async_task::Runnable;
@ -425,8 +425,14 @@ pub struct JoinHandle<T> {
}
impl<T> JoinHandle<T> {
pub fn cancel(&self) {
*self.cancel.lock().unwrap() = true;
pub fn cancel(&self) -> Option<StatusEvent> {
let mut lck = self.cancel.lock().unwrap();
if !*lck {
*lck = true;
Some(StatusEvent::JobCanceled(self.job_id))
} else {
None
}
}
}

View File

@ -53,7 +53,7 @@ pub use envelope::EnvelopeView;
/// Contains an Envelope view, with sticky headers, a pager for the body, and
/// subviews for more menus
#[derive(Debug, Default)]
#[derive(Debug)]
pub struct MailView {
coordinates: Option<(AccountHash, MailboxHash, EnvelopeHash)>,
dirty: bool,
@ -62,6 +62,7 @@ pub struct MailView {
theme_default: ThemeAttribute,
active_jobs: HashSet<JobId>,
state: MailViewState,
main_loop_handler: MainLoopHandler,
id: ComponentId,
}
@ -72,6 +73,7 @@ impl Clone for MailView {
forward_dialog: None,
state: MailViewState::default(),
active_jobs: self.active_jobs.clone(),
main_loop_handler: self.main_loop_handler.clone(),
..*self
}
}
@ -83,6 +85,17 @@ impl std::fmt::Display for MailView {
}
}
impl Drop for MailView {
fn drop(&mut self) {
if let MailViewState::LoadingBody { ref mut handle, .. } = self.state {
if let Some(canceled) = handle.cancel() {
self.main_loop_handler
.send(UIEvent::StatusEvent(canceled).into());
}
}
}
}
impl MailView {
pub fn new(
coordinates: Option<(AccountHash, MailboxHash, EnvelopeHash)>,
@ -96,6 +109,7 @@ impl MailView {
theme_default: crate::conf::value(context, "mail.view.body"),
active_jobs: Default::default(),
state: MailViewState::default(),
main_loop_handler: context.main_loop_handler.clone(),
id: ComponentId::default(),
};
@ -222,6 +236,11 @@ impl MailView {
new_coordinates: (AccountHash, MailboxHash, EnvelopeHash),
context: &mut Context,
) {
if let MailViewState::LoadingBody { ref mut handle, .. } = self.state {
if let Some(canceled) = handle.cancel() {
context.replies.push_back(UIEvent::StatusEvent(canceled));
}
}
if self.coordinates != Some(new_coordinates) {
self.coordinates = Some(new_coordinates);
self.init_futures(context);

View File

@ -84,7 +84,13 @@ pub enum ThreadEvent {
impl From<RefreshEvent> for ThreadEvent {
fn from(event: RefreshEvent) -> Self {
ThreadEvent::RefreshMailbox(Box::new(event))
Self::RefreshMailbox(Box::new(event))
}
}
impl From<UIEvent> for ThreadEvent {
fn from(event: UIEvent) -> Self {
Self::UIEvent(event)
}
}