Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent Action::pending() state #2652

Open
domwst opened this issue Jun 29, 2024 · 0 comments
Open

Inconsistent Action::pending() state #2652

domwst opened this issue Jun 29, 2024 · 0 comments

Comments

@domwst
Copy link

domwst commented Jun 29, 2024

TLDR;
When several calls of Action are pending, pending() is set to false on the first finished task.

While reading through the sources, I've found out this oddly looking line:

pending_dispatches.set(pending_dispatches.get().saturating_sub(1));

Which decreases pending count instead of increasing it. CSR example below showcases this bug:

#[component]
pub fn App() -> impl IntoView {
    let action = create_action(|v: &i32| {
        let v = *v;
        async move {
            gloo_timers::future::sleep(std::time::Duration::from_secs(4)).await;
            v + 1
        }
    });

    spawn_local(async move {
        action.dispatch(10);
        gloo_timers::future::sleep(std::time::Duration::from_secs(2)).await;
        action.dispatch(41);
    });

    view! {
        <div class="container">
            <h1>"Computation"</h1>
            <span>"Computation result: " {action.value()}</span>
            <span>
            { move ||
                if action.pending()() { "Loading..." } else { "" }
            }
            </span>
        </div>
    }
}

After the action.value() becomes 11, the "Loading..." text disappears and only after 2 seconds the action.value() becomes 42.

I've prepared PR, that fixes this.

Leptos Dependencies
N/A

To Reproduce
See above

Expected behavior
action.pending() to represent pending state of an Action

Screenshots
N/A

Additional context
N/A

domwst added a commit to domwst/leptos that referenced this issue Jun 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant