init
This commit is contained in:
+33
-13
@@ -4,9 +4,10 @@ use std::sync::Arc;
|
||||
|
||||
use parking_lot::RwLock;
|
||||
use std::collections::HashMap;
|
||||
use std::iter;
|
||||
use tokio::sync::broadcast;
|
||||
use tokio::task::JoinHandle;
|
||||
use tracing::log::kv::{Key, Value};
|
||||
// use tracing::log::kv::{Key, Value};
|
||||
use tracing::{debug, trace, warn};
|
||||
use uuid::Uuid;
|
||||
|
||||
@@ -17,7 +18,7 @@ pub type AnyEvent = Arc<dyn Any + Send + Sync>;
|
||||
const DEFAULT_CAPACITY: usize = 64;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum ScopeValue {
|
||||
pub enum ScopeValue {
|
||||
String(String),
|
||||
Uuid(Uuid),
|
||||
}
|
||||
@@ -29,10 +30,36 @@ impl ScopeValue {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
struct Scope {
|
||||
key: String,
|
||||
value: ScopeValue,
|
||||
pub struct Scope {
|
||||
pub key: String,
|
||||
pub value: ScopeValue,
|
||||
}
|
||||
|
||||
impl Scope {
|
||||
pub fn new(key: impl Into<String>, value: ScopeValue) -> Self {
|
||||
Self {
|
||||
key: key.into(),
|
||||
value,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn uuid(key: impl Into<String>, value: Uuid) -> Self {
|
||||
Self::new(key, ScopeValue::Uuid(value))
|
||||
}
|
||||
|
||||
pub fn string(key: impl Into<String>, value: impl Into<String>) -> Self {
|
||||
Self::new(key, ScopeValue::String(value.into()))
|
||||
}
|
||||
}
|
||||
impl IntoIterator for Scope {
|
||||
type Item = Scope;
|
||||
type IntoIter = iter::Once<Scope>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
iter::once(self)
|
||||
}
|
||||
}
|
||||
|
||||
/// The central event bus.
|
||||
@@ -157,14 +184,7 @@ impl EventBus {
|
||||
trace!(topic, "Emitting event");
|
||||
let event: AnyEvent = Arc::new(event);
|
||||
|
||||
if let Some(tx) = self.channels.read().get(topic) {
|
||||
let receiver_count = tx.receiver_count();
|
||||
let _ = tx.send(Arc::clone(&event));
|
||||
trace!(
|
||||
topic,
|
||||
receiver_count, "Event delivered to exact-topic channel"
|
||||
);
|
||||
}
|
||||
self.emit_arc(topic, event);
|
||||
}
|
||||
|
||||
// todo : undocumented...
|
||||
|
||||
@@ -43,7 +43,7 @@ macro_rules! match_event {
|
||||
}
|
||||
|
||||
mod bus;
|
||||
pub use bus::{AnyEvent, EventBus};
|
||||
pub use bus::{AnyEvent, EventBus, Scope, ScopeValue};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
Reference in New Issue
Block a user