Skip to content

Commit

Permalink
Move TemplateResult into sub-module
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechu10 committed Apr 6, 2021
1 parent 9466293 commit c9e3dd4
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 48 deletions.
6 changes: 5 additions & 1 deletion maple-core-macro/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ impl ToTokens for Component {
args,
} = self;

let quoted = quote! { ::maple_core::reactive::untrack(|| ::maple_core::TemplateResult::inner_element(&#path(#args))) };
let quoted = quote! {
::maple_core::reactive::untrack(||
::std::clone::Clone::clone(::maple_core::template_result::TemplateResult::inner_node(&#path(#args)))
)
};

tokens.extend(quoted);
}
Expand Down
2 changes: 1 addition & 1 deletion maple-core-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub fn template(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as HtmlTree);

let quoted = quote! {
::maple_core::TemplateResult::new(#input)
::maple_core::template_result::TemplateResult::new(#input)
};

TokenStream::from(quoted)
Expand Down
33 changes: 18 additions & 15 deletions maple-core/src/flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ where
if iterable.get().is_empty() {
for (_, (owner, _value, template, _i)) in templates.borrow_mut().drain() {
drop(owner); // destroy owner
template.node.remove_self();
template.inner_node().remove_self();
}
return;
}
Expand Down Expand Up @@ -114,7 +114,7 @@ where
}

for node in excess_nodes {
node.1 .0.node.remove_self();
node.1 .0.inner_node().remove_self();
}
}

Expand Down Expand Up @@ -161,13 +161,13 @@ where
if let Some(next_node) = templates.get(&key_fn(next_item)) {
next_node
.2
.node
.insert_sibling_before(&new_template.unwrap().node);
.inner_node()
.insert_sibling_before(new_template.unwrap().inner_node());
} else {
marker.insert_sibling_before(&new_template.unwrap().node);
marker.insert_sibling_before(new_template.unwrap().inner_node());
}
} else {
marker.insert_sibling_before(&new_template.unwrap().node);
marker.insert_sibling_before(new_template.unwrap().inner_node());
}
} else if match previous_value {
Some(prev) => prev.index,
Expand All @@ -177,12 +177,12 @@ where
// Location changed, move from old location to new location
// Node was moved in the DOM. Move node to new index.

let node = templates.borrow().get(&key).unwrap().2.node.clone();
let node = templates.borrow().get(&key).unwrap().2.inner_node().clone();

if let Some(next_item) = iterable.get().get(i + 1) {
let templates = templates.borrow();
let next_node = templates.get(&key_fn(next_item)).unwrap();
next_node.2.node.insert_sibling_before(&node); // Move to before next node
next_node.2.inner_node().insert_sibling_before(&node); // Move to before next node
} else {
marker.insert_sibling_before(&node); // Move to end.
}
Expand Down Expand Up @@ -211,8 +211,8 @@ where
(owner, item.clone(), new_template.clone().unwrap(), i),
);

let parent = old_node.node.parent_node().unwrap();
parent.replace_child(&new_template.unwrap().node, &old_node.node);
let parent = old_node.inner_node().parent_node().unwrap();
parent.replace_child(new_template.unwrap().inner_node(), old_node.inner_node());
}
}
}
Expand Down Expand Up @@ -275,7 +275,7 @@ where
if props.iterable.get().is_empty() {
for (owner, template) in templates.borrow_mut().drain(..) {
drop(owner); // destroy owner
template.node.remove_self();
template.inner_node().remove_self();
}
return;
}
Expand Down Expand Up @@ -304,16 +304,19 @@ where
(owner, new_template.as_ref().unwrap().clone()),
);

let parent = old_node.1.node.parent_node().unwrap();
parent.replace_child(&new_template.unwrap().node, &old_node.1.node);
let parent = old_node.1.inner_node().parent_node().unwrap();
parent.replace_child(
new_template.unwrap().inner_node(),
old_node.1.inner_node(),
);
} else {
debug_assert!(templates.borrow().len() == i, "pushing new value scenario");

templates
.borrow_mut()
.push((owner, new_template.as_ref().unwrap().clone()));

marker.insert_sibling_before(&new_template.unwrap().node);
marker.insert_sibling_before(&new_template.unwrap().inner_node());
}
}
}
Expand All @@ -323,7 +326,7 @@ where
let excess_nodes = templates.drain(props.iterable.get().len()..);

for node in excess_nodes {
node.1.node.remove_self();
node.1.inner_node().remove_self();
}
}

Expand Down
38 changes: 9 additions & 29 deletions maple-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#![deny(clippy::trait_duplication_in_bounds)]
#![deny(clippy::type_repetition_in_bounds)]

use generic_node::GenericNode;
pub use maple_core_macro::template;

pub mod easing;
Expand All @@ -25,36 +24,17 @@ pub mod macros;
pub mod noderef;
pub mod reactive;
pub mod render;
pub mod template_result;
pub mod utils;

/// Result of the [`template`] macro.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TemplateResult<G: GenericNode> {
node: G,
}

impl<G: GenericNode> TemplateResult<G> {
/// Create a new [`TemplateResult`] from a [`GenericNode`].
pub fn new(node: G) -> Self {
Self { node }
}

/// Create a new [`TemplateResult`] with a blank comment node
pub fn empty() -> Self {
Self::new(G::marker())
}

pub fn inner_element(&self) -> G {
self.node.clone()
}
}

/// Render a [`TemplateResult`] into the DOM.
/// Alias for [`render_to`] with `parent` being the `<body>` tag.
///
/// _This API requires the following crate features to be activated: `dom`_
#[cfg(feature = "dom")]
pub fn render(template_result: impl FnOnce() -> TemplateResult<generic_node::DomNode>) {
pub fn render(
template_result: impl FnOnce() -> template_result::TemplateResult<generic_node::DomNode>,
) {
let window = web_sys::window().unwrap();
let document = window.document().unwrap();

Expand All @@ -67,12 +47,12 @@ pub fn render(template_result: impl FnOnce() -> TemplateResult<generic_node::Dom
/// _This API requires the following crate features to be activated: `dom`_
#[cfg(feature = "dom")]
pub fn render_to(
template_result: impl FnOnce() -> TemplateResult<generic_node::DomNode>,
template_result: impl FnOnce() -> template_result::TemplateResult<generic_node::DomNode>,
parent: &web_sys::Node,
) {
let owner = reactive::create_root(|| {
parent
.append_child(&template_result().node.inner_element())
.append_child(&template_result().inner_node().inner_element())
.unwrap();
});

Expand All @@ -88,11 +68,11 @@ pub fn render_to(
/// _This API requires the following crate features to be activated: `ssr`_
#[cfg(feature = "ssr")]
pub fn render_to_string(
template_result: impl FnOnce() -> TemplateResult<generic_node::SsrNode>,
template_result: impl FnOnce() -> template_result::TemplateResult<generic_node::SsrNode>,
) -> String {
let mut ret = None;
let _owner =
reactive::create_root(|| ret = Some(format!("{}", template_result().inner_element())));
reactive::create_root(|| ret = Some(format!("{}", template_result().inner_node())));

ret.unwrap()
}
Expand All @@ -116,7 +96,7 @@ pub mod prelude {
pub use crate::render::Render;
#[cfg(feature = "ssr")]
pub use crate::render_to_string;
pub use crate::TemplateResult;
pub use crate::template_result::TemplateResult;
#[cfg(feature = "dom")]
pub use crate::{render, render_to};
}
4 changes: 2 additions & 2 deletions maple-core/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::fmt;

use crate::generic_node::GenericNode;
use crate::TemplateResult;
use crate::template_result::TemplateResult;

/// Trait for describing how something should be rendered into DOM nodes.
pub trait Render<G: GenericNode> {
Expand Down Expand Up @@ -39,6 +39,6 @@ impl<T: fmt::Display + ?Sized, G: GenericNode> Render<G> for T {

impl<G: GenericNode> Render<G> for TemplateResult<G> {
fn render(&self) -> G {
self.node.clone()
self.inner_node().clone()
}
}
23 changes: 23 additions & 0 deletions maple-core/src/template_result.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::generic_node::GenericNode;

/// Result of the [`template`] macro.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TemplateResult<G: GenericNode> {
node: G,
}

impl<G: GenericNode> TemplateResult<G> {
/// Create a new [`TemplateResult`] from a [`GenericNode`].
pub fn new(node: G) -> Self {
Self { node }
}

/// Create a new [`TemplateResult`] with a blank comment node
pub fn empty() -> Self {
Self::new(G::marker())
}

pub fn inner_node(&self) -> &G {
&self.node
}
}

0 comments on commit c9e3dd4

Please sign in to comment.