Giter Club home page Giter Club logo

Comments (8)

quangIO avatar quangIO commented on June 2, 2024

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.

jiker-burce avatar jiker-burce commented on June 2, 2024

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.

jiker-burce avatar jiker-burce commented on June 2, 2024

@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.

quangIO avatar quangIO commented on June 2, 2024

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.

DogeDark avatar DogeDark commented on June 2, 2024

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.

jiker-burce avatar jiker-burce commented on June 2, 2024

@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.

jiker-burce avatar jiker-burce commented on June 2, 2024

@quangIO

  • 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.

quangIO avatar quangIO commented on June 2, 2024

@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 type Lazy<Arc<RwLock<Option<Sender<String>>>>> (use once_cell)
  • Inside ClientForm:
    • Create the (sender, mut receiver) pair
    • FOOBAR_SENDER.write() the assign it to Some(sender)
    • Drop the write lock immediately

Depending on your logic, there could be a better design.

from dioxus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.