dioxus_std/utils/channel/
use_channel.rs1use async_broadcast::{broadcast, InactiveReceiver, Receiver, SendError, Sender, TrySendError};
2use dioxus::prelude::*;
3use uuid::Uuid;
4
5#[derive(Clone, Copy)]
7pub struct UseChannel<MessageType: Clone + 'static> {
8 id: Uuid,
9 sender: Signal<Sender<MessageType>>,
10 inactive_receiver: Signal<InactiveReceiver<MessageType>>,
11}
12
13impl<T: Clone> PartialEq for UseChannel<T> {
14 fn eq(&self, other: &Self) -> bool {
15 self.id == other.id
16 }
17}
18
19impl<MessageType: Clone + 'static> UseChannel<MessageType> {
20 pub fn try_send(&self, msg: impl Into<MessageType>) -> Result<(), TrySendError<MessageType>> {
22 self.sender.peek().try_broadcast(msg.into()).map(|_| ())
23 }
24
25 pub async fn send(&self, msg: impl Into<MessageType>) -> Result<(), SendError<MessageType>> {
27 self.sender.peek().broadcast(msg.into()).await.map(|_| ())
28 }
29
30 pub fn receiver(&mut self) -> Receiver<MessageType> {
33 self.inactive_receiver.peek().clone().activate()
34 }
35}
36
37pub fn use_channel<MessageType: Clone + 'static>(size: usize) -> UseChannel<MessageType> {
39 use_hook(|| {
40 let id = Uuid::new_v4();
41 let (sender, receiver) = broadcast::<MessageType>(size);
42 UseChannel {
43 id,
44 sender: Signal::new(sender),
45 inactive_receiver: Signal::new(receiver.deactivate()),
46 }
47 })
48}