removed warnings and fixed examples

This commit is contained in:
Maccesch 2023-11-13 00:20:58 +00:00
parent 8827eefa58
commit 28ab799156
11 changed files with 56 additions and 31 deletions

View file

@ -3,6 +3,21 @@
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Breaking Changes 🛠
- (@feral-dot-io) The use `use_<type>_storage` functions have been rewritten to use `Codec`s instead of always requiring `serde`.
- This also removes the feature `storage`
- By default the `StringCodec` is used which relies on types implementing `FromString + ToString`
- If you want to use `JsonCodec` you have to enable the feature `serde`
- If you want to use `ProstCodec` (new!) you have to enable the feature `prost`.
- (@feral-dot-io) The Rust flag `--cfg=web_sys_unstable_apis` is not needed anymore since relevant `web_sys` APIs are now stable.
This affects in particular
- `use_element_size`
- `use_resize_observer`
## [0.8.2] - 2023-11-09
### Fixes 🍕

View file

@ -21,7 +21,7 @@ simple_logger = "4"
tokio = { version = "1.25.0", optional = true }
tower = { version = "0.4.13", optional = true }
tower-http = { version = "0.4.3", features = ["fs"], optional = true }
wasm-bindgen = "=0.2.87"
wasm-bindgen = "0.2.88"
thiserror = "1.0.38"
tracing = { version = "0.1.37", optional = true }
http = "0.2.8"

View file

@ -3,7 +3,7 @@ use leptos::ev::{keypress, KeyboardEvent};
use leptos::*;
use leptos_meta::*;
use leptos_router::*;
use leptos_use::storage::use_local_storage;
use leptos_use::storage::{use_local_storage, StringCodec};
use leptos_use::{
use_color_mode, use_debounce_fn, use_event_listener, use_intl_number_format, use_timestamp,
use_window, ColorMode, UseColorModeReturn, UseIntlNumberFormatOptions,
@ -37,7 +37,7 @@ pub fn App() -> impl IntoView {
#[component]
fn HomePage() -> impl IntoView {
// Creates a reactive value to update the button
let (count, set_count, _) = use_local_storage("count-state", 0);
let (count, set_count, _) = use_local_storage::<i32, StringCodec>("count-state");
let on_click = move |_| set_count.update(|count| *count += 1);
let nf = use_intl_number_format(

View file

@ -114,7 +114,7 @@ use super::Codec;
/// # }
/// ```
#[derive(Clone, Default, PartialEq)]
pub struct JsonCodec();
pub struct JsonCodec;
impl<T: serde::Serialize + serde::de::DeserializeOwned> Codec<T> for JsonCodec {
type Error = serde_json::Error;
@ -143,7 +143,7 @@ mod tests {
s: String::from("party time 🎉"),
i: 42,
};
let codec = JsonCodec();
let codec = JsonCodec;
let enc = codec.encode(&t).unwrap();
let dec: Test = codec.decode(enc).unwrap();
assert_eq!(dec, t);

View file

@ -30,7 +30,7 @@ use thiserror::Error;
///
/// Note: we've defined and used the `prost` attribute here for brevity. Alternate usage would be to describe the message in a .proto file and use [`prost_build`](https://docs.rs/prost-build) to auto-generate the Rust code.
#[derive(Clone, Default, PartialEq)]
pub struct ProstCodec();
pub struct ProstCodec;
#[derive(Error, Debug, PartialEq)]
pub enum ProstCodecError {
@ -73,7 +73,7 @@ mod tests {
s: String::from("party time 🎉"),
i: 42,
};
let codec = ProstCodec();
let codec = ProstCodec;
assert_eq!(codec.decode(codec.encode(&t).unwrap()), Ok(t));
}
}

View file

@ -16,7 +16,7 @@ use std::str::FromStr;
/// # }
/// ```
#[derive(Clone, Default, PartialEq)]
pub struct StringCodec();
pub struct StringCodec;
impl<T: FromStr + ToString> Codec<T> for StringCodec {
type Error = T::Err;
@ -37,7 +37,7 @@ mod tests {
#[test]
fn test_string_codec() {
let s = String::from("party time 🎉");
let codec = StringCodec();
let codec = StringCodec;
assert_eq!(codec.encode(&s), Ok(s.clone()));
assert_eq!(codec.decode(s.clone()), Ok(s));
}

View file

@ -1,4 +1,4 @@
use super::{use_storage, Codec, StorageType, UseStorageOptions};
use super::{use_storage_with_options, Codec, StorageType, UseStorageOptions};
use leptos::signal_prelude::*;
/// Reactive [LocalStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage).

View file

@ -1,4 +1,4 @@
use super::{use_storage, Codec, StorageType, UseStorageOptions};
use super::{use_storage_with_options, Codec, StorageType, UseStorageOptions};
use leptos::signal_prelude::*;
/// Reactive [SessionStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage).

View file

@ -1,8 +1,7 @@
use crate::storage::StringCodec;
use crate::{
core::{MaybeRwSignal, StorageType},
use_event_listener, use_window,
utils::FilterOptions,
watch_with_options, WatchOptions,
};
use cfg_if::cfg_if;
use leptos::*;
@ -30,7 +29,7 @@ const INTERNAL_STORAGE_EVENT: &str = "leptos-use-storage";
///
/// ```
/// # use leptos::*;
/// # use leptos_use::storage::{StorageType, use_local_storage, use_session_storage, use_storage, UseStorageOptions, StringCodec, JsonCodec, ProstCodec};
/// # use leptos_use::storage::{StorageType, use_local_storage, use_session_storage, use_storage_with_options, UseStorageOptions, StringCodec, JsonCodec, ProstCodec};
/// # use serde::{Deserialize, Serialize};
/// #
/// # pub fn Demo() -> impl IntoView {
@ -73,15 +72,11 @@ const INTERNAL_STORAGE_EVENT: &str = "leptos-use-storage";
/// }
/// ```
#[inline(always)]
pub fn use_storage<T, C>(
pub fn use_storage(
storage_type: StorageType,
key: impl AsRef<str>,
) -> (Signal<T>, WriteSignal<T>, impl Fn() + Clone)
where
T: Clone + PartialEq,
C: Codec<T>,
{
use_storage_with_options(storage_type, key, UseStorageOptions::default())
) -> (Signal<String>, WriteSignal<String>, impl Fn() + Clone) {
use_storage_with_options::<String, StringCodec>(storage_type, key, UseStorageOptions::default())
}
/// Version of [`use_storage`] that accepts [`UseStorageOptions`].
@ -106,11 +101,23 @@ where
let default = data.get_untracked();
cfg_if! { if #[cfg(feature = "ssr")] {
let _ = codec;
let _ = on_error;
let _ = listen_to_storage_changes;
let _ = filter;
let _ = storage_type;
let _ = key;
let _ = INTERNAL_STORAGE_EVENT;
let remove = move || {
set_data.set(default.clone());
};
(data.into(), set_data, remove)
} else {
use crate::{use_event_listener, use_window, watch_with_options, WatchOptions};
// Get storage API
let storage = storage_type
.into_storage()
@ -340,6 +347,7 @@ pub trait Codec<T>: Clone + 'static {
}
/// Calls the on_error callback with the given error. Removes the error from the Result to avoid double error handling.
#[cfg(not(feature = "ssr"))]
fn handle_error<T, Err>(
on_error: &Rc<dyn Fn(UseStorageError<Err>)>,
result: Result<T, UseStorageError<Err>>,

View file

@ -1,5 +1,5 @@
use crate::core::{ElementMaybeSignal, MaybeRwSignal};
use crate::storage::{use_storage, StringCodec, UseStorageOptions};
use crate::storage::{use_storage_with_options, StringCodec, UseStorageOptions};
use std::fmt::{Display, Formatter};
use std::str::FromStr;

View file

@ -1,16 +1,9 @@
use crate::core::now;
use crate::utils::{create_filter_wrapper, DebounceOptions, FilterOptions, ThrottleOptions};
use crate::{
filter_builder_methods, use_document, use_event_listener, use_event_listener_with_options,
UseEventListenerOptions,
};
use crate::filter_builder_methods;
use crate::utils::{DebounceOptions, FilterOptions, ThrottleOptions};
use cfg_if::cfg_if;
use default_struct_builder::DefaultBuilder;
use leptos::ev::{visibilitychange, Custom};
use leptos::leptos_dom::helpers::TimeoutHandle;
use leptos::*;
use std::cell::Cell;
use std::time::Duration;
///
///
@ -93,7 +86,16 @@ pub fn use_idle_with_options(
let _ = events;
let _ = listen_for_visibility_change;
let _ = filter;
let _ = set_last_active;
let _ = set_idle;
} else {
use crate::utils::create_filter_wrapper;
use crate::{use_document, use_event_listener, use_event_listener_with_options,UseEventListenerOptions};
use leptos::ev::{visibilitychange, Custom};
use leptos::leptos_dom::helpers::TimeoutHandle;
use std::cell::Cell;
use std::time::Duration;
let reset = {
let timer = Cell::new(None::<TimeoutHandle>);