create cfg factory

This commit is contained in:
Evan Almloff committed 2023-06-01 13:13:50 -05:00
1 parent 037a248ad9
commit 67992f7da9
6 files changed
+90 -72

No files matched your search

+16 -11
View File
@@ -9,25 +9,26 @@ use crate::{
};
/// The config for [`GenericRouter`].
pub struct RouterCfg<R: Routable> {
config: RefCell<Option<RouterConfiguration<R>>>,
pub struct RouterConfigFactory<R: Routable> {
#[allow(clippy::type_complexity)]
config: RefCell<Option<Box<dyn FnOnce() -> RouterConfiguration<R>>>>,
}
impl<R: Routable> Default for RouterCfg<R>
impl<R: Routable> Default for RouterConfigFactory<R>
where
<R as FromStr>::Err: std::fmt::Display,
{
fn default() -> Self {
Self {
config: RefCell::new(Some(RouterConfiguration::default())),
}
Self::from(RouterConfiguration::default)
}
}
impl<R: Routable> From<RouterConfiguration<R>> for RouterCfg<R> {
fn from(value: RouterConfiguration<R>) -> Self {
impl<R: Routable, F: FnOnce() -> RouterConfiguration<R> + 'static> From<F>
for RouterConfigFactory<R>
{
fn from(value: F) -> Self {
Self {
config: RefCell::new(Some(value)),
config: RefCell::new(Some(Box::new(value))),
}
}
}
@@ -39,7 +40,7 @@ where
<R as FromStr>::Err: std::fmt::Display,
{
#[props(default, into)]
config: RouterCfg<R>,
config: RouterConfigFactory<R>,
}
impl<R: Routable> PartialEq for GenericRouterProps<R>
@@ -66,7 +67,11 @@ where
panic!("{}", msg);
}
let router = GenericRouterContext::new(
cx.props.config.config.take().unwrap_or_default(),
(cx.props
.config
.config
.take()
.expect("use_context_provider ran twice"))(),
cx.schedule_update_any(),
);
router
+1 -1
View File
@@ -15,7 +15,7 @@ use crate::{
pub struct ExternalNavigationFailure(String);
/// A function the router will call after every routing update.
pub type RoutingCallback<R> = Arc<dyn Fn(GenericRouterContext<R>) -> Option<NavigationTarget<R>>>;
pub(crate) type RoutingCallback<R> = Arc<dyn Fn(GenericRouterContext<R>) -> Option<NavigationTarget<R>>>;
struct MutableRouterState<R>
where
+55 -24
View File
@@ -1,3 +1,5 @@
use std::sync::Arc;
use crate::contexts::router::RoutingCallback;
use crate::history::HistoryProvider;
use crate::routable::Routable;
@@ -7,7 +9,7 @@ use crate::prelude::*;
/// Global configuration options for the router.
///
/// This implements [`Default`], so you can use it like this:
/// This implements [`Default`] and follows the builder pattern, so you can use it like this:
/// ```rust,no_run
/// # use dioxus_router::prelude::*;
/// # use serde::{Deserialize, Serialize};
@@ -21,20 +23,34 @@ use crate::prelude::*;
/// #[route("/")]
/// Index {},
/// }
/// let cfg = RouterConfiguration {
/// history: Box::<WebHistory<Route>>::default(),
/// ..Default::default()
/// };
/// let cfg = RouterConfiguration::default().history(WebHistory<Route>::default());
/// ```
pub struct RouterConfiguration<R: Routable> {
/// A component to render when an external navigation fails.
///
/// Defaults to a router-internal component called [`FailureExternalNavigation`]
pub failure_external_navigation: fn(Scope) -> Element,
/// The [`HistoryProvider`] the router should use.
///
/// Defaults to a default [`MemoryHistory`].
pub history: Box<dyn HistoryProvider<R>>,
pub(crate) failure_external_navigation: fn(Scope) -> Element,
pub(crate) history: Box<dyn HistoryProvider<R>>,
pub(crate) on_update: Option<RoutingCallback<R>>,
}
impl<R: Routable + Clone> Default for RouterConfiguration<R>
where
<R as std::str::FromStr>::Err: std::fmt::Display,
{
fn default() -> Self {
Self {
failure_external_navigation: FailureExternalNavigation::<R>,
history: {
#[cfg(all(target_arch = "wasm32", feature = "web"))]
let history = Box::<MemoryHistory<R>>::default();
#[cfg(not(all(target_arch = "wasm32", feature = "web")))]
let history = Box::<MemoryHistory<R>>::default();
history
},
on_update: None,
}
}
}
impl<R: Routable> RouterConfiguration<R> {
/// A function to be called whenever the routing is updated.
///
/// The callback is invoked after the routing is updated, but before components and hooks are
@@ -48,18 +64,33 @@ pub struct RouterConfiguration<R: Routable> {
/// navigation failure occurs.
///
/// Defaults to [`None`].
pub on_update: Option<RoutingCallback<R>>,
}
impl<R: Routable + Clone> Default for RouterConfiguration<R>
where
<R as std::str::FromStr>::Err: std::fmt::Display,
{
fn default() -> Self {
pub fn on_update(
self,
callback: impl Fn(GenericRouterContext<R>) -> Option<NavigationTarget<R>> + 'static,
) -> Self {
Self {
failure_external_navigation: FailureExternalNavigation::<R>,
history: Box::<MemoryHistory<R>>::default(),
on_update: None,
on_update: Some(Arc::new(callback)),
..self
}
}
/// The [`HistoryProvider`] the router should use.
///
/// Defaults to a default [`MemoryHistory`].
pub fn history(self, history: impl HistoryProvider<R> + 'static) -> Self {
Self {
history: Box::new(history),
..self
}
}
/// A component to render when an external navigation fails.
///
/// Defaults to a router-internal component called [`FailureExternalNavigation`]
pub fn failure_external_navigation(self, component: fn(Scope) -> Element) -> Self {
Self {
failure_external_navigation: component,
..self
}
}
}