pub struct Publisher<T> {
subs: Mutex<HashMap<SubscriptionID, Sender<T>>>,
subscription_buffer_size: usize,
}
Expand description
A simple publish-subscribe system.
use karyon_core::pubsub::{Publisher};
async {
let publisher = Publisher::new();
let sub = publisher.subscribe().await;
publisher.notify(&String::from("MESSAGE")).await;
let msg = sub.recv().await;
// ....
};
Fields§
§subs: Mutex<HashMap<SubscriptionID, Sender<T>>>
§subscription_buffer_size: usize
Implementations§
Source§impl<T: Clone> Publisher<T>
impl<T: Clone> Publisher<T>
Sourcepub fn with_buffer_size(size: usize) -> Arc<Publisher<T>>
pub fn with_buffer_size(size: usize) -> Arc<Publisher<T>>
Creates a new Publisher
with the provided buffer size for the
Subscription
channel.
This is important to control the memory used by the Subscription
channel.
If the subscriber can’t keep up with the new messages coming, then the
channel buffer will fill with new messages, and if the buffer is full,
the emit function will block until the subscriber starts to process
the buffered messages.
If size
is zero, this function will panic.
Sourcepub async fn subscribe(self: &Arc<Self>) -> Subscription<T>
pub async fn subscribe(self: &Arc<Self>) -> Subscription<T>
Subscribes and return a Subscription
Sourcepub async fn unsubscribe(self: &Arc<Self>, id: &SubscriptionID)
pub async fn unsubscribe(self: &Arc<Self>, id: &SubscriptionID)
Unsubscribes by providing subscription id
Auto Trait Implementations§
impl<T> !Freeze for Publisher<T>
impl<T> !RefUnwindSafe for Publisher<T>
impl<T> Send for Publisher<T>where
T: Send,
impl<T> Sync for Publisher<T>where
T: Send,
impl<T> Unpin for Publisher<T>
impl<T> UnwindSafe for Publisher<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more