refactor stop logic

This commit is contained in:
dignifiedquire
2020-03-21 19:05:50 +01:00
parent 4d6a9e4f14
commit d798356c19
4 changed files with 51 additions and 30 deletions

View File

@@ -268,13 +268,15 @@ async fn start(args: Vec<String>) -> Result<(), failure::Error> {
let context = Context::new("CLI".into(), Path::new(&args[1]).to_path_buf()).await?;
let ctx = context.clone();
std::thread::spawn(move || loop {
if ctx.has_next_event() {
if let Ok(event) = ctx.get_next_event() {
receive_event(event);
async_std::task::spawn(async move {
loop {
if ctx.has_next_event() {
if let Ok(event) = ctx.get_next_event() {
receive_event(event);
}
} else {
async_std::task::sleep(std::time::Duration::from_millis(50)).await;
}
} else {
std::thread::sleep(std::time::Duration::from_millis(50));
}
});
@@ -316,6 +318,7 @@ async fn start(args: Vec<String>) -> Result<(), failure::Error> {
}
Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => {
println!("Exiting...");
context.stop().await;
break;
}
Err(err) => {
@@ -324,11 +327,10 @@ async fn start(args: Vec<String>) -> Result<(), failure::Error> {
}
}
}
rl.save_history(".dc-history.txt")?;
println!("history saved");
context.stop().await;
Ok(())
}