Skip to content

Commit

Permalink
get_children utility
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechu10 committed Apr 20, 2021
1 parent 756fa3c commit e269592
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
1 change: 1 addition & 0 deletions maple-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ features = [
"Event",
"HtmlElement",
"HtmlInputElement",
"HtmlCollection",
"Node",
"Text",
"Window",
Expand Down
41 changes: 26 additions & 15 deletions maple-core/src/generic_node/dom_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
use std::cell::RefCell;

use ref_cast::RefCast;
use wasm_bindgen::{prelude::*, JsCast};
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::{Element, Event, Node, Text};

use crate::generic_node::{EventListener, GenericNode};
Expand Down Expand Up @@ -174,10 +175,7 @@ pub fn render(template_result: impl FnOnce() -> TemplateResult<DomNode>) {
/// For rendering under the `<body>` tag, use [`render`] instead.
///
/// _This API requires the following crate features to be activated: `dom`_
pub fn render_to(
template_result: impl FnOnce() -> TemplateResult<DomNode>,
parent: &web_sys::Node,
) {
pub fn render_to(template_result: impl FnOnce() -> TemplateResult<DomNode>, parent: &Node) {
let scope = create_root(|| {
for node in template_result() {
parent.append_child(&node.inner_element()).unwrap();
Expand All @@ -191,8 +189,8 @@ pub fn render_to(
GLOBAL_SCOPES.with(|global_scopes| global_scopes.borrow_mut().push(scope));
}

/// Render a [`TemplateResult`] under a `parent` node by reusing existing nodes (client side hydration).
/// Alias for [`hydrate_to`] with `parent` being the `<body>` tag.
/// Render a [`TemplateResult`] under a `parent` node by reusing existing nodes (client side
/// hydration). Alias for [`hydrate_to`] with `parent` being the `<body>` tag.
///
/// For rendering without hydration, use [`render`] instead.
///
Expand All @@ -204,18 +202,31 @@ pub fn hydrate(template_result: impl FnOnce() -> TemplateResult<DomNode>) {
hydrate_to(template_result, &document.body().unwrap());
}

/// Render a [`TemplateResult`] under a `parent` node by reusing existing nodes (client side hydration).
/// For rendering under the `<body>` tag, use [`hydrate_to`] instead.
/// Gets the children of an [`Element`] by collecting them into a [`Vec`]. Note that the returned
/// value is **NOT** live.
fn get_children(parent: &Element) -> Vec<Element> {
let children = parent.children();
let children_count = children.length();

let mut vec = Vec::new();
vec.reserve(children_count as usize);

for i in 0..children.length() {
vec.push(children.get_with_index(i).unwrap())
}

vec
}

/// Render a [`TemplateResult`] under a `parent` node by reusing existing nodes (client side
/// hydration). For rendering under the `<body>` tag, use [`hydrate_to`] instead.
///
/// For rendering without hydration, use [`render`] instead.
///
/// _This API requires the following crate features to be activated: `dom`_
pub fn hydrate_to(
template_result: impl FnOnce() -> TemplateResult<DomNode>,
parent: &web_sys::Node,
) {
while let Some(child) = parent.first_child() {
child.unchecked_into::<Element>().remove();
pub fn hydrate_to(template_result: impl FnOnce() -> TemplateResult<DomNode>, parent: &Node) {
for child in get_children(parent.unchecked_ref()) {
child.remove();
}

let scope = create_root(|| {
Expand Down
4 changes: 2 additions & 2 deletions maple-core/src/reactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ pub use effect::*;
pub use motion::*;
pub use signal::*;

/// Creates a new reactive root / scope. Generally, you won't need this method as it is called automatically
/// in [`render`](crate::generic_node::render).
/// Creates a new reactive root / scope. Generally, you won't need this method as it is called
/// automatically in [`render`](crate::generic_node::render).
///
/// # Example
/// ```
Expand Down
6 changes: 4 additions & 2 deletions maple-core/src/reactive/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ impl Drop for Running {
}

/// Owns the effects created in the current reactive scope.
/// The effects are dropped and the cleanup callbacks are called when the [`ReactiveScope`] is dropped.
/// The effects are dropped and the cleanup callbacks are called when the [`ReactiveScope`] is
/// dropped.
#[derive(Default)]
pub struct ReactiveScope {
effects: Vec<Rc<RefCell<Option<Running>>>>,
Expand Down Expand Up @@ -191,7 +192,8 @@ pub fn create_effect_initial<R: 'static>(
// Destroy old effects before new ones run.
let old_scope = mem::replace(
&mut running.borrow_mut().as_mut().unwrap().scope,
ReactiveScope::new(), /* placeholder until an actual ReactiveScope is created */
ReactiveScope::new(), /* placeholder until an actual ReactiveScope
* is created */
);
drop(old_scope);

Expand Down

0 comments on commit e269592

Please sign in to comment.