First pass of method to generate routelist

This commit is contained in:
benwis 2023-01-06 14:08:45 -08:00
parent aef589cd24
commit 63b1837315
6 changed files with 71 additions and 8 deletions

View file

@ -1,5 +1,6 @@
use cfg_if::cfg_if;
use leptos::*;
use leptos_router::generate_route_list;
mod todo;
// boilerplate to run in different modes
@ -28,6 +29,10 @@ cfg_if! {
let conf = get_configuration(Some("Cargo.toml")).await.unwrap();
let addr = conf.leptos_options.site_address.clone();
println!("BEFFOORE");
let routes = generate_route_list(|cx| view! { cx, <TodoApp/> });
println!("HIIIIIIIIIIII");
println!("Routes: {:#?}", routes);
HttpServer::new(move || {
let leptos_options = &conf.leptos_options;

View file

@ -38,9 +38,11 @@ cfg_if! {
pub async fn get_todos(cx: Scope) -> Result<Vec<Todo>, ServerFnError> {
// this is just an example of how to access server context injected in the handlers
let req =
use_context::<actix_web::HttpRequest>(cx).expect("couldn't get HttpRequest from context");
println!("req.path = {:?}", req.path());
use_context::<actix_web::HttpRequest>(cx);
if let Some(req) = req{
println!("req.path = {:#?}", req.path());
}
use futures::TryStreamExt;
let mut conn = db().await?;
@ -103,6 +105,10 @@ pub fn TodoApp(cx: Scope) -> impl IntoView {
cx,
<Todos/>
}/>
<Route path="potato" view=|cx| view! {
cx,
<Todos/>
}/>
</Routes>
</main>
</Router>

View file

@ -11,8 +11,8 @@ use wasm_bindgen::JsCast;
use leptos_reactive::use_transition;
use crate::{
create_location, matching::resolve_path, History, Location, LocationChange, RouteContext,
RouterIntegrationContext, State,
create_location, matching::resolve_path, Branch, History, Location, LocationChange,
RouteContext, RouterIntegrationContext, State,
};
#[cfg(not(feature = "ssr"))]
@ -49,6 +49,7 @@ pub struct RouterContext {
pub(crate) struct RouterContextInner {
pub location: Location,
pub base: RouteContext,
pub possible_routes: RefCell<Option<Vec<Branch>>>,
base_path: String,
history: Box<dyn History>,
cx: Scope,
@ -104,7 +105,7 @@ impl RouterContext {
value: base_path.to_string(),
replace: true,
scroll: false,
state: State(None)
state: State(None),
});
}
}
@ -153,6 +154,7 @@ impl RouterContext {
referrers,
state,
set_state,
possible_routes: Default::default(),
});
// handle all click events on anchor tags
@ -175,6 +177,15 @@ impl RouterContext {
pub fn base(&self) -> RouteContext {
self.inner.base.clone()
}
/// A list of all possible routes this router can match.
pub fn possible_branches(&self) -> Vec<Branch> {
self.inner
.possible_routes
.borrow()
.clone()
.unwrap_or_default()
}
}
impl RouterContextInner {

View file

@ -12,7 +12,7 @@ use crate::{
expand_optionals, get_route_matches, join_paths, Branch, Matcher, RouteDefinition,
RouteMatch,
},
RouteContext, RouterContext,
PossibleBranchContext, RouteContext, RouterContext,
};
/// Contains route definitions and manages the actual routing process.
@ -42,6 +42,7 @@ pub fn Routes(
})
.cloned()
.collect::<Vec<_>>();
create_branches(
&children,
&base.unwrap_or_default(),
@ -49,6 +50,10 @@ pub fn Routes(
&mut branches,
);
if let Some(context) = use_context::<PossibleBranchContext>(cx) {
*context.0.borrow_mut() = branches.clone();
}
// whenever path changes, update matches
let matches = create_memo(cx, {
let router = router.clone();

View file

@ -0,0 +1,34 @@
use leptos::*;
use std::{cell::RefCell, rc::Rc};
use crate::{Branch, RouterIntegrationContext, ServerIntegration};
/// Context to contain all possible routes.
#[derive(Clone, Default, Debug)]
pub struct PossibleBranchContext(pub(crate) Rc<RefCell<Vec<Branch>>>);
/// Generates a list of all routes this application could possibly serve.
#[cfg(feature = "ssr")]
pub fn generate_route_list<IV>(app_fn: impl FnOnce(Scope) -> IV + 'static) -> Vec<String>
where
IV: IntoView + 'static,
{
let runtime = create_runtime();
run_scope(runtime, move |cx| {
let integration = ServerIntegration {
path: "http://leptos.rs/".to_string(),
};
provide_context(cx, RouterIntegrationContext::new(integration));
let branches = PossibleBranchContext::default();
provide_context(cx, branches.clone());
let _ = app_fn(cx).into_view(cx);
let branches = branches.0.borrow();
branches
.iter()
.flat_map(|branch| branch.routes.last().map(|route| route.pattern.clone()))
.collect()
})
}

View file

@ -184,12 +184,14 @@
#![cfg_attr(not(feature = "stable"), feature(type_name_of_val))]
mod components;
mod extract_routes;
mod history;
mod hooks;
#[doc(hidden)]
pub mod matching;
pub use components::*;
pub use extract_routes::*;
pub use history::*;
pub use hooks::*;
pub use matching::*;