feat: connect an onchange listener

This commit is contained in:
Jonathan Kelley committed 2022-01-25 15:06:37 -05:00
1 parent 71371d570a
commit 29ed7ebece
4 files changed
+42 -30

No files matched your search

+28 -12
View File
@@ -9,6 +9,7 @@ use dioxus_core::ScopeId;
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)>>>,
root_found: Rc<Cell<Option<ScopeId>>>,
@@ -16,6 +17,12 @@ pub struct RouterService {
listener: HistoryListener,
}
pub enum RouteEvent {
Change,
Pop,
Push,
}
enum RouteSlot {
Routes {
// the partial route
@@ -36,28 +43,37 @@ impl RouterService {
let path = location.path();
let slots: Rc<RefCell<Vec<(ScopeId, String)>>> = Default::default();
let _slots = slots.clone();
let pending_events: Rc<RefCell<Vec<RouteEvent>>> = Default::default();
let root_found = Rc::new(Cell::new(None));
let regen = regen_route.clone();
let _root_found = root_found.clone();
let listener = history.listen(move || {
_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);
regen(*slot);
let listener = history.listen({
let pending_events = pending_events.clone();
let regen_route = regen_route.clone();
let root_found = root_found.clone();
let slots = slots.clone();
move || {
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);
regen_route(*slot);
}
// also regenerate the root
regen_route(root_scope);
pending_events.borrow_mut().push(RouteEvent::Change)
}
});
Self {
listener,
root_found,
history: Rc::new(RefCell::new(history)),
regen_route,
slots,
pending_events,
cur_path_params: Rc::new(RefCell::new(HashMap::new())),
listener,
}
}