From dc4c448e597b08cba4c79c194b0c1aa517daab29 Mon Sep 17 00:00:00 2001 From: hozan23 Date: Mon, 15 Jul 2024 13:28:00 +0200 Subject: core: rename EventSys to EventEmitter --- core/src/event.rs | 68 +++++++++++++++++++++++++++---------------------------- core/src/lib.rs | 2 +- 2 files changed, 35 insertions(+), 35 deletions(-) (limited to 'core') diff --git a/core/src/event.rs b/core/src/event.rs index 709dbd9..8437ea3 100644 --- a/core/src/event.rs +++ b/core/src/event.rs @@ -18,14 +18,14 @@ pub type EventListenerID = u32; type Listeners = HashMap>>>; -/// EventSys emits events to registered listeners based on topics. +/// EventEmitter emits events to registered listeners based on topics. /// # Example /// /// ``` -/// use karyon_core::event::{EventSys, EventValueTopic, EventValue}; +/// use karyon_core::event::{EventEmitter, EventValueTopic, EventValue}; /// /// async { -/// let event_sys = EventSys::new(); +/// let event_emitter = EventEmitter::new(); /// /// #[derive(Hash, PartialEq, Eq, Debug, Clone)] /// enum Topic { @@ -42,9 +42,9 @@ type Listeners = HashMap(&Topic::TopicA).await; +/// let listener = event_emitter.register::(&Topic::TopicA).await; /// -/// event_sys.emit_by_topic(&Topic::TopicA, &A(3)) .await; +/// event_emitter.emit_by_topic(&Topic::TopicA, &A(3)) .await; /// let msg: A = listener.recv().await.unwrap(); /// /// #[derive(Clone, Debug, PartialEq)] @@ -63,9 +63,9 @@ type Listeners = HashMap(&Topic::TopicB).await; +/// let listener = event_emitter.register::(&Topic::TopicB).await; /// -/// event_sys.emit(&B(3)) .await; +/// event_emitter.emit(&B(3)) .await; /// let msg: B = listener.recv().await.unwrap(); /// /// // .... @@ -73,24 +73,24 @@ type Listeners = HashMap { +pub struct EventEmitter { listeners: Mutex>, listener_buffer_size: usize, } -impl EventSys +impl EventEmitter where T: std::hash::Hash + Eq + std::fmt::Debug + Clone, { - /// Creates a new [`EventSys`] - pub fn new() -> Arc> { + /// Creates a new [`EventEmitter`] + pub fn new() -> Arc> { Arc::new(Self { listeners: Mutex::new(HashMap::new()), listener_buffer_size: CHANNEL_BUFFER_SIZE, }) } - /// Creates a new [`EventSys`] with the provided buffer size for the + /// Creates a new [`EventEmitter`] with the provided buffer size for the /// [`EventListener`] channel. /// /// This is important to control the memory used by the listener channel. @@ -100,7 +100,7 @@ where /// starts to consume the buffered events. /// /// If `size` is zero, this function will panic. - pub fn with_buffer_size(size: usize) -> Arc> { + pub fn with_buffer_size(size: usize) -> Arc> { Arc::new(Self { listeners: Mutex::new(HashMap::new()), listener_buffer_size: size, @@ -240,11 +240,11 @@ where } } -/// EventListener listens for and receives events from the [`EventSys`]. +/// EventListener listens for and receives events from the [`EventEmitter`]. pub struct EventListener { id: EventListenerID, recv_chan: Receiver, - event_sys: Weak>, + event_emitter: Weak>, event_id: String, topic: T, phantom: PhantomData, @@ -258,7 +258,7 @@ where /// Creates a new [`EventListener`]. fn new( id: EventListenerID, - event_sys: Weak>, + event_emitter: Weak>, recv_chan: Receiver, event_id: &str, topic: &T, @@ -266,7 +266,7 @@ where Self { id, recv_chan, - event_sys, + event_emitter, event_id: event_id.to_string(), topic: topic.clone(), phantom: PhantomData, @@ -288,9 +288,9 @@ where } } - /// Cancels the event listener and removes it from the [`EventSys`]. + /// Cancels the event listener and removes it from the [`EventEmitter`]. pub async fn cancel(&self) { - if let Some(es) = self.event_sys.upgrade() { + if let Some(es) = self.event_emitter.upgrade() { es.remove(&self.topic, &self.event_id, &self.id).await; } } @@ -306,7 +306,7 @@ where } } -/// An event within the [`EventSys`]. +/// An event within the [`EventEmitter`]. #[derive(Clone, Debug)] pub struct Event { /// The time at which the event was created. @@ -432,18 +432,18 @@ mod tests { } #[test] - fn test_event_sys() { + fn test_event_emitter() { block_on(async move { - let event_sys = EventSys::::new(); + let event_emitter = EventEmitter::::new(); - let a_listener = event_sys.register::(&Topic::TopicA).await; - let b_listener = event_sys.register::(&Topic::TopicB).await; + let a_listener = event_emitter.register::(&Topic::TopicA).await; + let b_listener = event_emitter.register::(&Topic::TopicB).await; - event_sys + event_emitter .emit_by_topic(&Topic::TopicA, &A { a_value: 3 }) .await .expect("Emit event"); - event_sys + event_emitter .emit_by_topic(&Topic::TopicB, &B { b_value: 5 }) .await .expect("Emit event"); @@ -455,17 +455,17 @@ mod tests { assert_eq!(msg, B { b_value: 5 }); // register the same event type to different topics - let c_listener = event_sys.register::(&Topic::TopicC).await; - let d_listener = event_sys.register::(&Topic::TopicD).await; + let c_listener = event_emitter.register::(&Topic::TopicC).await; + let d_listener = event_emitter.register::(&Topic::TopicD).await; - event_sys + event_emitter .emit(&C { c_value: 10 }) .await .expect("Emit event"); let msg = c_listener.recv().await.unwrap(); assert_eq!(msg, C { c_value: 10 }); - event_sys + event_emitter .emit_by_topic(&Topic::TopicD, &C { c_value: 10 }) .await .expect("Emit event"); @@ -473,10 +473,10 @@ mod tests { assert_eq!(msg, C { c_value: 10 }); // register different event types to the same topic - let e_listener = event_sys.register::(&Topic::TopicE).await; - let f_listener = event_sys.register::(&Topic::TopicE).await; + let e_listener = event_emitter.register::(&Topic::TopicE).await; + let f_listener = event_emitter.register::(&Topic::TopicE).await; - event_sys + event_emitter .emit_by_topic(&Topic::TopicE, &E { e_value: 5 }) .await .expect("Emit event"); @@ -484,7 +484,7 @@ mod tests { let msg = e_listener.recv().await.unwrap(); assert_eq!(msg, E { e_value: 5 }); - event_sys + event_emitter .emit_by_topic(&Topic::TopicE, &F { f_value: 5 }) .await .expect("Emit event"); diff --git a/core/src/lib.rs b/core/src/lib.rs index 62052a8..a7192d9 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -13,7 +13,7 @@ pub mod async_util; /// Represents karyon's Core Error. pub mod error; -/// [`event::EventSys`] implementation. +/// [`event::EventEmitter`] implementation. pub mod event; /// A simple publish-subscribe system [`Read More`](./pubsub/struct.Publisher.html) -- cgit v1.2.3