Skip to content

Commit 4aa0540

Browse files
authored
Add use_throttle_effect (#7)
1 parent ab87ffb commit 4aa0540

File tree

8 files changed

+133
-1
lines changed

8 files changed

+133
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ fn counter() -> Html {
9898
- `use_debounce` - debounces a function.
9999
- `use_debounce_effect` - debounces an effect.
100100
- `use_throttle` - throttles a function.
101+
- `use_throttle_effect` - throttles an effect.
101102

102103
### Lifecycles
103104

crates/yew-hooks/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "yew-hooks"
3-
version = "0.1.51"
3+
version = "0.1.52"
44
edition = "2018"
55
authors = ["Jet Li <[email protected]>"]
66
categories = ["gui", "wasm", "web-programming"]

crates/yew-hooks/src/hooks/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ mod use_size;
3939
mod use_state_ptr_eq;
4040
mod use_swipe;
4141
mod use_throttle;
42+
mod use_throttle_effect;
4243
mod use_throttle_state;
4344
mod use_timeout;
4445
mod use_title;
@@ -90,6 +91,7 @@ pub use use_size::*;
9091
pub use use_state_ptr_eq::*;
9192
pub use use_swipe::*;
9293
pub use use_throttle::*;
94+
pub use use_throttle_effect::*;
9395
pub use use_throttle_state::*;
9496
pub use use_timeout::*;
9597
pub use use_title::*;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
use yew::prelude::*;
2+
3+
use super::{use_throttle, use_unmount};
4+
5+
/// A hook that throttles calling effect callback, it is only called once every `millis`.
6+
///
7+
/// # Example
8+
///
9+
/// ```rust
10+
/// # use yew::prelude::*;
11+
/// #
12+
/// use yew_hooks::{use_throttle_effect, use_update};
13+
///
14+
/// #[function_component(ThrottleEffect)]
15+
/// fn throttle_effect() -> Html {
16+
/// let state = use_state(|| 0);
17+
/// let update = use_update();
18+
///
19+
/// {
20+
/// let state = state.clone();
21+
/// use_throttle_effect(
22+
/// move || {
23+
/// state.set(*state + 1);
24+
/// },
25+
/// 2000,
26+
/// )
27+
/// };
28+
///
29+
/// let onclick = { Callback::from(move |_| update()) };
30+
///
31+
/// html! {
32+
/// <>
33+
/// <button {onclick}>{ "Click fast!" }</button>
34+
/// <b>{ "State: " }</b> {*state}
35+
/// </>
36+
/// }
37+
/// }
38+
/// ```
39+
pub fn use_throttle_effect<Callback>(callback: Callback, millis: u32)
40+
where
41+
Callback: FnMut() + 'static,
42+
{
43+
let throttle = use_throttle(callback, millis);
44+
45+
{
46+
let throttle = throttle.clone();
47+
use_effect(move || {
48+
throttle.run();
49+
50+
|| ()
51+
});
52+
}
53+
54+
use_unmount(move || {
55+
throttle.cancel();
56+
});
57+
}
58+
59+
/// This hook is similar to [`use_throttle_effect`] but it accepts dependencies.
60+
///
61+
/// Whenever the dependencies are changed, the throttle effect is run again.
62+
/// To detect changes, dependencies must implement `PartialEq`.
63+
pub fn use_throttle_effect_with_deps<Callback, Dependents>(
64+
callback: Callback,
65+
millis: u32,
66+
deps: Dependents,
67+
) where
68+
Callback: FnMut() + 'static,
69+
Dependents: PartialEq + 'static,
70+
{
71+
let throttle = use_throttle(callback, millis);
72+
73+
{
74+
let throttle = throttle.clone();
75+
use_effect_with_deps(
76+
move |_| {
77+
throttle.run();
78+
79+
|| ()
80+
},
81+
deps,
82+
);
83+
}
84+
85+
use_unmount(move || {
86+
throttle.cancel();
87+
});
88+
}

examples/yew-app/src/routes/home.rs

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub fn home() -> Html {
4444
<li><Link<AppRoute> to={AppRoute::UseDebounce} classes="app-link" >{ "use_debounce" }</Link<AppRoute>> { " - debounces a function." }</li>
4545
<li><Link<AppRoute> to={AppRoute::UseDebounceEffect} classes="app-link" >{ "use_debounce_effect" }</Link<AppRoute>> { " - debounces an effect." }</li>
4646
<li><Link<AppRoute> to={AppRoute::UseThrottle} classes="app-link" >{ "use_throttle" }</Link<AppRoute>> { " - throttles a function." }</li>
47+
<li><Link<AppRoute> to={AppRoute::UseThrottleEffect} classes="app-link" >{ "use_throttle_effect" }</Link<AppRoute>> { " - throttles an effect." }</li>
4748
</ul>
4849

4950
<h2>{ "Lifecycles" }</h2>

examples/yew-app/src/routes/hooks/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ mod use_size;
4141
mod use_state_ptr_eq;
4242
mod use_swipe;
4343
mod use_throttle;
44+
mod use_throttle_effect;
4445
mod use_throttle_state;
4546
mod use_timeout;
4647
mod use_title;
@@ -94,6 +95,7 @@ pub use use_size::*;
9495
pub use use_state_ptr_eq::*;
9596
pub use use_swipe::*;
9697
pub use use_throttle::*;
98+
pub use use_throttle_effect::*;
9799
pub use use_throttle_state::*;
98100
pub use use_timeout::*;
99101
pub use use_title::*;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use yew::prelude::*;
2+
3+
use yew_hooks::{use_throttle_effect, use_update};
4+
5+
/// `use_throttle_effect` demo
6+
#[function_component(UseThrottleEffect)]
7+
pub fn throttle_effect() -> Html {
8+
let state = use_state(|| 0);
9+
let update = use_update();
10+
11+
{
12+
let state = state.clone();
13+
use_throttle_effect(
14+
move || {
15+
state.set(*state + 1);
16+
},
17+
2000,
18+
)
19+
};
20+
21+
let onclick = { Callback::from(move |_| update()) };
22+
23+
html! {
24+
<div class="app">
25+
<header class="app-header">
26+
<div>
27+
<button {onclick}>{ "Click fast!" }</button>
28+
<p>
29+
<b>{ "State: " }</b> {*state}
30+
</p>
31+
</div>
32+
</header>
33+
</div>
34+
}
35+
}

examples/yew-app/src/routes/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ pub enum AppRoute {
118118
UseThrottleState,
119119
#[at("/use_debounce_effect")]
120120
UseDebounceEffect,
121+
#[at("/use_throttle_effect")]
122+
UseThrottleEffect,
121123
#[not_found]
122124
#[at("/page-not-found")]
123125
PageNotFound,
@@ -182,6 +184,7 @@ pub fn switch(routes: &AppRoute) -> Html {
182184
AppRoute::UseThrottle => html! { <UseThrottle /> },
183185
AppRoute::UseThrottleState => html! { <UseThrottleState /> },
184186
AppRoute::UseDebounceEffect => html! { <UseDebounceEffect /> },
187+
AppRoute::UseThrottleEffect => html! { <UseThrottleEffect /> },
185188
AppRoute::PageNotFound => html! { <Home /> },
186189
}
187190
}

0 commit comments

Comments
 (0)