mirror of
https://github.com/DioxusLabs/dioxus
synced 2025-02-18 14:48:26 +00:00
make the router and navigator Copy
This commit is contained in:
parent
89b1e56fc3
commit
a0f1b1fc3e
3 changed files with 78 additions and 90 deletions
|
@ -75,15 +75,5 @@ pub fn launch(
|
||||||
virtual_dom.insert_any_root_context(context());
|
virtual_dom.insert_any_root_context(context());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "tokio")]
|
launch_virtual_dom(virtual_dom, desktop_config)
|
||||||
tokio::runtime::Builder::new_multi_thread()
|
|
||||||
.enable_all()
|
|
||||||
.build()
|
|
||||||
.unwrap()
|
|
||||||
.block_on(tokio::task::unconstrained(async move {
|
|
||||||
launch_with_props_blocking(virtual_dom, platform_config)
|
|
||||||
}));
|
|
||||||
|
|
||||||
#[cfg(not(feature = "tokio"))]
|
|
||||||
launch_with_props_blocking(virtual_dom, platform_config)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ pub fn navigator() -> Navigator {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A view into the navigation state of a router.
|
/// A view into the navigation state of a router.
|
||||||
#[derive(Clone)]
|
#[derive(Clone, Copy)]
|
||||||
pub struct Navigator(pub(crate) RouterContext);
|
pub struct Navigator(pub(crate) RouterContext);
|
||||||
|
|
||||||
impl Navigator {
|
impl Navigator {
|
||||||
|
|
|
@ -25,19 +25,13 @@ pub(crate) type RoutingCallback<R> =
|
||||||
pub(crate) type AnyRoutingCallback =
|
pub(crate) type AnyRoutingCallback =
|
||||||
Arc<dyn Fn(RouterContext) -> Option<NavigationTarget<Rc<dyn Any>>>>;
|
Arc<dyn Fn(RouterContext) -> Option<NavigationTarget<Rc<dyn Any>>>>;
|
||||||
|
|
||||||
struct MutableRouterState {
|
struct RouterContextInner {
|
||||||
/// The current prefix.
|
/// The current prefix.
|
||||||
prefix: Option<String>,
|
prefix: Option<String>,
|
||||||
|
|
||||||
history: Box<dyn AnyHistoryProvider>,
|
history: Box<dyn AnyHistoryProvider>,
|
||||||
|
|
||||||
unresolved_error: Option<ExternalNavigationFailure>,
|
unresolved_error: Option<ExternalNavigationFailure>,
|
||||||
}
|
|
||||||
|
|
||||||
/// A collection of router data that manages all routing functionality.
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct RouterContext {
|
|
||||||
state: Rc<RefCell<MutableRouterState>>,
|
|
||||||
|
|
||||||
subscribers: Arc<RwLock<HashSet<ScopeId>>>,
|
subscribers: Arc<RwLock<HashSet<ScopeId>>>,
|
||||||
subscriber_update: Arc<dyn Fn(ScopeId)>,
|
subscriber_update: Arc<dyn Fn(ScopeId)>,
|
||||||
|
@ -48,6 +42,35 @@ pub struct RouterContext {
|
||||||
any_route_to_string: fn(&dyn Any) -> String,
|
any_route_to_string: fn(&dyn Any) -> String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl RouterContextInner {
|
||||||
|
fn update_subscribers(&self) {
|
||||||
|
let update = &self.subscriber_update;
|
||||||
|
for &id in self.subscribers.read().unwrap().iter() {
|
||||||
|
update(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn external(&mut self, external: String) -> Option<ExternalNavigationFailure> {
|
||||||
|
match self.history.external(external.clone()) {
|
||||||
|
true => None,
|
||||||
|
false => {
|
||||||
|
let failure = ExternalNavigationFailure(external);
|
||||||
|
self.unresolved_error = Some(failure.clone());
|
||||||
|
|
||||||
|
self.update_subscribers();
|
||||||
|
|
||||||
|
Some(failure)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A collection of router data that manages all routing functionality.
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
pub struct RouterContext {
|
||||||
|
inner: CopyValue<RouterContextInner>,
|
||||||
|
}
|
||||||
|
|
||||||
impl RouterContext {
|
impl RouterContext {
|
||||||
pub(crate) fn new<R: Routable + 'static>(
|
pub(crate) fn new<R: Routable + 'static>(
|
||||||
mut cfg: RouterConfig<R>,
|
mut cfg: RouterConfig<R>,
|
||||||
|
@ -57,17 +80,13 @@ impl RouterContext {
|
||||||
R: Clone,
|
R: Clone,
|
||||||
<R as std::str::FromStr>::Err: std::fmt::Display,
|
<R as std::str::FromStr>::Err: std::fmt::Display,
|
||||||
{
|
{
|
||||||
let state = Rc::new(RefCell::new(MutableRouterState {
|
|
||||||
prefix: Default::default(),
|
|
||||||
history: cfg.take_history(),
|
|
||||||
unresolved_error: None,
|
|
||||||
}));
|
|
||||||
|
|
||||||
let subscriber_update = mark_dirty.clone();
|
let subscriber_update = mark_dirty.clone();
|
||||||
let subscribers = Arc::new(RwLock::new(HashSet::new()));
|
let subscribers = Arc::new(RwLock::new(HashSet::new()));
|
||||||
|
|
||||||
let myself = Self {
|
let mut myself = RouterContextInner {
|
||||||
state,
|
prefix: Default::default(),
|
||||||
|
history: cfg.take_history(),
|
||||||
|
unresolved_error: None,
|
||||||
subscribers: subscribers.clone(),
|
subscribers: subscribers.clone(),
|
||||||
subscriber_update,
|
subscriber_update,
|
||||||
|
|
||||||
|
@ -106,32 +125,32 @@ impl RouterContext {
|
||||||
|
|
||||||
// set the updater
|
// set the updater
|
||||||
{
|
{
|
||||||
let mut state = myself.state.borrow_mut();
|
myself.history.updater(Arc::new(move || {
|
||||||
state.history.updater(Arc::new(move || {
|
|
||||||
for &id in subscribers.read().unwrap().iter() {
|
for &id in subscribers.read().unwrap().iter() {
|
||||||
(mark_dirty)(id);
|
(mark_dirty)(id);
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
myself
|
Self {
|
||||||
|
inner: CopyValue::new_in_scope(myself, ScopeId::ROOT),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn route_from_str(&self, route: &str) -> Result<Rc<dyn Any>, String> {
|
pub(crate) fn route_from_str(&self, route: &str) -> Result<Rc<dyn Any>, String> {
|
||||||
let state = self.state.borrow();
|
self.inner.read().history.parse_route(route)
|
||||||
state.history.parse_route(route)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check whether there is a previous page to navigate back to.
|
/// Check whether there is a previous page to navigate back to.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn can_go_back(&self) -> bool {
|
pub fn can_go_back(&self) -> bool {
|
||||||
self.state.borrow().history.can_go_back()
|
self.inner.read().history.can_go_back()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check whether there is a future page to navigate forward to.
|
/// Check whether there is a future page to navigate forward to.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn can_go_forward(&self) -> bool {
|
pub fn can_go_forward(&self) -> bool {
|
||||||
self.state.borrow().history.can_go_forward()
|
self.inner.read().history.can_go_forward()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Go back to the previous location.
|
/// Go back to the previous location.
|
||||||
|
@ -139,7 +158,7 @@ impl RouterContext {
|
||||||
/// Will fail silently if there is no previous location to go to.
|
/// Will fail silently if there is no previous location to go to.
|
||||||
pub fn go_back(&self) {
|
pub fn go_back(&self) {
|
||||||
{
|
{
|
||||||
self.state.borrow_mut().history.go_back();
|
self.inner.write().history.go_back();
|
||||||
}
|
}
|
||||||
|
|
||||||
self.change_route();
|
self.change_route();
|
||||||
|
@ -150,7 +169,7 @@ impl RouterContext {
|
||||||
/// Will fail silently if there is no next location to go to.
|
/// Will fail silently if there is no next location to go to.
|
||||||
pub fn go_forward(&self) {
|
pub fn go_forward(&self) {
|
||||||
{
|
{
|
||||||
self.state.borrow_mut().history.go_forward();
|
self.inner.write().history.go_forward();
|
||||||
}
|
}
|
||||||
|
|
||||||
self.change_route();
|
self.change_route();
|
||||||
|
@ -160,12 +179,12 @@ impl RouterContext {
|
||||||
&self,
|
&self,
|
||||||
target: NavigationTarget<Rc<dyn Any>>,
|
target: NavigationTarget<Rc<dyn Any>>,
|
||||||
) -> Option<ExternalNavigationFailure> {
|
) -> Option<ExternalNavigationFailure> {
|
||||||
match target {
|
{
|
||||||
NavigationTarget::Internal(p) => {
|
let mut write = self.inner.write();
|
||||||
let mut state = self.state_mut();
|
match target {
|
||||||
state.history.push(p)
|
NavigationTarget::Internal(p) => write.history.push(p),
|
||||||
|
NavigationTarget::External(e) => return write.external(e),
|
||||||
}
|
}
|
||||||
NavigationTarget::External(e) => return self.external(e),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.change_route()
|
self.change_route()
|
||||||
|
@ -176,12 +195,12 @@ impl RouterContext {
|
||||||
/// The previous location will be available to go back to.
|
/// The previous location will be available to go back to.
|
||||||
pub fn push(&self, target: impl Into<IntoRoutable>) -> Option<ExternalNavigationFailure> {
|
pub fn push(&self, target: impl Into<IntoRoutable>) -> Option<ExternalNavigationFailure> {
|
||||||
let target = self.resolve_into_routable(target.into());
|
let target = self.resolve_into_routable(target.into());
|
||||||
match target {
|
{
|
||||||
NavigationTarget::Internal(p) => {
|
let mut write = self.inner.write();
|
||||||
let mut state = self.state_mut();
|
match target {
|
||||||
state.history.push(p)
|
NavigationTarget::Internal(p) => write.history.push(p),
|
||||||
|
NavigationTarget::External(e) => return write.external(e),
|
||||||
}
|
}
|
||||||
NavigationTarget::External(e) => return self.external(e),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.change_route()
|
self.change_route()
|
||||||
|
@ -194,10 +213,10 @@ impl RouterContext {
|
||||||
let target = self.resolve_into_routable(target.into());
|
let target = self.resolve_into_routable(target.into());
|
||||||
|
|
||||||
{
|
{
|
||||||
let mut state = self.state_mut();
|
let mut state = self.inner.write();
|
||||||
match target {
|
match target {
|
||||||
NavigationTarget::Internal(p) => state.history.replace(p),
|
NavigationTarget::Internal(p) => state.history.replace(p),
|
||||||
NavigationTarget::External(e) => return self.external(e),
|
NavigationTarget::External(e) => return state.external(e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -206,8 +225,8 @@ impl RouterContext {
|
||||||
|
|
||||||
/// The route that is currently active.
|
/// The route that is currently active.
|
||||||
pub fn current<R: Routable>(&self) -> R {
|
pub fn current<R: Routable>(&self) -> R {
|
||||||
self.state
|
self.inner
|
||||||
.borrow()
|
.read()
|
||||||
.history
|
.history
|
||||||
.current_route()
|
.current_route()
|
||||||
.downcast::<R>()
|
.downcast::<R>()
|
||||||
|
@ -218,11 +237,11 @@ impl RouterContext {
|
||||||
|
|
||||||
/// The route that is currently active.
|
/// The route that is currently active.
|
||||||
pub fn current_route_string(&self) -> String {
|
pub fn current_route_string(&self) -> String {
|
||||||
self.any_route_to_string(&*self.state.borrow().history.current_route())
|
self.any_route_to_string(&*self.inner.read().history.current_route())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn any_route_to_string(&self, route: &dyn Any) -> String {
|
pub(crate) fn any_route_to_string(&self, route: &dyn Any) -> String {
|
||||||
(self.any_route_to_string)(route)
|
(self.inner.read().any_route_to_string)(route)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn resolve_into_routable(
|
pub(crate) fn resolve_into_routable(
|
||||||
|
@ -243,72 +262,51 @@ impl RouterContext {
|
||||||
|
|
||||||
/// The prefix that is currently active.
|
/// The prefix that is currently active.
|
||||||
pub fn prefix(&self) -> Option<String> {
|
pub fn prefix(&self) -> Option<String> {
|
||||||
self.state.borrow().prefix.clone()
|
self.inner.read().prefix.clone()
|
||||||
}
|
|
||||||
|
|
||||||
fn external(&self, external: String) -> Option<ExternalNavigationFailure> {
|
|
||||||
let mut state = self.state_mut();
|
|
||||||
match state.history.external(external.clone()) {
|
|
||||||
true => None,
|
|
||||||
false => {
|
|
||||||
let failure = ExternalNavigationFailure(external);
|
|
||||||
state.unresolved_error = Some(failure.clone());
|
|
||||||
|
|
||||||
self.update_subscribers();
|
|
||||||
|
|
||||||
Some(failure)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn state_mut(&self) -> RefMut<MutableRouterState> {
|
|
||||||
self.state.borrow_mut()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Manually subscribe to the current route
|
/// Manually subscribe to the current route
|
||||||
pub fn subscribe(&self, id: ScopeId) {
|
pub fn subscribe(&self, id: ScopeId) {
|
||||||
self.subscribers.write().unwrap().insert(id);
|
self.inner.read().subscribers.write().unwrap().insert(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Manually unsubscribe from the current route
|
/// Manually unsubscribe from the current route
|
||||||
pub fn unsubscribe(&self, id: ScopeId) {
|
pub fn unsubscribe(&self, id: ScopeId) {
|
||||||
self.subscribers.write().unwrap().remove(&id);
|
self.inner.read().subscribers.write().unwrap().remove(&id);
|
||||||
}
|
|
||||||
|
|
||||||
fn update_subscribers(&self) {
|
|
||||||
for &id in self.subscribers.read().unwrap().iter() {
|
|
||||||
(self.subscriber_update)(id);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Clear any unresolved errors
|
/// Clear any unresolved errors
|
||||||
pub fn clear_error(&self) {
|
pub fn clear_error(&self) {
|
||||||
self.state.borrow_mut().unresolved_error = None;
|
let mut write_inner = self.inner.write();
|
||||||
|
write_inner.unresolved_error = None;
|
||||||
|
|
||||||
self.update_subscribers();
|
write_inner.update_subscribers();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn render_error(&self) -> Element {
|
pub(crate) fn render_error(&self) -> Element {
|
||||||
self.state
|
let inner_read = self.inner.write();
|
||||||
.borrow()
|
inner_read
|
||||||
.unresolved_error
|
.unresolved_error
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.and_then(|_| (self.failure_external_navigation)())
|
.and_then(|_| (inner_read.failure_external_navigation)())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn change_route(&self) -> Option<ExternalNavigationFailure> {
|
fn change_route(&self) -> Option<ExternalNavigationFailure> {
|
||||||
if let Some(callback) = &self.routing_callback {
|
let self_read = self.inner.read();
|
||||||
|
if let Some(callback) = &self_read.routing_callback {
|
||||||
let myself = self.clone();
|
let myself = self.clone();
|
||||||
|
let callback = callback.clone();
|
||||||
|
drop(self_read);
|
||||||
if let Some(new) = callback(myself) {
|
if let Some(new) = callback(myself) {
|
||||||
let mut state = self.state_mut();
|
let mut self_write = self.inner.write();
|
||||||
match new {
|
match new {
|
||||||
NavigationTarget::Internal(p) => state.history.replace(p),
|
NavigationTarget::Internal(p) => self_write.history.replace(p),
|
||||||
NavigationTarget::External(e) => return self.external(e),
|
NavigationTarget::External(e) => return self_write.external(e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.update_subscribers();
|
self.inner.read().update_subscribers();
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue