leptos-rs / leptos

Build fast web applications with Rust.
https://leptos.dev
MIT License
16.01k stars 627 forks source link

Add a way to clear the value of an action #2364

Open ChocolateLoverRaj opened 7 months ago

ChocolateLoverRaj commented 7 months ago

Is your feature request related to a problem? Please describe. I have a web page which connects to a USB or BLE device using an action:

let connect = create_action(|connection_type: &ConnectionType| {
        let connection_type = connection_type.to_owned();
        async move {
            let result = match connection_type {
                ConnectionType::Usb => UsbConnectionBuilder::connect().await,
                ConnectionType::Ble => BleConnectionBuilder::connect().await,
            }
            .map(|b| Rc::new(b));
            if let Err(e) = result.as_ref() {
                console_log!("Error connecting with {:#?}: {:#?}", connection_type, e);
            }
            result
        }
    });

    let connection = connect.value();

I want a way of clearing the value of an action when a "Disconnect" button is clicked.

Describe the solution you'd like A Action::clear_value function or something similar

Describe alternatives you've considered Create a signal that is a number. If the number is greater than the action version, it means that the action's result should be ignored. When the "Disconnect" button is pressed, increase the number.

Additional context My project that uses Leptos: https://github.com/ChocolateLoverRaj/rust-esp32c3-examples/tree/sensor-connect-web/sensor-connect

gbj commented 7 months ago

PR welcome to add it. Another possibility is to use a resource, which does allow mutation.

ChocolateLoverRaj commented 7 months ago

In my case I can't use a resource because resources will execute the async function right away, but I want it to execute when a button is clicked.

caioluis commented 6 months ago

@leorsousa05 and I will have a look at it in pair programming. Let us know if there was any progress since your last comment, @ChocolateLoverRaj.

ChocolateLoverRaj commented 6 months ago

I didn't start making a PR at all. I'm working on other programming projects rn. You don't need to wait for me.

leorsousa05 commented 6 months ago

I didn't start making a PR at all. I'm working on other programming projects rn. You don't need to wait for me.

We will probably start on monday. Good projects by the way!