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

Allow to consume deps in use_callback hook #2617

Merged
merged 1 commit into from Apr 15, 2022

Conversation

jetli
Copy link
Contributor

@jetli jetli commented Apr 15, 2022

Description

This pr refactors use_callback hook to allows to consume deps, e.g.

    let onclick = {
        let counter = counter.clone();
        use_callback(
            move |_e, counter| {
                // Consume the deps `counter` here.
                let _ = **counter;
            },
            counter
        )
    };

This is a break change of previous design, the signature of use_callback changes from use_callback(|in_type| {}, deps); to use_callback(|in_type, deps| {}, deps);.

Checklist

  • I have run cargo make pr-flow
  • I have reviewed my own code
  • I have added tests

@futursolo futursolo added breaking change A-yew Area: The main yew crate labels Apr 15, 2022
@github-actions
Copy link

github-actions bot commented Apr 15, 2022

Visit the preview URL for this PR (updated for commit 7e9951d):

https://yew-rs--pr2617-refactor-use-callbac-8kuzgr97.web.app

(expires Fri, 22 Apr 2022 16:14:19 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

@jetli
Copy link
Contributor Author

jetli commented Apr 15, 2022

@futursolo We talked about this in my previous PR, now I'm changing the signature of use_callback from use_callback(|in_type| {}, deps); to use_callback(|in_type, deps| {}, deps);, so we can easily to consume deps in function body, eliminate extra clone/move for deps. But the signature is a bit more complicated than before, so we consider this again to determine if this will be more convenient, any advice?

@github-actions
Copy link

Size Comparison

examples master (KB) pull request (KB) diff
boids 173.301 173.301 0
contexts 111.393 111.393 0
counter 86.998 86.998 0
counter_functional 87.473 87.473 0
dyn_create_destroy_apps 90.104 90.104 0
file_upload 102.929 102.929 0
function_memory_game 169.999 169.999 0
function_router 351.643 351.643 0
function_todomvc 161.887 161.887 0
futures 226.574 226.574 0
game_of_life 108.032 108.032 0
inner_html 83.506 83.506 0
js_callback 92.103 92.103 0
keyed_list 195.865 195.865 0
mount_point 86.514 86.514 0
nested_list 115.569 115.569 0
node_refs 89.831 89.831 0
password_strength 1539.505 1539.505 0
portals 96.905 96.905 0
router 316.716 316.716 0
simple_ssr 499.814 499.814 0
ssr_router 425.162 425.162 0
suspense 110.433 110.433 0
timer 89.712 89.712 0
todomvc 143.693 143.693 0
two_apps 87.581 87.581 0
webgl 87.124 87.124 0

Copy link
Member

@futursolo futursolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the signature is a bit more complicated than before,

I think in practice, a dependency for use_callback is almost always desirable.

So, this looks good to me.

Thank you.

Copy link
Member

@hamza1311 hamza1311 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thanks for implementing it

@hamza1311 hamza1311 merged commit 58db53a into yewstack:master Apr 15, 2022
@jetli jetli deleted the refactor_use_callback branch April 15, 2022 21:45
D: PartialEq + 'static,
{
(*use_memo(move |_| Callback::from(f), deps)).clone()
let deps = Rc::new(deps);
Copy link
Contributor

@laizy laizy Apr 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can use D: PartialEq + 'static + Clone to avoid this allocation, since in most cases the deps have already implemented Clone.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Rc Clone is usually more cheap than Clone of deps, as Rc is just like a pointer, and we don't want to constrain Clone to end user. and let's be consistent with use_state_eq/use_effect_with_deps etc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Not really, since this hook is used in the case that deps is not changed frequently, assume the ratio is 100:1. when this hook is called 100 times, using Rc needs 100 alloc + 100 dealloc, but using Clone just needs 1 deps.clone since the Callback is memoized.
  2. For consistence, the use_context hook also uses Clone, so this is not a special one.
  3. All the deps params of use_effect_with_deps in this repo implement Clone, so users are not constrained actually.
  4. If in some rare cases users provide a not Clone deps, they can call Rc::new at the calling site.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-yew Area: The main yew crate breaking change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants