Skip to content

Commit

Permalink
Use unmount and replace_with on Mountable to avoid DynAnchor path
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejhirsz committed Mar 28, 2023
1 parent 8529c64 commit 8e4072e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 16 deletions.
19 changes: 18 additions & 1 deletion crates/kobold/src/branching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
//! }
//! ```
use wasm_bindgen::JsValue;
use web_sys::Node;

use crate::dom::{self, Anchor, DynAnchor};
Expand Down Expand Up @@ -152,6 +153,22 @@ macro_rules! branch {
)*
}
}

fn replace_with(&self, new: &JsValue) {
match self {
$(
$name::$var(p) => p.replace_with(new),
)*
}
}

fn unmount(&self) {
match self {
$(
$name::$var(p) => p.unmount(),
)*
}
}
}

};
Expand Down Expand Up @@ -207,7 +224,7 @@ impl<T: View> View for Option<T> {
(html, old) => {
let new = html.build();

old.anchor().replace_with(new.js());
old.replace_with(new.js());

*old = new;
}
Expand Down
23 changes: 16 additions & 7 deletions crates/kobold/src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use std::ops::Deref;
use web_sys::Node;

use crate::attribute::AttributeView;
use crate::value::IntoText;
use crate::dom::TextContent;
use crate::value::{IntoText, Value};
use crate::{Mountable, View};

/// This is a wrapper around a `view` that will prevent updates to it, unless
Expand Down Expand Up @@ -238,30 +239,38 @@ macro_rules! impl_no_diff {

impl<T> View for $name<T>
where
T: IntoText + Copy,
T: Value<TextContent> + IntoText + Copy,
{
type Product = Node;

fn build(self) -> Self::Product {
fn build(self) -> Node {
self.into_text()
}

fn update(self, _: &mut Self::Product) {}
fn update(self, node: &mut Node) {
if $update {
self.0.set_prop(TextContent, node);
}
}
}

impl<T, P> AttributeView<P> for $name<T>
where
T: AttributeView<P>,
T: Value<P>,
{
type Product = ();

fn build(self) {}

fn build_in(self, prop: P, node: &Node) {
self.0.build_in(prop, node);
self.0.set_prop(prop, node);
}

fn update_in(self, _: P, _: &Node, _: &mut ()) {}
fn update_in(self, prop: P, node: &Node, _: &mut ()) {
if $update {
self.0.set_prop(prop, node);
}
}
}

impl<T> Diff for $name<T>
Expand Down
8 changes: 8 additions & 0 deletions crates/kobold/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,14 @@ pub trait Mountable: 'static {
fn js(&self) -> &JsValue {
self.anchor().as_ref()
}

fn unmount(&self) {
self.anchor().unmount();
}

fn replace_with(&self, new: &JsValue) {
self.anchor().replace_with(new);
}
}

/// Start the Kobold app by mounting given [`View`](View) in the document `body`.
Expand Down
4 changes: 2 additions & 2 deletions crates/kobold/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use web_sys::Node;

use crate::dom::{Anchor, Fragment, FragmentBuilder};
use crate::dom::{Fragment, FragmentBuilder};
use crate::{Mountable, View};

/// Wrapper type that implements `View` for iterators. Use the [`list`](ListIteratorExt::list)
Expand Down Expand Up @@ -82,7 +82,7 @@ where

if p.visible > updated {
for old in p.list[updated..p.visible].iter() {
old.anchor().unmount();
old.unmount();
}
p.visible = updated;
} else {
Expand Down
5 changes: 1 addition & 4 deletions crates/kobold/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,7 @@ impl View for String {
fn build(self) -> Self::Product {
let node = self.as_str().into_text();

TextProduct {
memo: self,
node,
}
TextProduct { memo: self, node }
}

fn update(self, p: &mut Self::Product) {
Expand Down
2 changes: 0 additions & 2 deletions examples/todomvc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ fn EntryInput(state: &Hook<State>) -> impl View + '_ {
mod test {
use super::*;


#[component]
fn ToggleAll(active_count: usize, state: &Hook<State>) -> impl View + '_ {
bind! { state:
Expand All @@ -98,7 +97,6 @@ mod test {
<label for="toggle-all" />
}
}

}

#[component]
Expand Down

0 comments on commit 8e4072e

Please sign in to comment.