2021-04-02 12:33:56 +00:00
|
|
|
use crate::app::{ExternalMsg, MsgIn, Task};
|
|
|
|
use std::fs;
|
|
|
|
use std::sync::mpsc::Sender;
|
|
|
|
use std::thread;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
pub fn keep_reading(pipe: String, tx: Sender<Task>) {
|
|
|
|
thread::spawn(move || loop {
|
|
|
|
let in_str = fs::read_to_string(&pipe).unwrap_or_default();
|
|
|
|
|
|
|
|
if !in_str.is_empty() {
|
|
|
|
let msgs = in_str
|
|
|
|
.lines()
|
2021-04-03 06:01:40 +00:00
|
|
|
.map(|s| serde_yaml::from_str::<ExternalMsg>(s.trim()));
|
2021-04-02 12:33:56 +00:00
|
|
|
|
2021-04-03 06:01:40 +00:00
|
|
|
msgs.for_each(|msg| match msg {
|
|
|
|
Ok(m) => {
|
2021-04-09 09:43:20 +00:00
|
|
|
tx.send(Task::new(MsgIn::External(m), None)).unwrap();
|
2021-04-03 06:01:40 +00:00
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
tx.send(Task::new(
|
|
|
|
MsgIn::External(ExternalMsg::LogError(e.to_string())),
|
|
|
|
None,
|
|
|
|
))
|
|
|
|
.unwrap();
|
|
|
|
}
|
2021-04-02 12:33:56 +00:00
|
|
|
});
|
|
|
|
fs::write(&pipe, "").unwrap();
|
2021-04-06 19:00:09 +00:00
|
|
|
} else {
|
|
|
|
thread::sleep(Duration::from_millis(50));
|
|
|
|
}
|
2021-04-02 12:33:56 +00:00
|
|
|
});
|
|
|
|
}
|