use_interval callback as box

This commit is contained in:
Maccesch 2023-06-15 20:10:30 +01:00
parent c222376dd5
commit 18ff2f49e1
5 changed files with 47 additions and 39 deletions

1
.idea/leptos-use.iml generated
View file

@ -32,6 +32,7 @@
<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_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$/target" />
<excludeFolder url="file://$MODULE_DIR$/docs/book/book" />

View file

@ -60,6 +60,7 @@ pub struct UseSpecificStorageOptions<T> {
/// Defaults to simply returning the stored value.
merge_defaults: fn(&str, &T) -> String,
/// Optional callback whenever an error occurs. The callback takes an argument of type [`UseStorageError`].
#[builder(into)]
on_error: Box<dyn CloneableFnWithArg<UseStorageError>>,
/// Debounce or throttle the writing to storage whenever the value changes.

View file

@ -452,6 +452,7 @@ pub struct UseStorageOptions<T> {
/// Defaults to simply returning the stored value.
pub(crate) merge_defaults: fn(&str, &T) -> String,
/// Optional callback whenever an error occurs. The callback takes an argument of type [`UseStorageError`].
#[builder(into)]
pub(crate) on_error: Box<dyn CloneableFnWithArg<UseStorageError>>,
/// Debounce or throttle the writing to storage whenever the value changes.

View file

@ -1,5 +1,5 @@
use crate::utils::Pausable;
use crate::{use_interval_fn, use_interval_fn_with_options, UseIntervalFnOptions, watch};
use crate::utils::{CloneableFn, CloneableFnWithArg, Pausable};
use crate::{use_interval_fn, use_interval_fn_with_options, watch, UseIntervalFnOptions};
use default_struct_builder::DefaultBuilder;
use leptos::leptos_dom::helpers::IntervalHandle;
use leptos::*;
@ -36,21 +36,20 @@ pub fn use_interval<N>(
cx: Scope,
interval: N,
) -> UseIntervalReturn<impl Fn() + Clone, impl Fn() + Clone, impl Fn() + Clone>
where
N: Into<MaybeSignal<u64>>,
where
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.
pub fn use_interval_with_options<CbFn, N>(
pub fn use_interval_with_options<N>(
cx: Scope,
interval: N,
options: UseIntervalOptions<CbFn>,
options: UseIntervalOptions,
) -> UseIntervalReturn<impl Fn() + Clone, impl Fn() + Clone, impl Fn() + Clone>
where
CbFn: Fn(u64) + Clone + 'static,
N: Into<MaybeSignal<u64>>,
where
N: Into<MaybeSignal<u64>>,
{
let UseIntervalOptions {
immediate,
@ -62,16 +61,24 @@ pub fn use_interval_with_options<CbFn, N>(
let update = move || set_counter.update(|count| *count += 1);
let reset = move || set_counter(0);
let cb =
move || {
update();
callback(counter());
};
let cb = move || {
update();
callback.clone()(counter());
};
let Pausable { is_active, pause, resume } = use_interval_fn_with_options(cx, cb, interval, UseIntervalFnOptions {
immediate,
immediate_callback: false,
});
let Pausable {
is_active,
pause,
resume,
} = use_interval_fn_with_options(
cx,
cb,
interval,
UseIntervalFnOptions {
immediate,
immediate_callback: false,
},
);
UseIntervalReturn {
counter: counter.into(),
@ -84,35 +91,31 @@ pub fn use_interval_with_options<CbFn, N>(
/// Options for [`use_interval_with_options`]
#[derive(DefaultBuilder)]
pub struct UseIntervalOptions<CbFn>
where
CbFn: Fn(u64) + Clone + 'static,
{
pub struct UseIntervalOptions {
/// Start the timer immediately. Defaults to `true`.
immediate: bool,
/// Callback on every interval.
callback: CbFn,
#[builder(into)]
callback: Box<dyn CloneableFnWithArg<u64>>,
}
// impl<CbFn> Default for UseIntervalOptions<CbFn>
// where CbFn: Fn(u64) + Clone + 'static
// {
// fn default() -> Self {
// Self {
// immediate: false,
// callback: |_: u64| {},
// }
// }
// }
impl Default for UseIntervalOptions {
fn default() -> Self {
Self {
immediate: false,
callback: Box::new(|_: u64| {}),
}
}
}
/// Return type of [`use_interval`].
#[derive(DefaultBuilder)]
pub struct UseIntervalReturn<PauseFn, ResumeFn, ResetFn>
where
PauseFn: Fn() + Clone,
ResumeFn: Fn() + Clone,
ResetFn: Fn() + Clone,
where
PauseFn: Fn() + Clone,
ResumeFn: Fn() + Clone,
ResetFn: Fn() + Clone,
{
/// Counter signal that increases by one every interval.
pub counter: Signal<u64>,
@ -128,4 +131,4 @@ pub struct UseIntervalReturn<PauseFn, ResumeFn, ResetFn>
/// Resume the counter
pub resume: ResumeFn,
}
}

View file

@ -451,9 +451,11 @@ pub struct UseScrollOptions {
offset: ScrollOffset,
/// Callback when scrolling is happening.
#[builder(into)]
on_scroll: Box<dyn CloneableFnWithArg<web_sys::Event>>,
/// Callback when scrolling stops (after `idle` + `throttle` milliseconds have passed).
#[builder(into)]
on_stop: Box<dyn CloneableFnWithArg<web_sys::Event>>,
/// Options passed to the `addEventListener("scroll", ...)` call