mirror of
https://github.com/adoyle0/leptos-use.git
synced 2025-01-23 09:09:21 -05:00
use_interval callback as box
This commit is contained in:
parent
c222376dd5
commit
18ff2f49e1
5 changed files with 47 additions and 39 deletions
1
.idea/leptos-use.iml
generated
1
.idea/leptos-use.iml
generated
|
@ -32,6 +32,7 @@
|
||||||
<sourceFolder url="file://$MODULE_DIR$/examples/on_click_outside/src" isTestSource="false" />
|
<sourceFolder url="file://$MODULE_DIR$/examples/on_click_outside/src" isTestSource="false" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/examples/use_abs/src" isTestSource="false" />
|
<sourceFolder url="file://$MODULE_DIR$/examples/use_abs/src" isTestSource="false" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/examples/use_interval_fn/src" isTestSource="false" />
|
<sourceFolder url="file://$MODULE_DIR$/examples/use_interval_fn/src" isTestSource="false" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/examples/use_interval/src" isTestSource="false" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/examples/use_event_listener/target" />
|
<excludeFolder url="file://$MODULE_DIR$/examples/use_event_listener/target" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/docs/book/book" />
|
<excludeFolder url="file://$MODULE_DIR$/docs/book/book" />
|
||||||
|
|
|
@ -60,6 +60,7 @@ pub struct UseSpecificStorageOptions<T> {
|
||||||
/// Defaults to simply returning the stored value.
|
/// Defaults to simply returning the stored value.
|
||||||
merge_defaults: fn(&str, &T) -> String,
|
merge_defaults: fn(&str, &T) -> String,
|
||||||
/// Optional callback whenever an error occurs. The callback takes an argument of type [`UseStorageError`].
|
/// Optional callback whenever an error occurs. The callback takes an argument of type [`UseStorageError`].
|
||||||
|
#[builder(into)]
|
||||||
on_error: Box<dyn CloneableFnWithArg<UseStorageError>>,
|
on_error: Box<dyn CloneableFnWithArg<UseStorageError>>,
|
||||||
|
|
||||||
/// Debounce or throttle the writing to storage whenever the value changes.
|
/// Debounce or throttle the writing to storage whenever the value changes.
|
||||||
|
|
|
@ -452,6 +452,7 @@ pub struct UseStorageOptions<T> {
|
||||||
/// Defaults to simply returning the stored value.
|
/// Defaults to simply returning the stored value.
|
||||||
pub(crate) merge_defaults: fn(&str, &T) -> String,
|
pub(crate) merge_defaults: fn(&str, &T) -> String,
|
||||||
/// Optional callback whenever an error occurs. The callback takes an argument of type [`UseStorageError`].
|
/// Optional callback whenever an error occurs. The callback takes an argument of type [`UseStorageError`].
|
||||||
|
#[builder(into)]
|
||||||
pub(crate) on_error: Box<dyn CloneableFnWithArg<UseStorageError>>,
|
pub(crate) on_error: Box<dyn CloneableFnWithArg<UseStorageError>>,
|
||||||
|
|
||||||
/// Debounce or throttle the writing to storage whenever the value changes.
|
/// Debounce or throttle the writing to storage whenever the value changes.
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::utils::Pausable;
|
use crate::utils::{CloneableFn, CloneableFnWithArg, Pausable};
|
||||||
use crate::{use_interval_fn, use_interval_fn_with_options, UseIntervalFnOptions, watch};
|
use crate::{use_interval_fn, use_interval_fn_with_options, watch, UseIntervalFnOptions};
|
||||||
use default_struct_builder::DefaultBuilder;
|
use default_struct_builder::DefaultBuilder;
|
||||||
use leptos::leptos_dom::helpers::IntervalHandle;
|
use leptos::leptos_dom::helpers::IntervalHandle;
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
|
@ -39,17 +39,16 @@ pub fn use_interval<N>(
|
||||||
where
|
where
|
||||||
N: Into<MaybeSignal<u64>>,
|
N: Into<MaybeSignal<u64>>,
|
||||||
{
|
{
|
||||||
use_interval_with_options(cx, interval, UseIntervalOptions { immediate: true, callback: |_: u64| {} })
|
use_interval_with_options(cx, interval, UseIntervalOptions::default())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Version of [`use_interval`] that takes `UseIntervalOptions`. See [`use_interval`] for how to use.
|
/// Version of [`use_interval`] that takes `UseIntervalOptions`. See [`use_interval`] for how to use.
|
||||||
pub fn use_interval_with_options<CbFn, N>(
|
pub fn use_interval_with_options<N>(
|
||||||
cx: Scope,
|
cx: Scope,
|
||||||
interval: N,
|
interval: N,
|
||||||
options: UseIntervalOptions<CbFn>,
|
options: UseIntervalOptions,
|
||||||
) -> UseIntervalReturn<impl Fn() + Clone, impl Fn() + Clone, impl Fn() + Clone>
|
) -> UseIntervalReturn<impl Fn() + Clone, impl Fn() + Clone, impl Fn() + Clone>
|
||||||
where
|
where
|
||||||
CbFn: Fn(u64) + Clone + 'static,
|
|
||||||
N: Into<MaybeSignal<u64>>,
|
N: Into<MaybeSignal<u64>>,
|
||||||
{
|
{
|
||||||
let UseIntervalOptions {
|
let UseIntervalOptions {
|
||||||
|
@ -62,16 +61,24 @@ pub fn use_interval_with_options<CbFn, N>(
|
||||||
let update = move || set_counter.update(|count| *count += 1);
|
let update = move || set_counter.update(|count| *count += 1);
|
||||||
let reset = move || set_counter(0);
|
let reset = move || set_counter(0);
|
||||||
|
|
||||||
let cb =
|
let cb = move || {
|
||||||
move || {
|
|
||||||
update();
|
update();
|
||||||
callback(counter());
|
callback.clone()(counter());
|
||||||
};
|
};
|
||||||
|
|
||||||
let Pausable { is_active, pause, resume } = use_interval_fn_with_options(cx, cb, interval, UseIntervalFnOptions {
|
let Pausable {
|
||||||
|
is_active,
|
||||||
|
pause,
|
||||||
|
resume,
|
||||||
|
} = use_interval_fn_with_options(
|
||||||
|
cx,
|
||||||
|
cb,
|
||||||
|
interval,
|
||||||
|
UseIntervalFnOptions {
|
||||||
immediate,
|
immediate,
|
||||||
immediate_callback: false,
|
immediate_callback: false,
|
||||||
});
|
},
|
||||||
|
);
|
||||||
|
|
||||||
UseIntervalReturn {
|
UseIntervalReturn {
|
||||||
counter: counter.into(),
|
counter: counter.into(),
|
||||||
|
@ -84,27 +91,23 @@ pub fn use_interval_with_options<CbFn, N>(
|
||||||
|
|
||||||
/// Options for [`use_interval_with_options`]
|
/// Options for [`use_interval_with_options`]
|
||||||
#[derive(DefaultBuilder)]
|
#[derive(DefaultBuilder)]
|
||||||
pub struct UseIntervalOptions<CbFn>
|
pub struct UseIntervalOptions {
|
||||||
where
|
|
||||||
CbFn: Fn(u64) + Clone + 'static,
|
|
||||||
{
|
|
||||||
/// Start the timer immediately. Defaults to `true`.
|
/// Start the timer immediately. Defaults to `true`.
|
||||||
immediate: bool,
|
immediate: bool,
|
||||||
|
|
||||||
/// Callback on every interval.
|
/// Callback on every interval.
|
||||||
callback: CbFn,
|
#[builder(into)]
|
||||||
|
callback: Box<dyn CloneableFnWithArg<u64>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// impl<CbFn> Default for UseIntervalOptions<CbFn>
|
impl Default for UseIntervalOptions {
|
||||||
// where CbFn: Fn(u64) + Clone + 'static
|
fn default() -> Self {
|
||||||
// {
|
Self {
|
||||||
// fn default() -> Self {
|
immediate: false,
|
||||||
// Self {
|
callback: Box::new(|_: u64| {}),
|
||||||
// immediate: false,
|
}
|
||||||
// callback: |_: u64| {},
|
}
|
||||||
// }
|
}
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
/// Return type of [`use_interval`].
|
/// Return type of [`use_interval`].
|
||||||
#[derive(DefaultBuilder)]
|
#[derive(DefaultBuilder)]
|
||||||
|
|
|
@ -451,9 +451,11 @@ pub struct UseScrollOptions {
|
||||||
offset: ScrollOffset,
|
offset: ScrollOffset,
|
||||||
|
|
||||||
/// Callback when scrolling is happening.
|
/// Callback when scrolling is happening.
|
||||||
|
#[builder(into)]
|
||||||
on_scroll: Box<dyn CloneableFnWithArg<web_sys::Event>>,
|
on_scroll: Box<dyn CloneableFnWithArg<web_sys::Event>>,
|
||||||
|
|
||||||
/// Callback when scrolling stops (after `idle` + `throttle` milliseconds have passed).
|
/// Callback when scrolling stops (after `idle` + `throttle` milliseconds have passed).
|
||||||
|
#[builder(into)]
|
||||||
on_stop: Box<dyn CloneableFnWithArg<web_sys::Event>>,
|
on_stop: Box<dyn CloneableFnWithArg<web_sys::Event>>,
|
||||||
|
|
||||||
/// Options passed to the `addEventListener("scroll", ...)` call
|
/// Options passed to the `addEventListener("scroll", ...)` call
|
||||||
|
|
Loading…
Add table
Reference in a new issue