Problem: use_storage's default_value with a signal might suggest changing the signal also changes the value used on an unset storage value. Rename to initial value and document this behaviour

This commit is contained in:
Joshua McQuistan 2023-11-04 13:50:41 +00:00
parent af653595a5
commit 9a9f1ba7d9
2 changed files with 8 additions and 8 deletions

View file

@ -27,7 +27,7 @@ pub struct UseStorageOptions<T: 'static, C: Codec<T>> {
codec: C, codec: C,
on_error: Rc<dyn Fn(UseStorageError<C::Error>)>, on_error: Rc<dyn Fn(UseStorageError<C::Error>)>,
listen_to_storage_changes: bool, listen_to_storage_changes: bool,
default_value: MaybeRwSignal<T>, initial_value: MaybeRwSignal<T>,
filter: FilterOptions, filter: FilterOptions,
} }
@ -186,11 +186,11 @@ where
codec, codec,
on_error, on_error,
listen_to_storage_changes, listen_to_storage_changes,
default_value, initial_value,
filter, filter,
} = options; } = options;
let (data, set_data) = default_value.into_signal(); let (data, set_data) = initial_value.into_signal();
let default = data.get_untracked(); let default = data.get_untracked();
cfg_if! { if #[cfg(feature = "ssr")] { cfg_if! { if #[cfg(feature = "ssr")] {
@ -384,7 +384,7 @@ impl<T: Default, C: Codec<T> + Default> Default for UseStorageOptions<T, C> {
codec: C::default(), codec: C::default(),
on_error: Rc::new(|_err| ()), on_error: Rc::new(|_err| ()),
listen_to_storage_changes: true, listen_to_storage_changes: true,
default_value: MaybeRwSignal::default(), initial_value: MaybeRwSignal::default(),
filter: FilterOptions::default(), filter: FilterOptions::default(),
} }
} }
@ -415,10 +415,10 @@ impl<T: Default, C: Codec<T>> UseStorageOptions<T, C> {
} }
} }
/// Default value to use when the storage key is not set. Accepts a signal. /// Initial value to use when the storage key is not set. Note that this value is read once on creation of the storage hook and not updated again. Accepts a signal and defaults to `T::default()`.
pub fn default_value(self, values: impl Into<MaybeRwSignal<T>>) -> Self { pub fn initial_value(self, initial: impl Into<MaybeRwSignal<T>>) -> Self {
Self { Self {
default_value: values.into(), initial_value: initial.into(),
..self ..self
} }
} }

View file

@ -266,7 +266,7 @@ fn get_store_signal(
storage_key, storage_key,
UseStorageOptions::default() UseStorageOptions::default()
.listen_to_storage_changes(listen_to_storage_changes) .listen_to_storage_changes(listen_to_storage_changes)
.default_value(initial_value), .initial_value(initial_value),
); );
(store, set_store) (store, set_store)
} else { } else {