how to send channel to child component #2401
Replies: 9 comments 1 reply
-
For Clone, you can wrap the receiver around Arc. For mutable, you use Mutex: Arc<Mutex<..>>. I am not sure if it gets you the desirable semantic though |
Beta Was this translation helpful? Give feedback.
-
thanks for the reply, but I don't think use the |
Beta Was this translation helpful? Give feedback.
-
@jkelleyrtp |
Beta Was this translation helpful? Give feedback.
-
I commented because I used a similar pattern in my application (no Mutex though) ;)
EDIT: The easier option is don't move the receiver to the child component at all. You can simply receive data in Client and update the ClientForm |
Beta Was this translation helpful? Give feedback.
-
You could wrap the receiver in a Signal: let recv = Signal::new(receive_rx);
rsx! {
ClientForm { receive_rx: recv, send_tx: send_tx }
} There are also a few hooks like |
Beta Was this translation helpful? Give feedback.
-
@DogeDark that could not work let recv = Signal::new(receive_rx);
let send = Signal::new(send_tx);
rsx! {
ClientForm{receive_rx: recv, send_tx: send}
}
#[component]
fn ClientForm(mut receive_rx: Signal<Receiver<String>>, mut send_tx: Signal<Sender<String>>) -> Element {
spawn(async move {
let mut recv = receive_rx.take();
while let Some(message) = recv.recv().await {
messages.push(Message{user: current_user.clone(), message },);
}
});
}
error[E0277]: the trait bound `tokio::sync::mpsc::Receiver<String>: std::default::Default` is not satisfied
--> src/libs/client_lib.rs:59:35
|
59 | let mut recv = receive_rx.take();
| ^^^^ the trait `std::default::Default` is not implemented for `tokio::sync::mpsc::Receiver<String>`
| |
Beta Was this translation helpful? Give feedback.
-
let recv = Arc::new(Mutex::new(receive_rx));
let send = Arc::new(Mutex::new(send_tx));
rsx! {
ClientForm{receive_rx: recv, send_tx: send}
}
#[component]
fn ClientForm(mut receive_rx: Arc<Mutex<Receiver<String>>>, mut send_tx: Signal<Sender<String>>) -> Element {}
Trait `PartialEq` is not implemented for `Arc<Mutex<Receiver<String>>>` |
Beta Was this translation helpful? Give feedback.
-
@jiker-burce You can make both compile. Let read the documentation and the error message carefully. Props in Dioxus require Partial and Clone trait to be implemented: you cannot pass the signal directly if you don't implement them. So you can wrap them in a wrapper type. All solutions above can be build-able (doesn't mean I am sure it will just work however): Mutex#[derive(Clone, Debug)]
struct ClientFormOptions {
receiver: Arc<Mutex<Receiver<String>>>,
send_tx: Sender<String>,
}
impl PartialEq for ClientFormOptions {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.receiver, &other.receiver) && self.send_tx.same_channel(&other.send_tx)
}
}
#[component]
fn ClientForm(options: ClientFormOptions) -> Element {
spawn(async move {
let receiver = options.receiver.lock().unwrap();
while let Some(message) = receiver.recv().await {
// messages.push(Message{user: current_user.clone(), message },);
}
});
todo!()
} FYI You don't need mut for Sender but I will keep your code as is. SignalSignal also requires lock (i.e. this may not what you want) #[component]
fn ClientFormX(mut receive_rx: Signal<Receiver<String>>, mut send_tx: Signal<Sender<String>>) -> Element {
spawn(async move {
let mut recv = receive_rx.write();
while let Some(message) = recv.recv().await {
// messages.push(Message{user: current_user.clone(), message },);
}
});
todo!()
} The alternative for this is using receiver in the parent component and signal the child to change. In other words, don't move the receiver to the child. Should you use any of these?You decide but I wouldn't use your current pattern at all. First alternative optionSee my edit in my comment above
Second alternative option
Depending on your logic, there could be a better design. |
Beta Was this translation helpful? Give feedback.
-
@quangIO thanks for your help, here is my original Desktop Chat Software. while let Some(message) = receive_rx.recv().await {
messages.push(Message{user: current_user.clone(), message },);
} do you have any wonderful idea? |
Beta Was this translation helpful? Give feedback.
-
Problem
Steps To Reproduce
Steps to reproduce the behavior:
Expected behavior
Screenshots
If applicable, add screenshots to help explain your problem.
Environment:
desktop
Beta Was this translation helpful? Give feedback.
All reactions