wip: debugging
This commit is contained in:
1 parent
875977f5a6
commit
3edf3e367f
11 files changed
+367
-128
No files matched your search
@@ -41,7 +41,7 @@ pub struct LinkProps<'a> {
|
||||
}
|
||||
|
||||
pub fn Link<'a>(cx: Scope<'a, LinkProps<'a>>) -> Element {
|
||||
log::trace!("render Link to {}", cx.props.to);
|
||||
// log::trace!("render Link to {}", cx.props.to);
|
||||
if let Some(service) = cx.consume_context::<RouterService>() {
|
||||
return cx.render(rsx! {
|
||||
a {
|
||||
|
||||
@@ -30,7 +30,7 @@ pub fn Route<'a>(cx: Scope<'a, RouteProps<'a>>) -> Element {
|
||||
Some(ctx) => ctx.total_route.to_string(),
|
||||
None => cx.props.to.to_string(),
|
||||
};
|
||||
log::trace!("total route for {} is {}", cx.props.to, total_route);
|
||||
// log::trace!("total route for {} is {}", cx.props.to, total_route);
|
||||
|
||||
// provide our route context
|
||||
let route_context = cx.provide_context(RouteContext {
|
||||
@@ -48,7 +48,7 @@ pub fn Route<'a>(cx: Scope<'a, RouteProps<'a>>) -> Element {
|
||||
Some(RouteInner {})
|
||||
});
|
||||
|
||||
log::trace!("Checking route {}", cx.props.to);
|
||||
// log::trace!("Checking route {}", cx.props.to);
|
||||
|
||||
if router_root.should_render(cx.scope_id()) {
|
||||
cx.render(rsx!(&cx.props.children))
|
||||
|
||||
@@ -17,6 +17,7 @@ pub struct RouterProps<'a> {
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
pub fn Router<'a>(cx: Scope<'a, RouterProps<'a>>) -> Element {
|
||||
log::debug!("running router {:?}", cx.scope_id());
|
||||
let svc = cx.use_hook(|_| {
|
||||
let update = cx.schedule_update_any();
|
||||
cx.provide_context(RouterService::new(update, cx.scope_id()))
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
pub trait RouterProvider {
|
||||
fn get_current_route(&self) -> String;
|
||||
fn subscribe_to_route_changes(&self, callback: Box<dyn Fn(String)>);
|
||||
}
|
||||
|
||||
enum RouteChange {
|
||||
LinkTo(String),
|
||||
Back(String),
|
||||
fn listen(&self, callback: Box<dyn Fn()>);
|
||||
}
|
||||
@@ -7,14 +7,18 @@ use std::{
|
||||
|
||||
use dioxus_core::ScopeId;
|
||||
|
||||
use crate::platform::RouterProvider;
|
||||
|
||||
pub struct RouterService {
|
||||
pub(crate) regen_route: Rc<dyn Fn(ScopeId)>,
|
||||
pub(crate) pending_events: Rc<RefCell<Vec<RouteEvent>>>,
|
||||
history: Rc<RefCell<BrowserHistory>>,
|
||||
slots: Rc<RefCell<Vec<(ScopeId, String)>>>,
|
||||
onchange_listeners: Rc<RefCell<HashSet<ScopeId>>>,
|
||||
root_found: Rc<Cell<Option<ScopeId>>>,
|
||||
cur_path_params: Rc<RefCell<HashMap<String, String>>>,
|
||||
|
||||
// history: Rc<dyn RouterProvider>,
|
||||
history: Rc<RefCell<BrowserHistory>>,
|
||||
listener: HistoryListener,
|
||||
}
|
||||
|
||||
@@ -58,12 +62,12 @@ impl RouterService {
|
||||
root_found.set(None);
|
||||
// checking if the route is valid is cheap, so we do it
|
||||
for (slot, root) in slots.borrow_mut().iter().rev() {
|
||||
log::trace!("regenerating slot {:?} for root '{}'", slot, root);
|
||||
// log::trace!("regenerating slot {:?} for root '{}'", slot, root);
|
||||
regen_route(*slot);
|
||||
}
|
||||
|
||||
for listener in onchange_listeners.borrow_mut().iter() {
|
||||
log::trace!("regenerating listener {:?}", listener);
|
||||
// log::trace!("regenerating listener {:?}", listener);
|
||||
regen_route(*listener);
|
||||
}
|
||||
|
||||
@@ -87,31 +91,31 @@ impl RouterService {
|
||||
}
|
||||
|
||||
pub fn push_route(&self, route: &str) {
|
||||
log::trace!("Pushing route: {}", route);
|
||||
// log::trace!("Pushing route: {}", route);
|
||||
self.history.borrow_mut().push(route);
|
||||
}
|
||||
|
||||
pub fn register_total_route(&self, route: String, scope: ScopeId, fallback: bool) {
|
||||
let clean = clean_route(route);
|
||||
log::trace!("Registered route '{}' with scope id {:?}", clean, scope);
|
||||
// log::trace!("Registered route '{}' with scope id {:?}", clean, scope);
|
||||
self.slots.borrow_mut().push((scope, clean));
|
||||
}
|
||||
|
||||
pub fn should_render(&self, scope: ScopeId) -> bool {
|
||||
log::trace!("Should render scope id {:?}?", scope);
|
||||
// log::trace!("Should render scope id {:?}?", scope);
|
||||
if let Some(root_id) = self.root_found.get() {
|
||||
log::trace!(" we already found a root with scope id {:?}", root_id);
|
||||
// log::trace!(" we already found a root with scope id {:?}", root_id);
|
||||
if root_id == scope {
|
||||
log::trace!(" yes - it's a match");
|
||||
// log::trace!(" yes - it's a match");
|
||||
return true;
|
||||
}
|
||||
log::trace!(" no - it's not a match");
|
||||
// log::trace!(" no - it's not a match");
|
||||
return false;
|
||||
}
|
||||
|
||||
let location = self.history.borrow().location();
|
||||
let path = location.path();
|
||||
log::trace!(" current path is '{}'", path);
|
||||
// log::trace!(" current path is '{}'", path);
|
||||
|
||||
let roots = self.slots.borrow();
|
||||
|
||||
@@ -120,23 +124,23 @@ impl RouterService {
|
||||
// fallback logic
|
||||
match root {
|
||||
Some((id, route)) => {
|
||||
log::trace!(
|
||||
" matched given scope id {:?} with route root '{}'",
|
||||
scope,
|
||||
route,
|
||||
);
|
||||
// log::trace!(
|
||||
// " matched given scope id {:?} with route root '{}'",
|
||||
// scope,
|
||||
// route,
|
||||
// );
|
||||
if let Some(params) = route_matches_path(route, path) {
|
||||
log::trace!(" and it matches the current path '{}'", path);
|
||||
// log::trace!(" and it matches the current path '{}'", path);
|
||||
self.root_found.set(Some(*id));
|
||||
*self.cur_path_params.borrow_mut() = params;
|
||||
true
|
||||
} else {
|
||||
if route == "" {
|
||||
log::trace!(" and the route is the root, so we will use that without a better match");
|
||||
// log::trace!(" and the route is the root, so we will use that without a better match");
|
||||
self.root_found.set(Some(*id));
|
||||
true
|
||||
} else {
|
||||
log::trace!(" and the route '{}' is not the root nor does it match the current path", route);
|
||||
// log::trace!(" and the route '{}' is not the root nor does it match the current path", route);
|
||||
false
|
||||
}
|
||||
}
|
||||
@@ -154,12 +158,12 @@ impl RouterService {
|
||||
}
|
||||
|
||||
pub fn subscribe_onchange(&self, id: ScopeId) {
|
||||
log::trace!("Subscribing onchange for scope id {:?}", id);
|
||||
// log::trace!("Subscribing onchange for scope id {:?}", id);
|
||||
self.onchange_listeners.borrow_mut().insert(id);
|
||||
}
|
||||
|
||||
pub fn unsubscribe_onchange(&self, id: ScopeId) {
|
||||
log::trace!("Subscribing onchange for scope id {:?}", id);
|
||||
// log::trace!("Subscribing onchange for scope id {:?}", id);
|
||||
self.onchange_listeners.borrow_mut().remove(&id);
|
||||
}
|
||||
}
|
||||
@@ -182,36 +186,36 @@ fn route_matches_path(route: &str, path: &str) -> Option<HashMap<String, String>
|
||||
let route_pieces = route.split('/').collect::<Vec<_>>();
|
||||
let path_pieces = clean_path(path).split('/').collect::<Vec<_>>();
|
||||
|
||||
log::trace!(
|
||||
" checking route pieces {:?} vs path pieces {:?}",
|
||||
route_pieces,
|
||||
path_pieces,
|
||||
);
|
||||
// log::trace!(
|
||||
// " checking route pieces {:?} vs path pieces {:?}",
|
||||
// route_pieces,
|
||||
// path_pieces,
|
||||
// );
|
||||
|
||||
if route_pieces.len() != path_pieces.len() {
|
||||
log::trace!(" the routes are different lengths");
|
||||
// log::trace!(" the routes are different lengths");
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut matches = HashMap::new();
|
||||
for (i, r) in route_pieces.iter().enumerate() {
|
||||
log::trace!(" checking route piece '{}' vs path", r);
|
||||
// log::trace!(" checking route piece '{}' vs path", r);
|
||||
// If this is a parameter then it matches as long as there's
|
||||
// _any_thing in that spot in the path.
|
||||
if r.starts_with(':') {
|
||||
log::trace!(
|
||||
" route piece '{}' starts with a colon so it matches anything",
|
||||
r,
|
||||
);
|
||||
// log::trace!(
|
||||
// " route piece '{}' starts with a colon so it matches anything",
|
||||
// r,
|
||||
// );
|
||||
let param = &r[1..];
|
||||
matches.insert(param.to_string(), path_pieces[i].to_string());
|
||||
continue;
|
||||
}
|
||||
log::trace!(
|
||||
" route piece '{}' must be an exact match for path piece '{}'",
|
||||
r,
|
||||
path_pieces[i],
|
||||
);
|
||||
// log::trace!(
|
||||
// " route piece '{}' must be an exact match for path piece '{}'",
|
||||
// r,
|
||||
// path_pieces[i],
|
||||
// );
|
||||
if path_pieces[i] != *r {
|
||||
return None;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user