leptos_use_storage/
options.rs1use crate::{MaybeRwSignal, NoFilter};
2use codee::CodecError;
3use leptos::prelude::*;
4use std::sync::Arc;
5use thiserror::Error;
6use wasm_bindgen::JsValue;
7
8#[derive(Error, Debug)]
10pub enum UseStorageError<E, D> {
11 #[error("storage not available")]
12 StorageNotAvailable(JsValue),
13 #[error("storage not returned from window")]
14 StorageReturnedNone,
15 #[error("failed to get item")]
16 GetItemFailed(JsValue),
17 #[error("failed to set item")]
18 SetItemFailed(JsValue),
19 #[error("failed to delete item")]
20 RemoveItemFailed(JsValue),
21 #[error("failed to notify item changed")]
22 NotifyItemChangedFailed(JsValue),
23 #[error("failed to encode / decode item value")]
24 ItemCodecError(CodecError<E, D>),
25}
26
27pub struct UseStorageOptions<T, E, D, F = NoFilter>
33where
34 T: Send + Sync + 'static,
35{
36 pub(crate) on_error: Arc<dyn Fn(UseStorageError<E, D>) + Send + Sync>,
38 pub(crate) listen_to_storage_changes: bool,
40 pub(crate) initial_value: MaybeRwSignal<T>,
42 pub(crate) filter: F,
44 pub(crate) delay_during_hydration: bool,
48}
49
50impl<T: Default, E, D> Default for UseStorageOptions<T, E, D>
51where
52 T: Send + Sync + 'static,
53{
54 fn default() -> Self {
55 Self {
56 on_error: Arc::new(|_err| ()),
57 listen_to_storage_changes: true,
58 initial_value: MaybeRwSignal::default(),
59 filter: NoFilter,
60 delay_during_hydration: false,
61 }
62 }
63}
64
65impl<T, E, D, F> UseStorageOptions<T, E, D, F>
66where
67 T: Send + Sync + 'static,
68{
69 pub fn on_error(
71 self,
72 on_error: impl Fn(UseStorageError<E, D>) + Send + Sync + 'static,
73 ) -> Self {
74 Self {
75 on_error: Arc::new(on_error),
76 ..self
77 }
78 }
79
80 pub fn listen_to_storage_changes(self, listen_to_storage_changes: bool) -> Self {
82 Self {
83 listen_to_storage_changes,
84 ..self
85 }
86 }
87
88 pub fn initial_value(self, initial: impl Into<MaybeRwSignal<T>>) -> Self {
90 Self {
91 initial_value: initial.into(),
92 ..self
93 }
94 }
95
96 pub fn filter<NewF>(self, filter: NewF) -> UseStorageOptions<T, E, D, NewF> {
99 UseStorageOptions {
100 on_error: self.on_error,
101 listen_to_storage_changes: self.listen_to_storage_changes,
102 initial_value: self.initial_value,
103 filter,
104 delay_during_hydration: self.delay_during_hydration,
105 }
106 }
107
108 pub fn delay_during_hydration(self, delay_during_hydration: bool) -> Self {
112 Self {
113 delay_during_hydration,
114 ..self
115 }
116 }
117}