From 18ff2f49e1455b9024ee0b1b6ffb50a15572a475 Mon Sep 17 00:00:00 2001 From: Maccesch Date: Thu, 15 Jun 2023 20:10:30 +0100 Subject: [PATCH] use_interval callback as box --- .idea/leptos-use.iml | 1 + src/storage/shared.rs | 1 + src/storage/use_storage.rs | 1 + src/use_interval.rs | 81 ++++++++++++++++++++------------------ src/use_scroll.rs | 2 + 5 files changed, 47 insertions(+), 39 deletions(-) diff --git a/.idea/leptos-use.iml b/.idea/leptos-use.iml index 434e2ba..8acd985 100644 --- a/.idea/leptos-use.iml +++ b/.idea/leptos-use.iml @@ -32,6 +32,7 @@ + diff --git a/src/storage/shared.rs b/src/storage/shared.rs index 461d74b..1f60b57 100644 --- a/src/storage/shared.rs +++ b/src/storage/shared.rs @@ -60,6 +60,7 @@ pub struct UseSpecificStorageOptions { /// 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>, /// Debounce or throttle the writing to storage whenever the value changes. diff --git a/src/storage/use_storage.rs b/src/storage/use_storage.rs index a3deac2..3f5a2f8 100644 --- a/src/storage/use_storage.rs +++ b/src/storage/use_storage.rs @@ -452,6 +452,7 @@ pub struct UseStorageOptions { /// 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>, /// Debounce or throttle the writing to storage whenever the value changes. diff --git a/src/use_interval.rs b/src/use_interval.rs index dd785ca..43e1726 100644 --- a/src/use_interval.rs +++ b/src/use_interval.rs @@ -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( cx: Scope, interval: N, ) -> UseIntervalReturn - where - N: Into>, +where + N: Into>, { - 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( +pub fn use_interval_with_options( cx: Scope, interval: N, - options: UseIntervalOptions, + options: UseIntervalOptions, ) -> UseIntervalReturn - where - CbFn: Fn(u64) + Clone + 'static, - N: Into>, +where + N: Into>, { let UseIntervalOptions { immediate, @@ -62,16 +61,24 @@ pub fn use_interval_with_options( 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( /// Options for [`use_interval_with_options`] #[derive(DefaultBuilder)] -pub struct UseIntervalOptions -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>, } -// impl Default for UseIntervalOptions -// 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 - 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, @@ -128,4 +131,4 @@ pub struct UseIntervalReturn /// Resume the counter pub resume: ResumeFn, -} \ No newline at end of file +} diff --git a/src/use_scroll.rs b/src/use_scroll.rs index 27743c9..189cdc9 100644 --- a/src/use_scroll.rs +++ b/src/use_scroll.rs @@ -451,9 +451,11 @@ pub struct UseScrollOptions { offset: ScrollOffset, /// Callback when scrolling is happening. + #[builder(into)] on_scroll: Box>, /// Callback when scrolling stops (after `idle` + `throttle` milliseconds have passed). + #[builder(into)] on_stop: Box>, /// Options passed to the `addEventListener("scroll", ...)` call