diff --git a/CHANGELOG.md b/CHANGELOG.md index e1c38c4..fed0397 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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__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 🍕 diff --git a/examples/ssr/Cargo.toml b/examples/ssr/Cargo.toml index 782e066..d37a754 100644 --- a/examples/ssr/Cargo.toml +++ b/examples/ssr/Cargo.toml @@ -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" diff --git a/examples/ssr/src/app.rs b/examples/ssr/src/app.rs index 2d5da01..cdfe5e3 100644 --- a/examples/ssr/src/app.rs +++ b/examples/ssr/src/app.rs @@ -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::("count-state"); let on_click = move |_| set_count.update(|count| *count += 1); let nf = use_intl_number_format( diff --git a/src/storage/codec_json.rs b/src/storage/codec_json.rs index 5a834a1..1a6a3a9 100644 --- a/src/storage/codec_json.rs +++ b/src/storage/codec_json.rs @@ -114,7 +114,7 @@ use super::Codec; /// # } /// ``` #[derive(Clone, Default, PartialEq)] -pub struct JsonCodec(); +pub struct JsonCodec; impl Codec 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); diff --git a/src/storage/codec_prost.rs b/src/storage/codec_prost.rs index e833410..2311e28 100644 --- a/src/storage/codec_prost.rs +++ b/src/storage/codec_prost.rs @@ -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)); } } diff --git a/src/storage/codec_string.rs b/src/storage/codec_string.rs index 1cae942..c7a8049 100644 --- a/src/storage/codec_string.rs +++ b/src/storage/codec_string.rs @@ -16,7 +16,7 @@ use std::str::FromStr; /// # } /// ``` #[derive(Clone, Default, PartialEq)] -pub struct StringCodec(); +pub struct StringCodec; impl Codec 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)); } diff --git a/src/storage/use_local_storage.rs b/src/storage/use_local_storage.rs index 73d864d..4479a4a 100644 --- a/src/storage/use_local_storage.rs +++ b/src/storage/use_local_storage.rs @@ -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). diff --git a/src/storage/use_session_storage.rs b/src/storage/use_session_storage.rs index ae6e1eb..127e457 100644 --- a/src/storage/use_session_storage.rs +++ b/src/storage/use_session_storage.rs @@ -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). diff --git a/src/storage/use_storage.rs b/src/storage/use_storage.rs index bce2f10..3c66fce 100644 --- a/src/storage/use_storage.rs +++ b/src/storage/use_storage.rs @@ -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( +pub fn use_storage( storage_type: StorageType, key: impl AsRef, -) -> (Signal, WriteSignal, impl Fn() + Clone) -where - T: Clone + PartialEq, - C: Codec, -{ - use_storage_with_options(storage_type, key, UseStorageOptions::default()) +) -> (Signal, WriteSignal, impl Fn() + Clone) { + use_storage_with_options::(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: 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( on_error: &Rc)>, result: Result>, diff --git a/src/use_color_mode.rs b/src/use_color_mode.rs index a9dd6fb..5f2b781 100644 --- a/src/use_color_mode.rs +++ b/src/use_color_mode.rs @@ -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; diff --git a/src/use_idle.rs b/src/use_idle.rs index 8a9cc96..1819929 100644 --- a/src/use_idle.rs +++ b/src/use_idle.rs @@ -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::);