Comments (8)
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
from dioxus.
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
thanks for the reply, but I don't think use the Arc<Mutex>
is a good idea, it will block the channel, the performance will be declined.
from dioxus.
@jkelleyrtp
so sorry to disturb you, could you give me any advice, does the dioxus framework has some components could share all kinds of data in the whole proggrame?
from dioxus.
I commented because I used a similar pattern in my application (no Mutex though) ;)
- There should be only one mutable borrow that actually receive data then performance is not a concern but I don't know Dioxus internal to know if this is actually safe (as in deadlock and stuff)
- You can use different channel than tokio::mpsc which does not require
mut
-> wrapping receiver in Arc is enough only - If you want global access, use once_cell or lazy_static
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
from dioxus.
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 use_coroutine
that might be better suited although I'm unsure if they'd have the same issue.
from dioxus.
@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>`
|
from dioxus.
- it also cant' work. ^_^
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>>>`
from dioxus.
@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.
Signal
Signal 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 option
See my edit in my comment above
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
Second alternative option
- Have a global object
FOOBAR_SENDER
of typeLazy<Arc<RwLock<Option<Sender<String>>>>>
(use once_cell) - Inside ClientForm:
- Create the (sender, mut receiver) pair
FOOBAR_SENDER.write()
the assign it toSome(sender)
- Drop the write lock immediately
Depending on your logic, there could be a better design.
from dioxus.
Related Issues (20)
- Manganis CSS Hot Reloading
- Request for `useLayoutEffect` for the web renderer HOT 1
- Hotreload doesn't pierce through macro contents HOT 5
- Support event handlers in element extensions
- Resource does not restart from async event handler HOT 1
- Request example with various controls HOT 1
- Can't match Resource.value() as shown in documentation. HOT 2
- bug: `cargo clippy` fails to run in Dioxus 0.5.1 apparently
- Opinion: `Signal` should not be callable HOT 2
- I can't run the code generated by `dx new` HOT 3
- i18n runs multiple times HOT 1
- Unable to do 'dx serve' on nixos if mio is a dependency HOT 2
- Implement multiple optional attributes for components
- use_effect with value HOT 3
- no `use_muda_event_handler` in the root HOT 1
- Switch to a generational map for Tasks
- Support prop extensions for callbacks
- Router error when rendering app via dioxus_ssr
- Support for Web API / WebRTC
- Multiple i18n concatenation HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dioxus.