use async_broadcast::{broadcast, InactiveReceiver, Receiver, SendError, Sender, TrySendError};
use dioxus::prelude::*;
use uuid::Uuid;
#[derive(Clone, Copy)]
pub struct UseChannel<MessageType: Clone + 'static> {
id: Uuid,
sender: Signal<Sender<MessageType>>,
inactive_receiver: Signal<InactiveReceiver<MessageType>>,
}
impl<T: Clone> PartialEq for UseChannel<T> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl<MessageType: Clone + 'static> UseChannel<MessageType> {
pub fn try_send(&self, msg: impl Into<MessageType>) -> Result<(), TrySendError<MessageType>> {
self.sender.peek().try_broadcast(msg.into()).map(|_| ())
}
pub async fn send(&self, msg: impl Into<MessageType>) -> Result<(), SendError<MessageType>> {
self.sender.peek().broadcast(msg.into()).await.map(|_| ())
}
pub fn receiver(&mut self) -> Receiver<MessageType> {
self.inactive_receiver.peek().clone().activate()
}
}
pub fn use_channel<MessageType: Clone + 'static>(size: usize) -> UseChannel<MessageType> {
use_hook(|| {
let id = Uuid::new_v4();
let (sender, receiver) = broadcast::<MessageType>(size);
UseChannel {
id,
sender: Signal::new(sender),
inactive_receiver: Signal::new(receiver.deactivate()),
}
})
}