fix with_initial_path

This commit is contained in:
Evan Almloff committed 2023-07-14 08:22:31 -07:00
1 parent 8a142bbc95
commit 1dac04ba8a
3 files changed
+46 -20

No files matched your search

+6 -2
View File
@@ -60,13 +60,17 @@ impl<R> GenericRouterContext<R>
where
R: Routable,
{
pub(crate) fn new(cfg: RouterConfig<R>, mark_dirty: Arc<dyn Fn(ScopeId) + Sync + Send>) -> Self
pub(crate) fn new(
mut cfg: RouterConfig<R>,
mark_dirty: Arc<dyn Fn(ScopeId) + Sync + Send>,
) -> Self
where
R: Clone,
<R as std::str::FromStr>::Err: std::fmt::Display,
{
let state = Arc::new(RwLock::new(MutableRouterState {
prefix: Default::default(),
history: cfg.history,
history: cfg.take_history(),
unresolved_error: None,
}));
+3 -2
View File
@@ -39,7 +39,8 @@ where
pub fn with_initial_path(path: R) -> Self {
Self {
current: path,
..Default::default()
history: Vec::new(),
future: Vec::new(),
}
}
}
@@ -52,7 +53,7 @@ where
Self {
current: "/".parse().unwrap_or_else(|err| {
panic!("index route does not exist:\n{}\n use MemoryHistory::with_initial_path to set a custom path", err)
}),
}),
history: Vec::new(),
future: Vec::new(),
}
+37 -16
View File
@@ -26,7 +26,7 @@ use crate::prelude::*;
/// ```
pub struct RouterConfig<R: Routable> {
pub(crate) failure_external_navigation: fn(Scope) -> Element,
pub(crate) history: Box<dyn HistoryProvider<R>>,
pub(crate) history: Option<Box<dyn HistoryProvider<R>>>,
pub(crate) on_update: Option<RoutingCallback<R>>,
}
@@ -39,18 +39,29 @@ where
fn default() -> Self {
Self {
failure_external_navigation: FailureExternalNavigation::<R>,
history: {
#[cfg(all(target_arch = "wasm32", feature = "web"))]
let history = Box::<WebHistory<R>>::default();
#[cfg(not(all(target_arch = "wasm32", feature = "web")))]
let history = Box::<MemoryHistory<R>>::default();
history
},
history: None,
on_update: None,
}
}
}
#[cfg(feature = "serde")]
impl<R: Routable + Clone> RouterConfig<R>
where
<R as std::str::FromStr>::Err: std::fmt::Display,
R: serde::Serialize + serde::de::DeserializeOwned,
{
pub(crate) fn get_history(self) -> Box<dyn HistoryProvider<R>> {
self.history.unwrap_or_else(|| {
#[cfg(all(target_arch = "wasm32", feature = "web"))]
let history = Box::<WebHistory<R>>::default();
#[cfg(not(all(target_arch = "wasm32", feature = "web")))]
let history = Box::<MemoryHistory<R>>::default();
history
})
}
}
#[cfg(not(feature = "serde"))]
impl<R: Routable + Clone> Default for RouterConfig<R>
where
@@ -59,18 +70,28 @@ where
fn default() -> Self {
Self {
failure_external_navigation: FailureExternalNavigation::<R>,
history: {
#[cfg(all(target_arch = "wasm32", feature = "web"))]
let history = Box::<WebHistory<R>>::default();
#[cfg(not(all(target_arch = "wasm32", feature = "web")))]
let history = Box::<MemoryHistory<R>>::default();
history
},
history: None,
on_update: None,
}
}
}
#[cfg(not(feature = "serde"))]
impl<R: Routable + Clone> RouterConfig<R>
where
<R as std::str::FromStr>::Err: std::fmt::Display,
{
pub(crate) fn take_history(&mut self) -> Box<dyn HistoryProvider<R>> {
self.history.take().unwrap_or_else(|| {
#[cfg(all(target_arch = "wasm32", feature = "web"))]
let history = Box::<WebHistory<R>>::default();
#[cfg(not(all(target_arch = "wasm32", feature = "web")))]
let history = Box::<MemoryHistory<R>>::default();
history
})
}
}
impl<R: Routable> RouterConfig<R> {
/// A function to be called whenever the routing is updated.
///
@@ -100,7 +121,7 @@ impl<R: Routable> RouterConfig<R> {
/// Defaults to a default [`MemoryHistory`].
pub fn history(self, history: impl HistoryProvider<R> + 'static) -> Self {
Self {
history: Box::new(history),
history: Some(Box::new(history)),
..self
}
}