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:
parent
fd4c4cd0af
commit
f0648c2a18
|
|
@ -3,7 +3,6 @@ pub mod thread;
|
|||
|
||||
use std::os::unix::net::UnixListener;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::{fs, io, net};
|
||||
|
||||
use crossbeam_channel as chan;
|
||||
|
|
@ -35,6 +34,7 @@ use crate::{service, LocalTime};
|
|||
|
||||
pub use handle::Error as HandleError;
|
||||
pub use handle::Handle;
|
||||
pub use node::events::Emitter;
|
||||
|
||||
/// A client error.
|
||||
#[derive(Error, Debug)]
|
||||
|
|
@ -84,39 +84,6 @@ pub enum Error {
|
|||
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.
|
||||
pub struct Runtime {
|
||||
pub id: NodeId,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
use std::ops::Deref;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::time;
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue