MailBackend: remove connect() method

async
Manos Pitsidianakis 2020-07-05 19:56:17 +03:00
parent f48343ca89
commit b5748c247a
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
7 changed files with 27 additions and 57 deletions

View File

@ -299,11 +299,12 @@ pub type ResultFuture<T> = Result<Pin<Box<dyn Future<Output = Result<T>> + Send
pub trait MailBackend: ::std::fmt::Debug + Send + Sync {
fn is_async(&self) -> bool;
fn is_remote(&self) -> bool;
fn is_online(&self) -> Result<()>;
fn is_online(&self) -> Result<()> {
Err(MeliError::new("Unimplemented."))
}
fn is_online_async(&self) -> ResultFuture<()> {
Err(MeliError::new("Unimplemented."))
}
fn connect(&mut self) {}
fn get(&mut self, mailbox: &Mailbox) -> Result<Async<Result<Vec<Envelope>>>>;
fn get_async(
&mut self,

View File

@ -194,18 +194,6 @@ impl MailBackend for ImapType {
}
fn is_online(&self) -> Result<()> {
//if let Ok(mut g) = try_lock(&self.connection, None) {
// let _ = g.connect();
//}
try_lock(
&self.uid_store.is_online,
Some(std::time::Duration::from_millis(12)),
)?
.1
.clone()
}
fn connect(&mut self) {
let connection = self.connection.clone();
let _ = std::thread::Builder::new()
.name(format!("{} connecting", self.account_name.as_str(),))
@ -214,6 +202,12 @@ impl MailBackend for ImapType {
let _ = g.connect();
}
});
try_lock(
&self.uid_store.is_online,
Some(std::time::Duration::from_millis(12)),
)?
.1
.clone()
}
fn get(&mut self, mailbox: &Mailbox) -> Result<Async<Result<Vec<Envelope>>>> {

View File

@ -282,34 +282,13 @@ impl MailBackend for ImapType {
fn is_online_async(&self) -> ResultFuture<()> {
let connection = self.connection.clone();
Ok(Box::pin(async move {
debug!("INSIDE is_online_async()");
let mut conn = connection.lock().await;
debug!(conn.connect().await)?;
conn.connect().await?;
Ok(())
}))
}
fn is_online(&self) -> Result<()> {
Ok(())
//if let Ok(mut g) = try_lock(&self.connection, None) {
// let _ = g.connect();
//}
}
fn connect(&mut self) {
/*
let connection = self.connection.clone();
let _ = std::thread::Builder::new()
.name(format!("{} connecting", self.account_name.as_str(),))
.spawn(move || {
if let Ok(mut g) = try_lock(&connection, None) {
let _ = g.connect();
}
});
*/
}
fn get(&mut self, _mailbox: &Mailbox) -> Result<Async<Result<Vec<Envelope>>>> {
Err(MeliError::new("Unimplemented."))
}

View File

@ -199,16 +199,13 @@ impl MailBackend for JmapType {
}
fn is_online(&self) -> Result<()> {
self.online.lock().unwrap().1.clone()
}
fn connect(&mut self) {
if self.is_online().is_err()
if self.online.lock().unwrap().1.is_err()
&& Instant::now().duration_since(self.online.lock().unwrap().0)
>= std::time::Duration::new(2, 0)
{
let _ = self.mailboxes();
}
self.online.lock().unwrap().1.clone()
}
fn get(&mut self, mailbox: &Mailbox) -> Result<Async<Result<Vec<Envelope>>>> {

View File

@ -152,8 +152,8 @@ pub struct Account {
sender: Sender<ThreadEvent>,
event_queue: VecDeque<(MailboxHash, RefreshEvent)>,
notify_fn: Arc<NotifyFn>,
is_async: bool,
is_remote: bool,
pub is_async: bool,
pub is_remote: bool,
}
pub enum JobRequest {
@ -344,12 +344,11 @@ impl Account {
}
}
}
Ok(Account {
let mut ret = Account {
index,
hash,
name,
is_online: false,
is_online: !backend.is_remote(),
mailbox_entries: Default::default(),
mailboxes_order: Default::default(),
tree: Default::default(),
@ -366,7 +365,13 @@ impl Account {
is_async: backend.is_async(),
is_remote: backend.is_remote(),
backend: Arc::new(RwLock::new(backend)),
})
};
if !ret.is_remote && !ret.is_async {
ret.init(None)?;
}
Ok(ret)
}
fn init(&mut self, ref_mailboxes: Option<HashMap<MailboxHash, Mailbox>>) -> Result<()> {
@ -1400,14 +1405,10 @@ impl Account {
/* Call only in Context::is_online, since only Context can launch the watcher threads if an
* account goes from offline to online. */
pub fn is_online(&mut self) -> Result<()> {
if !self.is_remote {
if !self.is_remote && !self.is_async {
return Ok(());
}
if !self.is_online {
self.backend.write().unwrap().connect();
}
if self.is_async {
if self.is_online {
return Ok(());
@ -1425,9 +1426,6 @@ impl Account {
self.init(None)?;
}
self.is_online = ret.is_ok();
if !self.is_online {
self.backend.write().unwrap().connect();
}
ret
}
}

View File

@ -93,8 +93,6 @@ impl MailBackend for PluginBackend {
}
}
fn connect(&mut self) {}
fn get(&mut self, mailbox: &Mailbox) -> Result<Async<Result<Vec<Envelope>>>> {
let mut w = AsyncBuilder::new();
let _mailbox_hash = mailbox.hash();

View File

@ -358,6 +358,9 @@ impl State {
s.switch_to_alternate_screen();
debug!("inserting mailbox hashes:");
for i in 0..s.context.accounts.len() {
if !s.context.accounts[i].is_remote {
s.context.accounts[i].watch();
}
if s.context.is_online(i).is_ok() && s.context.accounts[i].is_empty() {
//return Err(MeliError::new(format!(
// "Account {} has no mailboxes configured.",