dioxus_router/contexts/
router.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
use std::{
    collections::HashSet,
    sync::{Arc, Mutex},
};

use dioxus_history::history;
use dioxus_lib::prelude::*;

use crate::{
    components::child_router::consume_child_route_mapping, navigation::NavigationTarget,
    prelude::SiteMapSegment, routable::Routable, router_cfg::RouterConfig,
};

/// This context is set in the root of the virtual dom if there is a router present.
#[derive(Clone, Copy)]
struct RootRouterContext(Signal<Option<RouterContext>>);

/// Try to get the router that was created closest to the root of the virtual dom. This may be called outside of the router.
///
/// This will return `None` if there is no router present or the router has not been created yet.
pub fn root_router() -> Option<RouterContext> {
    if let Some(ctx) = ScopeId::ROOT.consume_context::<RootRouterContext>() {
        ctx.0.cloned()
    } else {
        ScopeId::ROOT.provide_context(RootRouterContext(Signal::new_in_scope(None, ScopeId::ROOT)));
        None
    }
}

pub(crate) fn provide_router_context(ctx: RouterContext) {
    if root_router().is_none() {
        ScopeId::ROOT.provide_context(RootRouterContext(Signal::new_in_scope(
            Some(ctx),
            ScopeId::ROOT,
        )));
    }
    provide_context(ctx);
}

/// An error that can occur when navigating.
#[derive(Debug, Clone)]
pub struct ExternalNavigationFailure(pub String);

/// A function the router will call after every routing update.
pub(crate) type RoutingCallback<R> =
    Arc<dyn Fn(GenericRouterContext<R>) -> Option<NavigationTarget<R>>>;
pub(crate) type AnyRoutingCallback = Arc<dyn Fn(RouterContext) -> Option<NavigationTarget>>;

struct RouterContextInner {
    unresolved_error: Option<ExternalNavigationFailure>,

    subscribers: Arc<Mutex<HashSet<ReactiveContext>>>,
    routing_callback: Option<AnyRoutingCallback>,

    failure_external_navigation: fn() -> Element,

    internal_route: fn(&str) -> bool,

    site_map: &'static [SiteMapSegment],
}

impl RouterContextInner {
    fn update_subscribers(&self) {
        for &id in self.subscribers.lock().unwrap().iter() {
            id.mark_dirty();
        }
    }

    fn subscribe_to_current_context(&self) {
        if let Some(rc) = ReactiveContext::current() {
            rc.subscribe(self.subscribers.clone());
        }
    }

    fn external(&mut self, external: String) -> Option<ExternalNavigationFailure> {
        match 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 {
    pub(crate) fn new<R: Routable + 'static>(cfg: RouterConfig<R>) -> Self
    where
        <R as std::str::FromStr>::Err: std::fmt::Display,
    {
        let subscribers = Arc::new(Mutex::new(HashSet::new()));
        let mapping = consume_child_route_mapping();

        let myself = RouterContextInner {
            unresolved_error: None,
            subscribers: subscribers.clone(),
            routing_callback: cfg.on_update.map(|update| {
                Arc::new(move |ctx| {
                    let ctx = GenericRouterContext {
                        inner: ctx,
                        _marker: std::marker::PhantomData,
                    };
                    update(ctx).map(|t| match t {
                        NavigationTarget::Internal(r) => match mapping.as_ref() {
                            Some(mapping) => {
                                NavigationTarget::Internal(mapping.format_route_as_root_route(r))
                            }
                            None => NavigationTarget::Internal(r.to_string()),
                        },
                        NavigationTarget::External(s) => NavigationTarget::External(s),
                    })
                }) as Arc<dyn Fn(RouterContext) -> Option<NavigationTarget>>
            }),

            failure_external_navigation: cfg.failure_external_navigation,

            internal_route: |route| R::from_str(route).is_ok(),

            site_map: R::SITE_MAP,
        };

        // set the updater
        history().updater(Arc::new(move || {
            for &rc in subscribers.lock().unwrap().iter() {
                rc.mark_dirty();
            }
        }));

        Self {
            inner: CopyValue::new_in_scope(myself, ScopeId::ROOT),
        }
    }

    /// Check if the router is running in a liveview context
    /// We do some slightly weird things for liveview because of the network boundary
    pub(crate) fn include_prevent_default(&self) -> bool {
        history().include_prevent_default()
    }

    /// Check whether there is a previous page to navigate back to.
    #[must_use]
    pub fn can_go_back(&self) -> bool {
        history().can_go_back()
    }

    /// Check whether there is a future page to navigate forward to.
    #[must_use]
    pub fn can_go_forward(&self) -> bool {
        history().can_go_forward()
    }

    /// Go back to the previous location.
    ///
    /// Will fail silently if there is no previous location to go to.
    pub fn go_back(&self) {
        history().go_back();
        self.change_route();
    }

    /// Go back to the next location.
    ///
    /// Will fail silently if there is no next location to go to.
    pub fn go_forward(&self) {
        history().go_forward();
        self.change_route();
    }

    pub(crate) fn push_any(&self, target: NavigationTarget) -> Option<ExternalNavigationFailure> {
        {
            let mut write = self.inner.write_unchecked();
            match target {
                NavigationTarget::Internal(p) => history().push(p),
                NavigationTarget::External(e) => return write.external(e),
            }
        }

        self.change_route()
    }

    /// Push a new location.
    ///
    /// The previous location will be available to go back to.
    pub fn push(&self, target: impl Into<NavigationTarget>) -> Option<ExternalNavigationFailure> {
        let target = target.into();
        {
            let mut write = self.inner.write_unchecked();
            match target {
                NavigationTarget::Internal(p) => {
                    let history = history();
                    history.push(p)
                }
                NavigationTarget::External(e) => return write.external(e),
            }
        }

        self.change_route()
    }

    /// Replace the current location.
    ///
    /// The previous location will **not** be available to go back to.
    pub fn replace(
        &self,
        target: impl Into<NavigationTarget>,
    ) -> Option<ExternalNavigationFailure> {
        let target = target.into();
        {
            let mut state = self.inner.write_unchecked();
            match target {
                NavigationTarget::Internal(p) => {
                    let history = history();
                    history.replace(p)
                }
                NavigationTarget::External(e) => return state.external(e),
            }
        }

        self.change_route()
    }

    /// The route that is currently active.
    pub fn current<R: Routable>(&self) -> R {
        let absolute_route = self.full_route_string();
        // If this is a child route, map the absolute route to the child route before parsing
        let mapping = consume_child_route_mapping::<R>();
        match mapping.as_ref() {
            Some(mapping) => mapping
                .parse_route_from_root_route(&absolute_route)
                .unwrap_or_else(|| {
                    panic!("route's display implementation must be parsable by FromStr")
                }),
            None => R::from_str(&absolute_route).unwrap_or_else(|_| {
                panic!("route's display implementation must be parsable by FromStr")
            }),
        }
    }

    /// The full route that is currently active. If this is called from inside a child router, this will always return the parent's view of the route.
    pub fn full_route_string(&self) -> String {
        let inner = self.inner.read();
        inner.subscribe_to_current_context();
        let history = history();
        history.current_route()
    }

    /// The prefix that is currently active.
    pub fn prefix(&self) -> Option<String> {
        let history = history();
        history.current_prefix()
    }

    /// Clear any unresolved errors
    pub fn clear_error(&self) {
        let mut write_inner = self.inner.write_unchecked();
        write_inner.unresolved_error = None;

        write_inner.update_subscribers();
    }

    /// Get the site map of the router.
    pub fn site_map(&self) -> &'static [SiteMapSegment] {
        self.inner.read().site_map
    }

    pub(crate) fn render_error(&self) -> Option<Element> {
        let inner_write = self.inner.write_unchecked();
        inner_write.subscribe_to_current_context();
        inner_write
            .unresolved_error
            .as_ref()
            .map(|_| (inner_write.failure_external_navigation)())
    }

    fn change_route(&self) -> Option<ExternalNavigationFailure> {
        let self_read = self.inner.read();
        if let Some(callback) = &self_read.routing_callback {
            let myself = *self;
            let callback = callback.clone();
            drop(self_read);
            if let Some(new) = callback(myself) {
                let mut self_write = self.inner.write_unchecked();
                match new {
                    NavigationTarget::Internal(p) => {
                        let history = history();
                        history.replace(p)
                    }
                    NavigationTarget::External(e) => return self_write.external(e),
                }
            }
        }

        self.inner.read().update_subscribers();

        None
    }

    pub(crate) fn internal_route(&self, route: &str) -> bool {
        (self.inner.read().internal_route)(route)
    }
}

pub struct GenericRouterContext<R> {
    inner: RouterContext,
    _marker: std::marker::PhantomData<R>,
}

impl<R> GenericRouterContext<R>
where
    R: Routable,
{
    /// Check whether there is a previous page to navigate back to.
    #[must_use]
    pub fn can_go_back(&self) -> bool {
        self.inner.can_go_back()
    }

    /// Check whether there is a future page to navigate forward to.
    #[must_use]
    pub fn can_go_forward(&self) -> bool {
        self.inner.can_go_forward()
    }

    /// Go back to the previous location.
    ///
    /// Will fail silently if there is no previous location to go to.
    pub fn go_back(&self) {
        self.inner.go_back();
    }

    /// Go back to the next location.
    ///
    /// Will fail silently if there is no next location to go to.
    pub fn go_forward(&self) {
        self.inner.go_forward();
    }

    /// Push a new location.
    ///
    /// The previous location will be available to go back to.
    pub fn push(
        &self,
        target: impl Into<NavigationTarget<R>>,
    ) -> Option<ExternalNavigationFailure> {
        self.inner.push(target.into())
    }

    /// Replace the current location.
    ///
    /// The previous location will **not** be available to go back to.
    pub fn replace(
        &self,
        target: impl Into<NavigationTarget<R>>,
    ) -> Option<ExternalNavigationFailure> {
        self.inner.replace(target.into())
    }

    /// The route that is currently active.
    pub fn current(&self) -> R
    where
        R: Clone,
    {
        self.inner.current()
    }

    /// The prefix that is currently active.
    pub fn prefix(&self) -> Option<String> {
        self.inner.prefix()
    }

    /// Clear any unresolved errors
    pub fn clear_error(&self) {
        self.inner.clear_error()
    }
}