Skip to main content

leptos_use_timestamp/
options.rs

1use default_struct_builder::DefaultBuilder;
2use std::rc::Rc;
3
4/// Options for [`use_timestamp_with_controls_and_options`](crate::use_timestamp_with_controls_and_options).
5#[derive(DefaultBuilder)]
6pub struct UseTimestampOptions {
7    /// Offset value in milliseconds that is added to the returned timestamp. Defaults to `0.0`.
8    pub(crate) offset: f64,
9
10    /// Whether to update the timestamp immediately. Defaults to `true`.
11    pub(crate) immediate: bool,
12
13    /// Update interval in milliseconds or `RequestAnimationFrame`. Defaults to `RequestAnimationFrame`.
14    #[builder(into)]
15    pub(crate) interval: TimestampInterval,
16
17    /// Callback to be called whenever the timestamp is updated.
18    pub(crate) callback: Rc<dyn Fn(f64)>,
19}
20
21/// Interval type for [`UseTimestampOptions`].
22#[derive(Copy, Clone, Eq, PartialEq, Debug)]
23pub enum TimestampInterval {
24    /// use [`fn@crate::use_raf_fn`] for updating the timestamp
25    RequestAnimationFrame,
26
27    /// use [`fn@crate::use_interval_fn`] for updating the timestamp
28    Interval(u64),
29}
30
31impl From<u64> for TimestampInterval {
32    fn from(value: u64) -> Self {
33        Self::Interval(value)
34    }
35}
36
37impl Default for UseTimestampOptions {
38    fn default() -> Self {
39        Self {
40            offset: 0.0,
41            immediate: true,
42            interval: TimestampInterval::RequestAnimationFrame,
43            callback: Rc::new(|_| {}),
44        }
45    }
46}