Fix some sync issues

Handle out messages immediately instead or scheduling in messages.
This commit is contained in:
Arijit Basu 2021-05-11 14:13:48 +05:30 committed by Arijit Basu
parent cd5bf81646
commit 82b975c5f0
4 changed files with 18 additions and 38 deletions

View File

@ -2057,7 +2057,7 @@ impl App {
})
}
fn add_directory(mut self, parent: String, dir: DirectoryBuffer) -> Result<Self> {
pub fn add_directory(mut self, parent: String, dir: DirectoryBuffer) -> Result<Self> {
self.directory_buffers.insert(parent, dir);
self.msg_out.push_back(MsgOut::Refresh);
Ok(self)
@ -2270,17 +2270,17 @@ impl App {
Ok(self)
}
fn log_info(mut self, message: String) -> Result<Self> {
pub fn log_info(mut self, message: String) -> Result<Self> {
self.logs.push(Log::new(LogLevel::Info, message));
Ok(self)
}
fn log_success(mut self, message: String) -> Result<Self> {
pub fn log_success(mut self, message: String) -> Result<Self> {
self.logs.push(Log::new(LogLevel::Success, message));
Ok(self)
}
fn log_error(mut self, message: String) -> Result<Self> {
pub fn log_error(mut self, message: String) -> Result<Self> {
self.logs.push(Log::new(LogLevel::Error, message));
Ok(self)
}

View File

@ -47,14 +47,10 @@ pub fn explore_async(
parent: String,
focused_path: Option<String>,
tx_msg_in: Sender<Task>,
tx_pwd_watcher: Sender<String>,
) {
thread::spawn(move || {
explore_sync(config, parent.clone(), focused_path)
.map(|buf| {
tx_pwd_watcher
.send(buf.parent().clone())
.unwrap_or_default();
tx_msg_in
.send(Task::new(
MsgIn::Internal(InternalMsg::AddDirectory(parent.clone(), buf)),
@ -78,23 +74,15 @@ pub fn explore_recursive_async(
parent: String,
focused_path: Option<String>,
tx_msg_in: Sender<Task>,
tx_pwd_watcher: Sender<String>,
) {
let path = PathBuf::from(&parent);
explore_async(
config.clone(),
parent,
focused_path,
tx_msg_in.clone(),
tx_pwd_watcher.clone(),
);
explore_async(config.clone(), parent, focused_path, tx_msg_in.clone());
if let Some(grand_parent) = path.parent() {
explore_recursive_async(
config,
grand_parent.to_string_lossy().to_string(),
path.file_name().map(|f| f.to_string_lossy().to_string()),
tx_msg_in,
tx_pwd_watcher,
);
}
}

View File

@ -17,10 +17,6 @@ pub fn keep_watching(
thread::spawn(move || loop {
if let Ok(new_pwd) = rx_pwd_watcher.try_recv() {
pwd = PathBuf::from(new_pwd);
last_modified = pwd
.metadata()
.and_then(|m| m.modified())
.unwrap_or(last_modified);
} else {
pwd.metadata()
.and_then(|m| m.modified())

View File

@ -75,8 +75,8 @@ pub fn run(mut app: app::App, focused_path: Option<String>) -> Result<Option<Str
app.pwd().clone(),
focused_path,
tx_msg_in.clone(),
tx_pwd_watcher.clone(),
);
tx_pwd_watcher.send(app.pwd().clone())?;
let mut hb = Handlebars::new();
hb.register_helper("humansize", Box::new(to_humansize));
@ -129,6 +129,8 @@ pub fn run(mut app: app::App, focused_path: Option<String>) -> Result<Option<Str
while let Some(msg) = app.pop_msg_out() {
match msg {
// NOTE: Do not schedule critical tasks via tx_msg_in in this loop.
// Try handling them immediately.
app::MsgOut::Enque(task) => {
tx_msg_in.send(task)?;
}
@ -164,19 +166,14 @@ pub fn run(mut app: app::App, focused_path: Option<String>) -> Result<Option<Str
app.focused_node().map(|n| n.relative_path().clone()),
) {
Ok(buf) => {
tx_pwd_watcher.send(buf.parent().clone())?;
let msg = app::MsgIn::Internal(app::InternalMsg::AddDirectory(
buf.parent().clone(),
buf,
));
tx_msg_in.send(app::Task::new(msg, None))?;
let pwd = buf.parent().clone();
app = app.add_directory(pwd.clone(), buf)?;
}
Err(e) => {
let msg =
app::MsgIn::External(app::ExternalMsg::LogError(e.to_string()));
tx_msg_in.send(app::Task::new(msg, None))?;
app = app.log_error(e.to_string())?;
}
}
};
tx_pwd_watcher.send(app.pwd().clone())?;
}
app::MsgOut::ExplorePwdAsync => {
@ -185,8 +182,8 @@ pub fn run(mut app: app::App, focused_path: Option<String>) -> Result<Option<Str
app.pwd().clone(),
app.focused_node().map(|n| n.relative_path().clone()),
tx_msg_in.clone(),
tx_pwd_watcher.clone(),
);
tx_pwd_watcher.send(app.pwd().clone())?;
}
app::MsgOut::ExploreParentsAsync => {
@ -195,12 +192,13 @@ pub fn run(mut app: app::App, focused_path: Option<String>) -> Result<Option<Str
app.pwd().clone(),
app.focused_node().map(|n| n.relative_path().clone()),
tx_msg_in.clone(),
tx_pwd_watcher.clone(),
);
tx_pwd_watcher.send(app.pwd().clone())?;
}
app::MsgOut::Refresh => {
// $PWD watcher
tx_pwd_watcher.send(app.pwd().clone())?;
// UI
terminal.draw(|f| ui::draw(f, &app, &hb))?;
}
@ -220,8 +218,7 @@ pub fn run(mut app: app::App, focused_path: Option<String>) -> Result<Option<Str
.unwrap_or_else(|e| Err(e.to_string()));
if let Err(e) = status {
let msg = app::MsgIn::External(app::ExternalMsg::LogError(e));
tx_msg_in.send(app::Task::new(msg, None))?;
app = app.log_error(e.to_string())?;
};
tx_event_reader.send(false)?;
@ -247,8 +244,7 @@ pub fn run(mut app: app::App, focused_path: Option<String>) -> Result<Option<Str
.unwrap_or_else(|e| Err(e.to_string()));
if let Err(e) = status {
let msg = app::MsgIn::External(app::ExternalMsg::LogError(e));
tx_msg_in.send(app::Task::new(msg, None))?;
app = app.log_error(e.to_string())?;
};
terminal.clear()?;