Toggle the Comet when pressing F12

This commit is contained in:
Héctor Ramón Jiménez 2024-05-11 12:25:44 +02:00
parent fc53a97831
commit b7c65c877d
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
9 changed files with 125 additions and 35 deletions

View file

@ -35,6 +35,12 @@ pub enum Event {
duration: Duration,
span: Span,
},
QuitRequested {
at: SystemTime,
},
AlreadyRunning {
at: SystemTime,
},
}
impl Event {
@ -43,19 +49,36 @@ impl Event {
Self::Connected { at, .. }
| Self::Disconnected { at, .. }
| Self::ThemeChanged { at, .. }
| Self::SpanFinished { at, .. } => *at,
| Self::SpanFinished { at, .. }
| Self::QuitRequested { at }
| Self::AlreadyRunning { at } => *at,
}
}
}
pub fn is_running() -> bool {
std::net::TcpListener::bind(client::SERVER_ADDRESS).is_err()
}
pub fn run() -> impl Stream<Item = Event> {
stream::channel(|mut output| async move {
let mut buffer = Vec::new();
loop {
let Ok(mut stream) = connect().await else {
delay().await;
continue;
let mut stream = match connect().await {
Ok(stream) => stream,
Err(error) => {
if error.kind() == io::ErrorKind::AddrInUse {
let _ = output
.send(Event::AlreadyRunning {
at: SystemTime::now(),
})
.await;
}
delay().await;
continue;
}
};
loop {
@ -124,6 +147,11 @@ pub fn run() -> impl Stream<Item = Event> {
}
}
}
client::Message::Quit { at } => {
let _ = output
.send(Event::QuitRequested { at })
.await;
}
};
}
Err(Error::IOFailed(_)) => {