Skip to content

Commit

Permalink
Allow to consume deps in use_callback (#2617)
Browse files Browse the repository at this point in the history
  • Loading branch information
jetli committed Apr 15, 2022
1 parent d2c3685 commit 58db53a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
24 changes: 19 additions & 5 deletions packages/yew/src/functional/hooks/use_callback.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::rc::Rc;

use crate::callback::Callback;
use crate::functional::{hook, use_memo};

Expand Down Expand Up @@ -37,15 +39,17 @@ use crate::functional::{hook, use_memo};
/// // This callback depends on (), so it's created only once, then MyComponent
/// // will be rendered only once even when you click the button mutiple times.
/// let callback = use_callback(
/// move |name| format!("Hello, {}!", name),
/// move |name, _| format!("Hello, {}!", name),
/// ()
/// );
///
/// // It can also be used for events.
/// // It can also be used for events, this callback depends on `counter`.
/// let oncallback = {
/// let counter = counter.clone();
/// use_callback(
/// move |_e| (),
/// move |_e, counter| {
/// let _ = **counter;
/// },
/// counter
/// )
/// };
Expand All @@ -68,8 +72,18 @@ pub fn use_callback<IN, OUT, F, D>(f: F, deps: D) -> Callback<IN, OUT>
where
IN: 'static,
OUT: 'static,
F: Fn(IN) -> OUT + 'static,
F: Fn(IN, &D) -> OUT + 'static,
D: PartialEq + 'static,
{
(*use_memo(move |_| Callback::from(f), deps)).clone()
let deps = Rc::new(deps);

(*use_memo(
move |deps| {
let deps = deps.clone();
let f = move |value: IN| f(value, deps.as_ref());
Callback::from(f)
},
deps,
))
.clone()
}
2 changes: 1 addition & 1 deletion packages/yew/tests/use_callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async fn use_callback_works() {
fn use_callback_comp() -> Html {
let state = use_state(|| 0);

let callback = use_callback(move |name| format!("Hello, {}!", name), ());
let callback = use_callback(move |name, _| format!("Hello, {}!", name), ());

use_effect(move || {
if *state < 5 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ fn callback() -> Html {
// This callback depends on (), so it's created only once, then MyComponent
// will be rendered only once even when you click the button mutiple times.
let callback = use_callback(
move |name| format!("Hello, {}!", name),
move |name, _| format!("Hello, {}!", name),
()
);

// It can also be used for events.
// It can also be used for events, this callback depends on `counter`.
let oncallback = {
let counter = counter.clone();
use_callback(
move |_e| (),
move |_e, counter| {
let _ = **counter;
},
counter
)
};
Expand Down

1 comment on commit 58db53a

@github-actions
Copy link

Choose a reason for hiding this comment

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

Yew master branch benchmarks (Lower is better)

Benchmark suite Current: 58db53a Previous: d2c3685 Ratio
yew-struct-keyed 01_run1k 162.3635 189.8065 0.86
yew-struct-keyed 02_replace1k 187.0155 195.83 0.95
yew-struct-keyed 03_update10th1k_x16 329.96500000000003 345.147 0.96
yew-struct-keyed 04_select1k 53.821 54.781 0.98
yew-struct-keyed 05_swap1k 77.1215 78.62049999999999 0.98
yew-struct-keyed 06_remove-one-1k 25.424999999999997 28.857 0.88
yew-struct-keyed 07_create10k 2912.3915 2931.251 0.99
yew-struct-keyed 08_create1k-after1k_x2 409.2745 436.16 0.94
yew-struct-keyed 09_clear1k_x8 186.4505 186.9615 1.00
yew-struct-keyed 21_ready-memory 1.457233428955078 1.457233428955078 1
yew-struct-keyed 22_run-memory 1.6556472778320312 1.659870147705078 1.00
yew-struct-keyed 23_update5-memory 1.6958503723144531 1.6978225708007812 1.00
yew-struct-keyed 24_run5-memory 1.944721221923828 1.9438705444335935 1.00
yew-struct-keyed 25_run-clear-memory 1.3280715942382812 1.328044891357422 1.00
yew-struct-keyed 31_startup-ci 1839.414 1732.196 1.06
yew-struct-keyed 32_startup-bt 31.728 32.94800000000001 0.96
yew-struct-keyed 33_startup-mainthreadcost 218.8160000000001 239.65199999999996 0.91
yew-struct-keyed 34_startup-totalbytes 328.7392578125 328.7392578125 1

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.