From 1dac04ba8a6ab068a77a0280056933b5fbdaf4ce Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Fri, 14 Jul 2023 08:22:31 -0700 Subject: [PATCH] fix with_initial_path --- packages/router/src/contexts/router.rs | 8 +++- packages/router/src/history/memory.rs | 5 ++- packages/router/src/router_cfg.rs | 53 ++++++++++++++++++-------- 3 files changed, 46 insertions(+), 20 deletions(-) diff --git a/packages/router/src/contexts/router.rs b/packages/router/src/contexts/router.rs index 58a99f19d..a64b64b4c 100644 --- a/packages/router/src/contexts/router.rs +++ b/packages/router/src/contexts/router.rs @@ -60,13 +60,17 @@ impl GenericRouterContext where R: Routable, { - pub(crate) fn new(cfg: RouterConfig, mark_dirty: Arc) -> Self + pub(crate) fn new( + mut cfg: RouterConfig, + mark_dirty: Arc, + ) -> Self where R: Clone, + ::Err: std::fmt::Display, { let state = Arc::new(RwLock::new(MutableRouterState { prefix: Default::default(), - history: cfg.history, + history: cfg.take_history(), unresolved_error: None, })); diff --git a/packages/router/src/history/memory.rs b/packages/router/src/history/memory.rs index e90119663..07c1f4379 100644 --- a/packages/router/src/history/memory.rs +++ b/packages/router/src/history/memory.rs @@ -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(), } diff --git a/packages/router/src/router_cfg.rs b/packages/router/src/router_cfg.rs index 637f1d324..31eeeb4d5 100644 --- a/packages/router/src/router_cfg.rs +++ b/packages/router/src/router_cfg.rs @@ -26,7 +26,7 @@ use crate::prelude::*; /// ``` pub struct RouterConfig { pub(crate) failure_external_navigation: fn(Scope) -> Element, - pub(crate) history: Box>, + pub(crate) history: Option>>, pub(crate) on_update: Option>, } @@ -39,18 +39,29 @@ where fn default() -> Self { Self { failure_external_navigation: FailureExternalNavigation::, - history: { - #[cfg(all(target_arch = "wasm32", feature = "web"))] - let history = Box::>::default(); - #[cfg(not(all(target_arch = "wasm32", feature = "web")))] - let history = Box::>::default(); - history - }, + history: None, on_update: None, } } } +#[cfg(feature = "serde")] +impl RouterConfig +where + ::Err: std::fmt::Display, + R: serde::Serialize + serde::de::DeserializeOwned, +{ + pub(crate) fn get_history(self) -> Box> { + self.history.unwrap_or_else(|| { + #[cfg(all(target_arch = "wasm32", feature = "web"))] + let history = Box::>::default(); + #[cfg(not(all(target_arch = "wasm32", feature = "web")))] + let history = Box::>::default(); + history + }) + } +} + #[cfg(not(feature = "serde"))] impl Default for RouterConfig where @@ -59,18 +70,28 @@ where fn default() -> Self { Self { failure_external_navigation: FailureExternalNavigation::, - history: { - #[cfg(all(target_arch = "wasm32", feature = "web"))] - let history = Box::>::default(); - #[cfg(not(all(target_arch = "wasm32", feature = "web")))] - let history = Box::>::default(); - history - }, + history: None, on_update: None, } } } +#[cfg(not(feature = "serde"))] +impl RouterConfig +where + ::Err: std::fmt::Display, +{ + pub(crate) fn take_history(&mut self) -> Box> { + self.history.take().unwrap_or_else(|| { + #[cfg(all(target_arch = "wasm32", feature = "web"))] + let history = Box::>::default(); + #[cfg(not(all(target_arch = "wasm32", feature = "web")))] + let history = Box::>::default(); + history + }) + } +} + impl RouterConfig { /// A function to be called whenever the routing is updated. /// @@ -100,7 +121,7 @@ impl RouterConfig { /// Defaults to a default [`MemoryHistory`]. pub fn history(self, history: impl HistoryProvider + 'static) -> Self { Self { - history: Box::new(history), + history: Some(Box::new(history)), ..self } }