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/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" />

View file

@ -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.

View file

@ -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.

View file

@ -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::*;
@ -36,21 +36,20 @@ pub fn use_interval<N>(
cx: Scope, cx: Scope,
interval: N, interval: N,
) -> UseIntervalReturn<impl Fn() + Clone, impl Fn() + Clone, impl Fn() + Clone> ) -> UseIntervalReturn<impl Fn() + Clone, impl Fn() + Clone, impl Fn() + Clone>
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 {
immediate, immediate,
@ -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.clone()(counter());
callback(counter()); };
};
let Pausable { is_active, pause, resume } = use_interval_fn_with_options(cx, cb, interval, UseIntervalFnOptions { let Pausable {
immediate, is_active,
immediate_callback: false, pause,
}); resume,
} = use_interval_fn_with_options(
cx,
cb,
interval,
UseIntervalFnOptions {
immediate,
immediate_callback: false,
},
);
UseIntervalReturn { UseIntervalReturn {
counter: counter.into(), counter: counter.into(),
@ -84,35 +91,31 @@ 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)]
pub struct UseIntervalReturn<PauseFn, ResumeFn, ResetFn> pub struct UseIntervalReturn<PauseFn, ResumeFn, ResetFn>
where where
PauseFn: Fn() + Clone, PauseFn: Fn() + Clone,
ResumeFn: Fn() + Clone, ResumeFn: Fn() + Clone,
ResetFn: Fn() + Clone, ResetFn: Fn() + Clone,
{ {
/// Counter signal that increases by one every interval. /// Counter signal that increases by one every interval.
pub counter: Signal<u64>, pub counter: Signal<u64>,
@ -128,4 +131,4 @@ pub struct UseIntervalReturn<PauseFn, ResumeFn, ResetFn>
/// Resume the counter /// Resume the counter
pub resume: ResumeFn, pub resume: ResumeFn,
} }

View file

@ -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