Add reload when receiving refresh event!!

embed
Manos Pitsidianakis 2018-08-11 19:19:30 +03:00
parent 9abfc855fa
commit 1b44aae5ce
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
5 changed files with 45 additions and 10 deletions

View File

@ -57,7 +57,7 @@ impl Account {
.position(|x: &Folder| x.name() == settings.sent_folder);
for f in ref_folders {
folders.push(None);
let mut handle = backend.get(&f);
let handle = backend.get(&f);
workers.push(Some(handle));
}
Account {
@ -72,6 +72,11 @@ impl Account {
backend: backend,
}
}
pub fn reload(&mut self, idx: usize) {
let ref_folders: Vec<Folder> = self.backend.folders();
let handle = self.backend.get(&ref_folders[idx]);
self.workers[idx] = Some(handle);
}
pub fn watch(&self, r: RefreshEventConsumer) -> () {
self.backend.watch(r).unwrap();
}

View File

@ -181,11 +181,9 @@ fn main() {
}
},
ThreadEvent::RefreshMailbox { hash : h } => {
eprintln!("got refresh mailbox hash {:x}", h);
state.hash_to_folder(h);
//state.rcv_event(UIEvent { id: 0, event_type: UIEventType::Notification(n.clone())});
state.redraw();
/* Don't handle this yet. */
},
ThreadEvent::UIEvent(UIEventType::ChangeMode(f)) => {
state.mode = f;

View File

@ -36,7 +36,7 @@ impl Component for Composer {
context.dirty_areas.push_back(area);
}
fn process_event(&mut self, event: &UIEvent, context: &mut Context) {}
fn process_event(&mut self, _event: &UIEvent, _context: &mut Context) {}
fn is_dirty(&self) -> bool {
true

View File

@ -692,9 +692,10 @@ impl Component for MailListing {
self.view = None;
}
UIEventType::MailboxUpdate((ref idxa, ref idxf)) => {
if *idxa == self.new_cursor_pos.1 && *idxf == self.new_cursor_pos.0 {
self.refresh_mailbox(context);
if *idxa == self.new_cursor_pos.0 && *idxf == self.new_cursor_pos.1 {
self.dirty = true;
self.refresh_mailbox(context);
return;
}
}
UIEventType::ChangeMode(UIMode::Normal) => {

View File

@ -50,7 +50,6 @@ pub struct Context {
/// Events queue that components send back to the state
pub replies: VecDeque<UIEvent>,
backends: Backends,
input_thread: chan::Sender<bool>,
pub temp_files: Vec<File>,
@ -155,7 +154,6 @@ impl State<std::io::Stdout> {
accounts,
mailbox_hashes: FnvHashMap::with_capacity_and_hasher(1, Default::default()),
backends,
settings: settings.clone(),
runtime_settings: settings,
dirty_areas: VecDeque::with_capacity(5),
@ -188,8 +186,41 @@ impl State<std::io::Stdout> {
}
s
}
pub fn hash_to_folder(&self, hash: u64) {
eprintln!("got refresh {:?}", self.context.mailbox_hashes[&hash]);
/*
* When we receive a folder hash from a watcher thread,
* we match the hash to the index of the mailbox, request a reload
* and startup a thread to remind us to poll it every now and then till it's finished.
*/
pub fn hash_to_folder(&mut self, hash: u64) {
let (idxa, idxm) = self.context.mailbox_hashes[&hash];
self.context.accounts[idxa].reload(idxm);
let (startup_tx, startup_rx) = chan::async();
let startup_thread = {
let sender = self.sender.clone();
let startup_rx = startup_rx.clone();
thread::Builder::new()
.name("startup-thread".to_string())
.spawn(move || {
let dur = time::Duration::from_millis(100);
loop {
chan_select! {
default => {},
startup_rx.recv() -> _ => {
sender.send(ThreadEvent::UIEvent(UIEventType::MailboxUpdate((idxa,idxm))));
sender.send(ThreadEvent::ThreadJoin(thread::current().id()));
return;
}
}
sender.send(ThreadEvent::UIEvent(UIEventType::StartupCheck));
thread::sleep(dur);
}
})
.unwrap()
};
self.startup_thread = Some(startup_tx);
self.threads
.insert(startup_thread.thread().id(), startup_thread);
}
/// If an owned thread returns a `ThreadEvent::ThreadJoin` event to `State` then it must remove