-
Notifications
You must be signed in to change notification settings - Fork 6
The file is updated #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shitcodebykaushik
wants to merge
5
commits into
igumnoff:main
Choose a base branch
from
shitcodebykaushik:broacdcast
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
861afb8
The file is updated
shitcodebykaushik 71eabf2
The varsion is update of the dashmap
shitcodebykaushik 0d70cc1
The file is ready to go
shitcodebykaushik 068ad25
This should be visible
shitcodebykaushik 863ee9f
Delete e dashmapDashMap
shitcodebykaushik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,8 @@ | ||
| use std::future::Future; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| use std::pin::Pin; | ||
| use dashmap::DashMap; | ||
|
|
||
| use crate::{SSSD}; | ||
|
|
||
| use std::collections::HashMap; | ||
| use crate::{SSSD}; // Replace with your actual crate items | ||
|
|
||
| use std::sync::{ | ||
| atomic::{AtomicUsize, Ordering}, | ||
|
|
@@ -12,27 +11,23 @@ use std::sync::{ | |
|
|
||
| use tokio::{ | ||
| sync::{ | ||
| mpsc::{ | ||
| self, | ||
| error::{SendError}, | ||
| Sender | ||
| }, | ||
| Mutex | ||
| } | ||
| mpsc::{self, error::SendError, Sender}, | ||
| }, | ||
| }; | ||
|
|
||
|
|
||
| use dashmap::DashMap; | ||
|
|
||
| /// Marker-trait for events | ||
| pub trait Event: Copy + Clone + SSSD {} | ||
| impl <S> Event for S where S: Copy + Clone + SSSD {} | ||
| pub trait Event: Copy + Clone + SSSD {} // Replace SSSD with your actual trait | ||
|
|
||
| impl<S> Event for S where S: Copy + Clone + SSSD {} | ||
|
|
||
| /// `EventCallback` is a marker-trait for callback which will be stored in subscribers | ||
| /// Auto-implemented | ||
| pub trait EventCallback<E>: Send + Sync + 'static { | ||
| fn call<'a>(&'a self, event: E) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> | ||
| where | ||
| Self: 'a; | ||
| where | ||
| Self: 'a; | ||
| } | ||
|
|
||
| impl<F, E, Fut> EventCallback<E> for F | ||
|
|
@@ -42,47 +37,47 @@ where | |
| Fut: Future<Output = ()> + Send + 'static, | ||
| { | ||
| fn call<'a>(&'a self, event: E) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> | ||
| where | ||
| Self: 'a, | ||
| where | ||
| Self: 'a, | ||
| { | ||
| Box::pin(async move { | ||
| self(event).await; | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| /// # Event Bus | ||
| // # Event Bus | ||
| /// | ||
| /// `EventBus<E,R>`, where `E` implements `Event` marker trait `R` `implements EventCallback<E>` | ||
| /// `EventBus<E>`, where `E` implements `Event` | ||
| /// | ||
| /// `EventBus` accessible from all actors | ||
| /// | ||
| /// on `new()` Event Bus spawnin background task with loop | ||
| /// on `new()` Event Bus spawning background task with loop | ||
| /// which is checking for events | ||
| /// | ||
| /// ## Fields | ||
| /// | ||
| /// * `subscribers` - HashMap that contains id of subsriber and tuple of (Event, Callback) which is executes on event (Variant of `E`) | ||
| /// * `subscribers` - DashMap that contains id of subscriber and tuple of (Event, Callback) which is executed on event (Variant of `E`) | ||
| /// | ||
| /// * `events` - Vector of all events | ||
| /// * `tx` - Sender channel for publishing events | ||
| /// | ||
| /// * `counter` - Atomic counter for generating subscriber IDs | ||
| /// | ||
| /// ## Methods | ||
| /// | ||
| /// * `new` - Creating new instance of `EventBus` | ||
| /// | ||
| /// * `publish` - Sends an event to a | ||
| /// * `publish` - Sends an event to all subscribers | ||
| /// | ||
| /// * `subscribe` - Creates record in `subscribers` of enum variant and callback to that variant, returns subscriber's id | ||
| /// and unwinding event stack, it's promissing that subscribers will react on events which are published | ||
| /// after subscription only | ||
| /// * `subscribe` - Registers a callback for a specific event type | ||
| /// | ||
| /// * `unsubscribe` - Remove record from `subscribers` | ||
| /// * `unsubscribe` - Removes a subscriber based on subscriber ID | ||
| /// | ||
| pub struct EventBus<E> | ||
| where | ||
| E: Event, | ||
| { | ||
| subscribers: Arc<Mutex<HashMap<usize, Pin<Box<dyn EventCallback<E>>>>>>, | ||
| subscribers: Arc<DashMap<usize, Pin<Box<dyn EventCallback<E>>>>>, | ||
| tx: Sender<E>, | ||
| counter: AtomicUsize, | ||
| } | ||
|
|
@@ -91,12 +86,11 @@ impl<E> EventBus<E> | |
| where | ||
| E: Event, | ||
| { | ||
| // FIXME: Calls function only once for some reason... | ||
| pub fn new() -> Self { | ||
| let (tx, mut rx) = mpsc::channel(1000); | ||
|
|
||
| let event_bus = Self { | ||
| subscribers: Arc::new(Mutex::new(HashMap::new())), | ||
| subscribers: Arc::new(DashMap::new()), | ||
| tx: tx, | ||
| counter: AtomicUsize::new(0), | ||
| }; | ||
|
|
@@ -107,13 +101,11 @@ where | |
| let handle = tokio::runtime::Handle::current(); | ||
| { | ||
| let _join_handler = handle.spawn(async move { | ||
| loop { | ||
| while let Some(event) = rx.recv().await { | ||
| let subscribers = subs.lock().await; | ||
| log::trace!("processing event: {:?}", event); | ||
| for (_id, callback) in &*subscribers { | ||
| callback.call(event).await; | ||
| } | ||
| while let Some(event) = rx.recv().await { | ||
| let subscribers = subs.iter(); | ||
| log::trace!("processing event: {:?}", event); | ||
| for (_id, callback) in subscribers { | ||
| callback.call(event).await; | ||
| } | ||
| } | ||
| }); | ||
|
|
@@ -132,29 +124,18 @@ where | |
| where | ||
| F: EventCallback<E>, | ||
| { | ||
|
|
||
| let id = self.counter.fetch_add(1, Ordering::SeqCst); | ||
| log::trace!("New subscriber in event bus. id: {}", id); | ||
|
|
||
| let mut subscribers = self.subscribers.lock().await; | ||
| subscribers.insert(id, Box::pin(callback)); | ||
| self.subscribers.insert(id, Box::pin(callback)); | ||
|
|
||
| id | ||
| } | ||
|
|
||
| pub async fn unsubscribe(&self, subscriber_id: usize) { | ||
|
|
||
| log::trace!("Removed subscriber in event bus. id: {}", subscriber_id); | ||
|
|
||
| let mut subscribers = self.subscribers.lock().await; | ||
| subscribers.remove(&subscriber_id); | ||
| self.subscribers.remove(&subscriber_id); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| // impl<E> Default for EventBus<E> | ||
| // where E: Event, | ||
| // { | ||
| // fn default() -> Self { | ||
| // Self::new() | ||
| // } | ||
| // } | ||
| // Revie this code | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this dependency should be optional.
Only included for "broadcast" feature.
Check for example:
remote = ["tokio/net", "bincode"]