radicle: move Emitter to events module

Move the events Emitter to the `radicle::node::events` module to allow it to be
used outside of `radicle-node`.

Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
X-Clacks-Overhead: GNU Terry Pratchett
This commit is contained in:
Fintan Halpenny 2024-04-23 17:17:20 +01:00
parent fd4c4cd0af
commit f0648c2a18
No known key found for this signature in database
GPG Key ID: C93C17467280C75B
2 changed files with 36 additions and 34 deletions

View File

@ -3,7 +3,6 @@ pub mod thread;
use std::os::unix::net::UnixListener; use std::os::unix::net::UnixListener;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::{fs, io, net}; use std::{fs, io, net};
use crossbeam_channel as chan; use crossbeam_channel as chan;
@ -35,6 +34,7 @@ use crate::{service, LocalTime};
pub use handle::Error as HandleError; pub use handle::Error as HandleError;
pub use handle::Handle; pub use handle::Handle;
pub use node::events::Emitter;
/// A client error. /// A client error.
#[derive(Error, Debug)] #[derive(Error, Debug)]
@ -84,39 +84,6 @@ pub enum Error {
GitVersion(#[from] git::VersionError), GitVersion(#[from] git::VersionError),
} }
/// Publishes events to subscribers.
#[derive(Debug, Clone)]
pub struct Emitter<T> {
subscribers: Arc<Mutex<Vec<chan::Sender<T>>>>,
}
impl<T> Default for Emitter<T> {
fn default() -> Emitter<T> {
Emitter {
subscribers: Default::default(),
}
}
}
impl<T: Clone> Emitter<T> {
/// Emit event to subscribers and drop those who can't receive it.
pub(crate) fn emit(&self, event: T) {
self.subscribers
.lock()
.unwrap()
.retain(|s| s.try_send(event.clone()).is_ok());
}
/// Subscribe to events stream.
pub fn subscribe(&self) -> chan::Receiver<T> {
let (sender, receiver) = chan::unbounded();
let mut subs = self.subscribers.lock().unwrap();
subs.push(sender);
receiver
}
}
/// Holds join handles to the client threads, as well as a client handle. /// Holds join handles to the client threads, as well as a client handle.
pub struct Runtime { pub struct Runtime {
pub id: NodeId, pub id: NodeId,

View File

@ -1,4 +1,6 @@
use std::ops::Deref; use std::ops::Deref;
use std::sync::Arc;
use std::sync::Mutex;
use std::time; use std::time;
use crossbeam_channel as chan; use crossbeam_channel as chan;
@ -119,3 +121,36 @@ impl Events {
} }
} }
} }
/// Publishes events to subscribers.
#[derive(Debug, Clone)]
pub struct Emitter<T> {
subscribers: Arc<Mutex<Vec<chan::Sender<T>>>>,
}
impl<T> Default for Emitter<T> {
fn default() -> Emitter<T> {
Emitter {
subscribers: Default::default(),
}
}
}
impl<T: Clone> Emitter<T> {
/// Emit event to subscribers and drop those who can't receive it.
pub fn emit(&self, event: T) {
self.subscribers
.lock()
.unwrap()
.retain(|s| s.try_send(event.clone()).is_ok());
}
/// Subscribe to events stream.
pub fn subscribe(&self) -> chan::Receiver<T> {
let (sender, receiver) = chan::unbounded();
let mut subs = self.subscribers.lock().unwrap();
subs.push(sender);
receiver
}
}