Skip to main content

topcoat_runtime/
router.rs

1#[cfg(feature = "router")]
2use topcoat_router::RouterBuilder;
3
4#[cfg(feature = "router")]
5use crate::PageRerunRoute;
6
7/// The app context value marking a router as set up for the browser
8/// runtime.
9///
10/// [`RouterBuilderRuntimeExt::runtime`] registers it, and the script
11/// component checks for it, so a page that includes the runtime script on
12/// a router missing the runtime's routes fails loudly instead of breaking
13/// in the browser.
14#[derive(Debug, Clone, Copy)]
15pub struct RuntimeSetup;
16
17/// Sets up the browser runtime on a [`RouterBuilder`].
18#[cfg(feature = "router")]
19pub trait RouterBuilderRuntimeExt {
20    /// Mounts the routes the browser runtime talks to on its own, such as
21    /// the route a page re-runs through, and marks the router as set up
22    /// with [`RuntimeSetup`].
23    ///
24    /// Every application using the runtime calls this once. The routes
25    /// behind the application's own procedures and shards are registered
26    /// separately, by discovery or by hand.
27    #[must_use]
28    fn runtime(self) -> Self;
29}
30
31#[cfg(feature = "router")]
32impl RouterBuilderRuntimeExt for RouterBuilder {
33    fn runtime(self) -> Self {
34        self.route(PageRerunRoute::root())
35            .route(PageRerunRoute::nested())
36            .app_context(RuntimeSetup)
37    }
38}