fix web history
This commit is contained in:
1 parent
b91fb39142
commit
dd48c5a163
5 files changed
+45
-15
No files matched your search
@@ -45,6 +45,12 @@ pub struct WebHistory<R: Serialize + DeserializeOwned> {
|
||||
phantom: std::marker::PhantomData<R>,
|
||||
}
|
||||
|
||||
impl<R: Serialize + DeserializeOwned> Default for WebHistory<R> {
|
||||
fn default() -> Self {
|
||||
Self::new(None, true)
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Serialize + DeserializeOwned> WebHistory<R> {
|
||||
/// Create a new [`WebHistory`].
|
||||
///
|
||||
@@ -97,9 +103,9 @@ where
|
||||
<R as std::str::FromStr>::Err: std::fmt::Display,
|
||||
{
|
||||
fn current_route(&self) -> R {
|
||||
match get_current(&self.history) {
|
||||
match get_current::<WebHistoryState<_>>(&self.history) {
|
||||
// Try to get the route from the history state
|
||||
Some(route) => route,
|
||||
Some(route) => route.state,
|
||||
// If that fails, get the route from the current URL
|
||||
None => R::from_str(
|
||||
&self
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use gloo::console::error;
|
||||
use gloo_utils::format::JsValueSerdeExt;
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
use wasm_bindgen::JsValue;
|
||||
@@ -24,8 +25,15 @@ pub(crate) fn push_state_and_url<V: Serialize>(
|
||||
}
|
||||
|
||||
pub(crate) fn get_current<V: DeserializeOwned>(history: &History) -> Option<V> {
|
||||
history
|
||||
.state()
|
||||
.ok()
|
||||
.and_then(|state| state.into_serde().ok())
|
||||
let state = history.state();
|
||||
if let Err(err) = &state {
|
||||
error!(err);
|
||||
}
|
||||
state.ok().and_then(|state| {
|
||||
let deserialized = state.into_serde();
|
||||
if let Err(err) = &deserialized {
|
||||
error!(format!("{}", err));
|
||||
}
|
||||
deserialized.ok()
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user