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

@ -9,6 +9,7 @@ use tokio::net;
use tokio::sync::mpsc;
use tokio::time;
use std::sync::atomic::{self, AtomicBool};
use std::sync::Arc;
use std::thread;
@ -17,6 +18,7 @@ pub const SERVER_ADDRESS: &str = "127.0.0.1:9167";
#[derive(Debug, Clone)]
pub struct Client {
sender: mpsc::Sender<Message>,
is_connected: Arc<AtomicBool>,
_handle: Arc<thread::JoinHandle<()>>,
}
@ -31,6 +33,9 @@ pub enum Message {
at: SystemTime,
event: Event,
},
Quit {
at: SystemTime,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
@ -47,28 +52,50 @@ impl Client {
event,
});
}
pub fn is_connected(&self) -> bool {
self.is_connected.load(atomic::Ordering::Relaxed)
}
pub fn quit(&self) {
let _ = self.sender.try_send(Message::Quit {
at: SystemTime::now(),
});
}
}
#[must_use]
pub fn connect(name: String) -> Client {
let (sender, receiver) = mpsc::channel(100);
let is_connected = Arc::new(AtomicBool::new(false));
let handle = std::thread::spawn(move || run(name, receiver));
let handle = {
let is_connected = is_connected.clone();
std::thread::spawn(move || run(name, is_connected.clone(), receiver))
};
Client {
sender,
is_connected,
_handle: Arc::new(handle),
}
}
#[tokio::main]
async fn run(name: String, mut receiver: mpsc::Receiver<Message>) {
async fn run(
name: String,
is_connected: Arc<AtomicBool>,
mut receiver: mpsc::Receiver<Message>,
) {
let version = semver::Version::parse(env!("CARGO_PKG_VERSION"))
.expect("Parse package version");
loop {
match _connect().await {
Ok(mut stream) => {
is_connected.store(true, atomic::Ordering::Relaxed);
let _ = send(
&mut stream,
Message::Connected {
@ -92,6 +119,7 @@ async fn run(name: String, mut receiver: mpsc::Receiver<Message>) {
}
}
Err(_) => {
is_connected.store(false, atomic::Ordering::Relaxed);
time::sleep(time::Duration::from_secs(2)).await;
}
}

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(_)) => {