leptos_use/utils/filters/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
mod debounce;
mod throttle;

pub use debounce::*;
pub use throttle::*;

use leptos::MaybeSignal;
use std::cell::RefCell;
use std::rc::Rc;

macro_rules! RcFilterFn {
    ($R:ident) => {
        Rc<dyn Fn(Rc<dyn Fn() -> $R>) -> Rc<RefCell<Option<$R>>>>
    }
}

pub fn create_filter_wrapper<F, R>(
    filter: RcFilterFn!(R),
    func: F,
) -> impl Fn() -> Rc<RefCell<Option<R>>> + Clone
where
    F: Fn() -> R + Clone + 'static,
    R: 'static,
{
    move || Rc::clone(&filter)(Rc::new(func.clone()))
}

pub fn create_filter_wrapper_with_arg<F, Arg, R>(
    filter: RcFilterFn!(R),
    func: F,
) -> impl Fn(Arg) -> Rc<RefCell<Option<R>>> + Clone
where
    F: Fn(Arg) -> R + Clone + 'static,
    R: 'static,
    Arg: Clone + 'static,
{
    move |arg: Arg| {
        let func = func.clone();
        Rc::clone(&filter)(Rc::new(move || func(arg.clone())))
    }
}

/// Specify a debounce or throttle filter with their respective options or no filter
#[derive(Default)]
pub enum FilterOptions {
    #[default]
    None,
    Debounce {
        ms: MaybeSignal<f64>,
        options: DebounceOptions,
    },
    Throttle {
        ms: MaybeSignal<f64>,
        options: ThrottleOptions,
    },
}

impl FilterOptions {
    pub fn debounce(ms: impl Into<MaybeSignal<f64>>) -> Self {
        Self::Debounce {
            ms: ms.into(),
            options: DebounceOptions::default(),
        }
    }

    pub fn throttle(ms: impl Into<MaybeSignal<f64>>) -> Self {
        Self::Throttle {
            ms: ms.into(),
            options: ThrottleOptions::default(),
        }
    }

    pub fn filter_fn<R>(&self) -> RcFilterFn!(R)
    where
        R: 'static,
    {
        match self {
            FilterOptions::Debounce { ms, options } => Rc::new(debounce_filter(*ms, *options)),
            FilterOptions::Throttle { ms, options } => Rc::new(throttle_filter(*ms, *options)),
            FilterOptions::None => {
                Rc::new(|invoke: Rc<dyn Fn() -> R>| Rc::new(RefCell::new(Some(invoke()))))
            }
        }
    }
}

/// Defines builder methods to define filter options without having to use nested methods
#[macro_export]
macro_rules! filter_builder_methods {
    (
        #[$filter_docs:meta]
        $filter_field_name:ident
    ) => {
        /// Debounce
        #[$filter_docs]
        /// by `ms` milliseconds.
        pub fn debounce(self, ms: impl Into<MaybeSignal<f64>>) -> Self {
            self.debounce_with_options(ms, DebounceOptions::default())
        }

        /// Debounce
        #[$filter_docs]
        /// by `ms` milliseconds with additional options.
        pub fn debounce_with_options(
            self,
            ms: impl Into<MaybeSignal<f64>>,
            options: DebounceOptions,
        ) -> Self {
            Self {
                $filter_field_name: FilterOptions::Debounce {
                    ms: ms.into(),
                    options,
                },
                ..self
            }
        }

        /// Throttle
        #[$filter_docs]
        /// by `ms` milliseconds.
        pub fn throttle(self, ms: impl Into<MaybeSignal<f64>>) -> Self {
            self.throttle_with_options(ms, ThrottleOptions::default())
        }

        /// Throttle
        #[$filter_docs]
        /// by `ms` milliseconds with additional options.
        pub fn throttle_with_options(
            self,
            ms: impl Into<MaybeSignal<f64>>,
            options: ThrottleOptions,
        ) -> Self {
            Self {
                $filter_field_name: FilterOptions::Throttle {
                    ms: ms.into(),
                    options,
                },
                ..self
            }
        }
    };
}