Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
radicle: move Emitter to events module
Fintan Halpenny committed 2 years ago
commit f0648c2a184732134e38f8ce1eeae5f6b2f5a04d
parent fd4c4cd0af8836213a0e846e674fb6554c123e96
2 files changed +36 -34
modified radicle-node/src/runtime.rs
@@ -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,
modified radicle/src/node/events.rs
@@ -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
+
    }
+
}