Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use_route should subscribe to changes to the route #168

Merged
merged 4 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions packages/router/src/hooks/use_route.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dioxus_core::ScopeState;
use dioxus_core::{ScopeId, ScopeState};
use gloo::history::{HistoryResult, Location};
use serde::de::DeserializeOwned;
use std::{rc::Rc, str::FromStr};
Expand Down Expand Up @@ -74,10 +74,28 @@ impl UseRoute {
/// This hook provides access to information about the current location in the
/// context of a [`Router`]. If this function is called outside of a `Router`
/// component it will panic.
pub fn use_route(cx: &ScopeState) -> UseRoute {
let router = cx
.consume_context::<RouterService>()
.expect("Cannot call use_route outside the scope of a Router component")
.clone();
UseRoute { router }
pub fn use_route(cx: &ScopeState) -> &UseRoute {
&cx.use_hook(|_| {
let router = cx
.consume_context::<RouterService>()
.expect("Cannot call use_route outside the scope of a Router component");

router.subscribe_onchange(cx.scope_id());

UseRouteInner {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think calling this UseRouteInner is confusing when it's not contained in a UseRoute. Maybe the existing UseRoute needs to be renamed to Inner and this one should be UseRoute?

Copy link
Member Author

@jkelleyrtp jkelleyrtp Jan 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmmm maybe something like UseRouteListener since its only job is to be dropped when the component is unmounted?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense.

router: UseRoute { router },
scope: cx.scope_id(),
}
})
.router
}

struct UseRouteInner {
router: UseRoute,
scope: ScopeId,
}
impl Drop for UseRouteInner {
fn drop(&mut self) {
self.router.router.unsubscribe_onchange(self.scope)
}
}
14 changes: 13 additions & 1 deletion packages/router/src/service.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use gloo::history::{BrowserHistory, History, HistoryListener, Location};
use std::{
cell::{Cell, Ref, RefCell},
collections::HashMap,
collections::{HashMap, HashSet},
rc::Rc,
};

Expand All @@ -12,6 +12,7 @@ pub struct RouterService {
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>>>,
listener: HistoryListener,
Expand Down Expand Up @@ -73,6 +74,7 @@ impl RouterService {
regen_route,
slots,
pending_events,
onchange_listeners: Rc::new(RefCell::new(HashSet::new())),
cur_path_params: Rc::new(RefCell::new(HashMap::new())),
}
}
Expand Down Expand Up @@ -143,6 +145,16 @@ impl RouterService {
pub fn current_path_params(&self) -> Ref<HashMap<String, String>> {
self.cur_path_params.borrow()
}

pub fn subscribe_onchange(&self, id: ScopeId) {
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);
self.onchange_listeners.borrow_mut().remove(&id);
}
}

fn clean_route(route: String) -> String {
Expand Down