diff --git a/dynatos-reactive/src/lib.rs b/dynatos-reactive/src/lib.rs index 017b3c0..b9ae098 100644 --- a/dynatos-reactive/src/lib.rs +++ b/dynatos-reactive/src/lib.rs @@ -97,6 +97,22 @@ pub trait SignalWith { F: for<'a> FnOnce(Self::Value<'a>) -> O; } +/// Types which may be set by [`SignalSet`] +pub trait SignalSetWith: Sized { + fn set(&mut self, new_value: T); +} + +impl SignalSetWith for T { + fn set(&mut self, new_value: T) { + *self = new_value; + } +} +impl SignalSetWith for Option { + fn set(&mut self, new_value: T) { + *self = Some(new_value); + } +} + /// Signal set pub trait SignalSet { /// Sets the signal value @@ -105,10 +121,11 @@ pub trait SignalSet { impl SignalSet for S where - S: SignalUpdate, + S: SignalUpdate, + S::Value: SignalSetWith, { fn set(&self, new_value: T) { - self.update(|value| *value = new_value); + self.update(|value| SignalSetWith::set(value, new_value)); } } diff --git a/dynatos-reactive/src/with_default.rs b/dynatos-reactive/src/with_default.rs index 1468cce..6afd311 100644 --- a/dynatos-reactive/src/with_default.rs +++ b/dynatos-reactive/src/with_default.rs @@ -1,7 +1,7 @@ //! `Option` Signal with default value // Imports -use crate::{SignalReplace, SignalSet, SignalUpdate, SignalWith}; +use crate::{SignalReplace, SignalUpdate, SignalWith}; /// Wrapper for a `Signal>` with a default value #[derive(Clone, Debug)] @@ -38,15 +38,6 @@ where } } -impl SignalSet> for WithDefault -where - S: SignalSet>, -{ - fn set(&self, new_value: Option) { - self.inner.set(new_value) - } -} - impl SignalReplace for WithDefault where S: SignalReplace>, diff --git a/dynatos-router/src/anchor.rs b/dynatos-router/src/anchor.rs index d788178..2aafae9 100644 --- a/dynatos-router/src/anchor.rs +++ b/dynatos-router/src/anchor.rs @@ -6,6 +6,7 @@ use { dynatos_html::{html, ElementWithAttr}, dynatos_reactive::SignalSet, dynatos_util::{ev, EventTargetWithListener}, + url::Url, web_sys::Element, }; @@ -21,8 +22,11 @@ where .with_event_listener::(move |ev| { ev.prevent_default(); dynatos_context::with_expect::(|location| { - let new_location = new_location.as_ref(); - location.set(new_location); + let new_location = &new_location.as_ref(); + match new_location.parse::() { + Ok(new_location) => location.set(new_location), + Err(err) => tracing::warn!("Unable to parse new location as a valid url {new_location:?}: {err}"), + } }); }) } diff --git a/dynatos-router/src/location.rs b/dynatos-router/src/location.rs index 83ee3d3..3f517ae 100644 --- a/dynatos-router/src/location.rs +++ b/dynatos-router/src/location.rs @@ -2,7 +2,7 @@ // Imports use { - dynatos_reactive::{Signal, SignalSet, SignalUpdate, SignalWith}, + dynatos_reactive::{Signal, SignalUpdate, SignalWith}, dynatos_util::{ev, EventTargetAddListener}, url::Url, wasm_bindgen::JsValue, @@ -54,22 +54,6 @@ impl SignalWith for Location { } } -impl SignalSet<&'_ str> for Location { - fn set(&self, new_location: &'_ str) { - let window = web_sys::window().expect("Unable to get window"); - let history = window.history().expect("Unable to get history"); - - // Push the new location into history - history - .push_state_with_url(&JsValue::UNDEFINED, "", Some(new_location)) - .expect("Unable to push history"); - - // Then parse the location back - let new_location = self::parse_location_url(); - self.0.update(|inner| inner.location = new_location); - } -} - impl SignalUpdate for Location { type Value = Url; diff --git a/dynatos-router/src/query_signal.rs b/dynatos-router/src/query_signal.rs index 81a57f1..1ccbad7 100644 --- a/dynatos-router/src/query_signal.rs +++ b/dynatos-router/src/query_signal.rs @@ -73,15 +73,6 @@ impl SignalWith for QuerySignal { } } -impl SignalSet for QuerySignal -where - T: ToString + 'static, -{ - fn set(&self, new_value: T) { - self.update(|value| *value = Some(new_value)); - } -} - impl SignalReplace> for QuerySignal where T: ToString + 'static,