chore: clean up the repo some

This commit is contained in:
Jonathan Kelley 2022-12-06 17:50:25 -08:00
parent 5a0ae67ccb
commit 5673017def
13 changed files with 30 additions and 27 deletions

View file

@ -10,7 +10,7 @@ fn main() {
let mut dom = VirtualDom::new(app); let mut dom = VirtualDom::new(app);
let _ = dom.rebuild(); let _ = dom.rebuild();
let output = dioxus_ssr::render_vdom(&dom); let output = dioxus_ssr::render(&dom);
println!("{}", output); println!("{}", output);
} }

View file

@ -2,14 +2,14 @@
//! It also proves that lifetimes work properly, especially when used with use_ref //! It also proves that lifetimes work properly, especially when used with use_ref
use dioxus::prelude::*; use dioxus::prelude::*;
use dioxus_ssr::config::Config;
fn main() { fn main() {
let mut vdom = VirtualDom::new(example); let mut vdom = VirtualDom::new(example);
_ = vdom.rebuild(); _ = vdom.rebuild();
let out = dioxus_ssr::render_vdom_cfg(&vdom, Config::default().newline(true).indent(true)); let mut renderer = dioxus_ssr::Renderer::new();
println!("{}", out); renderer.pretty = true;
renderer.render(&vdom);
} }
fn example(cx: Scope) -> Element { fn example(cx: Scope) -> Element {

View file

@ -24,7 +24,7 @@ macro_rules! use_callback {
) )
}; };
} }
pub fn use_callback<'a, T, R, F>(cx: &'a ScopeState, make: impl FnOnce() -> R) -> impl FnMut(T) + 'a pub fn use_callback<T, R, F>(cx: &ScopeState, make: impl FnOnce() -> R) -> impl FnMut(T) + '_
where where
R: FnMut(T) -> F + 'static, R: FnMut(T) -> F + 'static,
F: Future<Output = ()> + 'static, F: Future<Output = ()> + 'static,

View file

@ -51,7 +51,7 @@ where
// Clone in our cells // Clone in our cells
let values = state.values.clone(); let values = state.values.clone();
let _schedule_update = state.update.clone(); let schedule_update = state.update.clone();
let waker = state.waker.clone(); let waker = state.waker.clone();
// Cancel the current future // Cancel the current future
@ -66,10 +66,7 @@ where
// if there's a waker, we dont re-render the component. Instead we just progress that future // if there's a waker, we dont re-render the component. Instead we just progress that future
match waker.borrow().as_ref() { match waker.borrow().as_ref() {
Some(waker) => waker.wake_by_ref(), Some(waker) => waker.wake_by_ref(),
None => { None => schedule_update(),
// schedule_update()
}
} }
}))); })));
} }

View file

@ -105,7 +105,7 @@ pub fn Link<'a>(cx: Scope<'a, LinkProps<'a>>) -> Element {
} }
}; };
let route = use_route(&cx); let route = use_route(cx);
let url = route.url(); let url = route.url();
let path = url.path(); let path = url.path();
let active = path == cx.props.to; let active = path == cx.props.to;

View file

@ -32,7 +32,7 @@ pub struct RedirectProps<'a> {
/// ///
/// It will replace the current route rather than pushing the current one to the stack. /// It will replace the current route rather than pushing the current one to the stack.
pub fn Redirect<'a>(cx: Scope<'a, RedirectProps<'a>>) -> Element { pub fn Redirect<'a>(cx: Scope<'a, RedirectProps<'a>>) -> Element {
let router = use_router(&cx); let router = use_router(cx);
let immediate_redirect = cx.use_hook(|| { let immediate_redirect = cx.use_hook(|| {
if let Some(from) = cx.props.from { if let Some(from) = cx.props.from {

View file

@ -42,7 +42,7 @@ pub fn Route<'a>(cx: Scope<'a, RouteProps<'a>>) -> Element {
}); });
// submit our rout // submit our rout
router_root.register_total_route(route_context.total_route.clone(), cx.scope_id()); router_root.register_total_route(route_context.total_route, cx.scope_id());
}); });
log::trace!("Checking Route: {:?}", cx.props.to); log::trace!("Checking Route: {:?}", cx.props.to);

View file

@ -41,7 +41,7 @@ pub struct RouterProps<'a> {
pub fn Router<'a>(cx: Scope<'a, RouterProps<'a>>) -> Element { pub fn Router<'a>(cx: Scope<'a, RouterProps<'a>>) -> Element {
let svc = use_context_provider(cx, || { let svc = use_context_provider(cx, || {
RouterService::new( RouterService::new(
&cx, cx,
RouterCfg { RouterCfg {
base_url: cx.props.base_url.map(|s| s.to_string()), base_url: cx.props.base_url.map(|s| s.to_string()),
active_class: cx.props.active_class.map(|s| s.to_string()), active_class: cx.props.active_class.map(|s| s.to_string()),

View file

@ -60,7 +60,7 @@ impl Component {
match self match self
.fields .fields
.iter() .iter()
.find(|f| f.name.to_string() == "key") .find(|f| f.name == "key")
.map(|f| &f.content) .map(|f| &f.content)
{ {
Some(ContentField::Formatted(fmt)) => Some(fmt), Some(ContentField::Formatted(fmt)) => Some(fmt),

View file

@ -21,10 +21,7 @@ pub struct IfmtInput {
impl IfmtInput { impl IfmtInput {
pub fn is_static(&self) -> bool { pub fn is_static(&self) -> bool {
match self.segments.as_slice() { matches!(self.segments.as_slice(), &[Segment::Literal(_)])
&[Segment::Literal(_)] => true,
_ => false,
}
} }
} }

View file

@ -180,10 +180,10 @@ impl<'a> DynamicContext<'a> {
// [0, 2] // [0, 2]
// [0, 2, 1] // [0, 2, 1]
let static_attrs = el.attributes.iter().filter_map(|attr| match &attr.attr { let static_attrs = el.attributes.iter().map(|attr| match &attr.attr {
ElementAttr::AttrText { name, value } if value.is_static() => { ElementAttr::AttrText { name, value } if value.is_static() => {
let value = value.source.as_ref().unwrap(); let value = value.source.as_ref().unwrap();
Some(quote! { quote! {
::dioxus::core::TemplateAttribute::Static { ::dioxus::core::TemplateAttribute::Static {
name: dioxus_elements::#el_name::#name.0, name: dioxus_elements::#el_name::#name.0,
namespace: dioxus_elements::#el_name::#name.1, namespace: dioxus_elements::#el_name::#name.1,
@ -192,12 +192,12 @@ impl<'a> DynamicContext<'a> {
// todo: we don't diff these so we never apply the volatile flag // todo: we don't diff these so we never apply the volatile flag
// volatile: dioxus_elements::#el_name::#name.2, // volatile: dioxus_elements::#el_name::#name.2,
} }
}) }
} }
ElementAttr::CustomAttrText { name, value } if value.is_static() => { ElementAttr::CustomAttrText { name, value } if value.is_static() => {
let value = value.source.as_ref().unwrap(); let value = value.source.as_ref().unwrap();
Some(quote! { quote! {
::dioxus::core::TemplateAttribute::Static { ::dioxus::core::TemplateAttribute::Static {
name: dioxus_elements::#el_name::#name.0, name: dioxus_elements::#el_name::#name.0,
namespace: dioxus_elements::#el_name::#name.1, namespace: dioxus_elements::#el_name::#name.1,
@ -206,7 +206,7 @@ impl<'a> DynamicContext<'a> {
// todo: we don't diff these so we never apply the volatile flag // todo: we don't diff these so we never apply the volatile flag
// volatile: dioxus_elements::#el_name::#name.2, // volatile: dioxus_elements::#el_name::#name.2,
} }
}) }
} }
ElementAttr::AttrExpression { .. } ElementAttr::AttrExpression { .. }
@ -217,7 +217,7 @@ impl<'a> DynamicContext<'a> {
let ct = self.dynamic_attributes.len(); let ct = self.dynamic_attributes.len();
self.dynamic_attributes.push(attr); self.dynamic_attributes.push(attr);
self.attr_paths.push(self.current_path.clone()); self.attr_paths.push(self.current_path.clone());
Some(quote! { ::dioxus::core::TemplateAttribute::Dynamic { id: #ct } }) quote! { ::dioxus::core::TemplateAttribute::Dynamic { id: #ct } }
} }
}); });

View file

@ -141,7 +141,7 @@ impl ToTokens for BodyNode {
pat, expr, body, .. pat, expr, body, ..
} = exp; } = exp;
let renderer = TemplateRenderer { roots: &body }; let renderer = TemplateRenderer { roots: body };
tokens.append_all(quote! { tokens.append_all(quote! {
__cx.make_node( __cx.make_node(

View file

@ -7,7 +7,7 @@ pub mod template;
use dioxus_core::{Element, LazyNodes, Scope, VirtualDom}; use dioxus_core::{Element, LazyNodes, Scope, VirtualDom};
use std::cell::Cell; use std::cell::Cell;
use crate::renderer::Renderer; pub use crate::renderer::Renderer;
/// A convenience function to render an `rsx!` call to a string /// A convenience function to render an `rsx!` call to a string
/// ///
@ -44,3 +44,12 @@ pub fn render_lazy(f: LazyNodes<'_, '_>) -> String {
pub fn render(dom: &VirtualDom) -> String { pub fn render(dom: &VirtualDom) -> String {
Renderer::new().render(dom) Renderer::new().render(dom)
} }
/// A convenience function to pre-render an existing VirtualDom to a string
///
/// We generally recommend creating a new `Renderer` to take advantage of template caching.
pub fn pre_render(dom: &VirtualDom) -> String {
let mut renderer = Renderer::new();
renderer.pre_render = true;
renderer.render(dom)
}