change router to use option again

This commit is contained in:
Adrian Wannenmacher committed 2022-12-23 18:55:03 +01:00
1 parent dbbfdefc21
commit f1cbfe0874
7 files changed
+14 -25

No files matched your search

@@ -64,7 +64,7 @@ pub fn GoBackButton<'a>(cx: Scope<'a, HistoryButtonProps<'a>>) -> Element {
error!("{msg}, will be inactive"); error!("{msg}, will be inactive");
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
panic!("{}", msg); panic!("{}", msg);
anyhow::bail!("{msg}"); return None;
} }
}; };
let state = loop { let state = loop {
@@ -139,7 +139,7 @@ pub fn GoForwardButton<'a>(cx: Scope<'a, HistoryButtonProps<'a>>) -> Element {
error!("{msg}, will be inactive"); error!("{msg}, will be inactive");
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
panic!("{}", msg); panic!("{}", msg);
anyhow::bail!("{msg}"); return None;
} }
}; };
let state = loop { let state = loop {
+1 -1
View File
@@ -122,7 +122,7 @@ pub fn Link<'a>(cx: Scope<'a, LinkProps<'a>>) -> Element {
error!("{msg}, will be inactive"); error!("{msg}, will be inactive");
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
panic!("{}", msg); panic!("{}", msg);
anyhow::bail!("{msg}"); return None;
} }
}; };
let state = loop { let state = loop {
+1 -1
View File
@@ -77,7 +77,7 @@ pub fn Outlet(cx: Scope<OutletProps>) -> Element {
error!("{msg}, will be inactive"); error!("{msg}, will be inactive");
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
panic!("{}", msg); panic!("{}", msg);
anyhow::bail!("{msg}"); return None;
} }
}; };
let state = loop { let state = loop {
-8
View File
@@ -1,8 +0,0 @@
/// Errors that may occur during routing.
#[derive(Debug, thiserror::Error)]
pub enum RouterError {
/// A hook that needs access to a router was called inside a component, that has no parent
/// calling the [`use_router`](crate::hooks::use_router) hook.
#[error("component needing access not inside router")]
NotInsideRouter,
}
+5 -5
View File
@@ -1,7 +1,7 @@
use dioxus::prelude::{ScopeId, ScopeState}; use dioxus::prelude::{ScopeId, ScopeState};
use dioxus_router_core::Navigator; use dioxus_router_core::Navigator;
use crate::{utils::use_router_internal::use_router_internal, RouterError}; use crate::{utils::use_router_internal::use_router_internal};
/// A hook that allows for programmatic navigation. /// A hook that allows for programmatic navigation.
/// ///
@@ -58,14 +58,14 @@ use crate::{utils::use_router_internal::use_router_internal, RouterError};
/// # let _ = vdom.rebuild(); /// # let _ = vdom.rebuild();
/// # assert_eq!(dioxus_ssr::render(&vdom), "<h1>App</h1><p>Content</p>"); /// # assert_eq!(dioxus_ssr::render(&vdom), "<h1>App</h1><p>Content</p>");
/// ``` /// ```
pub fn use_navigate(cx: &ScopeState) -> Result<Navigator<ScopeId>, RouterError> { pub fn use_navigate(cx: &ScopeState) -> Option<Navigator<ScopeId>> {
match use_router_internal(cx) { match use_router_internal(cx) {
Some(r) => Ok(r.sender.clone().into()), Some(r) => Some(r.sender.clone().into()),
#[allow(unreachable_code)]
None => { None => {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
panic!("`use_navigate` must have access to a parent router"); panic!("`use_navigate` must have access to a parent router");
Err(RouterError::NotInsideRouter) #[allow(unreachable_code)]
None
} }
} }
} }
+5 -5
View File
@@ -2,7 +2,7 @@ use async_rwlock::RwLockReadGuard;
use dioxus::{core::Component, prelude::ScopeState}; use dioxus::{core::Component, prelude::ScopeState};
use dioxus_router_core::RouterState; use dioxus_router_core::RouterState;
use crate::{utils::use_router_internal::use_router_internal, RouterError}; use crate::{utils::use_router_internal::use_router_internal};
/// A hook that provides access to information about the current routing location. /// A hook that provides access to information about the current routing location.
/// ///
@@ -56,18 +56,18 @@ use crate::{utils::use_router_internal::use_router_internal, RouterError};
/// ``` /// ```
/// ///
/// [`use_router`]: crate::hooks::use_router /// [`use_router`]: crate::hooks::use_router
pub fn use_route(cx: &ScopeState) -> Result<RwLockReadGuard<RouterState<Component>>, RouterError> { pub fn use_route(cx: &ScopeState) -> Option<RwLockReadGuard<RouterState<Component>>> {
match use_router_internal(cx) { match use_router_internal(cx) {
Some(r) => loop { Some(r) => loop {
if let Some(s) = r.state.try_read() { if let Some(s) = r.state.try_read() {
break Ok(s); break Some(s);
} }
}, },
#[allow(unreachable_code)]
None => { None => {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
panic!("`use_route` must have access to a parent router"); panic!("`use_route` must have access to a parent router");
Err(RouterError::NotInsideRouter) #[allow(unreachable_code)]
None
} }
} }
} }
-3
View File
@@ -20,9 +20,6 @@ mod contexts {
pub(crate) mod router; pub(crate) mod router;
} }
mod error;
pub use error::RouterError;
pub use dioxus_router_core::history; pub use dioxus_router_core::history;
/// Hooks for interacting with the router in components. /// Hooks for interacting with the router in components.