Giter VIP home page Giter VIP logo

Comments (6)

gbj avatar gbj commented on June 16, 2024

Note also that I've resisted this for .update() on signals, because of the fact that event handlers require a function that returns (). Returning something from .update() means that on:click=move |_| count.update(|n| *n += 1) would need to be rewritten across the board to on:click=move |_| { count.update(|n| *n += 1); }, which is worse IMO.

We previously had an .update_returning(), which was deprecated and merged into .try_update()

I would actually suggest that

// note the change to .flatten()
if let Some(v) = stored.try_update_value(|v| v.take()).flatten() {
  // do something with v
}

is the best solution here, as it won't panic.

from leptos.

luxalpa avatar luxalpa commented on June 16, 2024

Returning something from .update() means that on:click=move |_| count.update(|n| *n += 1) would need to be rewritten across the board to on:click=move |_| { count.update(|n| *n += 1); }, which is worse IMO.

I don't think this is true. In my little test:

fn my_update<O>(f: impl FnOnce(&mut i32) -> O) -> O {
    todo!()
}

let _ = view! {
        <div on:click=move |_| my_update(move |v| *v += 1) />
    };

compiled with no problems. I think fn() -> () and fn() are identical.

from leptos.

gbj avatar gbj commented on June 16, 2024

Sorry, I'm talking about the version that returns an Option. .update() currently does not panic if you update a signal that has been disposed. .update_value() does and that should probably be changed. I will not change .update() on signals to panic if disposed and return a value.

from leptos.

luxalpa avatar luxalpa commented on June 16, 2024

Since it seems the idiomatic use case would be to always use try_update in every scenario, what is the use case for update then? And why doesn't the same apply to with which does return the inner value? I think that's rather inconsistent and definitely counter intuitive.

from leptos.

gbj avatar gbj commented on June 16, 2024

And why doesn't the same apply to with which does return the inner value?

The goal of the design decisions here is minimize panicking while making the most-common case the default one. .with() must return a value, or it's meaningless, so it defaults to panicking if disposed, and you can opt into .try_with() if that might be the case. .update() does not need to return a value in the overwhelming majority of cases, so it defaults to not panicking, and you can opt into .try_update() if you want the return value.

Unless I'm mistaken you're suggesting changing the behavior of .update() so that it is essentially .try_update().unwrap(), right? This will increase the likelihood of panicking, which is bad because panics are unrecoverable in WASM.

Since it seems the idiomatic use case would be to always use try_update in every scenario, what is the use case for update then?

I don't understand what you mean here, sorry.

from leptos.

luxalpa avatar luxalpa commented on June 16, 2024

ok, I think I understand the issue with this. Somehow I was under the impression that the callback was called every time, but ofc it makes sense that it's not being called when the signal is disposed.

from leptos.

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.