Skip to content

Commit

Permalink
fix: run unmount logic for element children (closes #2832) (#2834)
Browse files Browse the repository at this point in the history
  • Loading branch information
gbj authored and chrisp60 committed Aug 15, 2024
1 parent 36df797 commit 88dee08
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tachys/src/html/element/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,12 +443,22 @@ impl<At, Ch, R: Renderer> Deref for ElementState<At, Ch, R> {

impl<At, Ch, R> Mountable<R> for ElementState<At, Ch, R>
where
Ch: Mountable<R>,
R: Renderer,
{
fn unmount(&mut self) {
if let Some(children) = self.children.as_mut() {
children.unmount_from_parent();
}
R::remove(self.el.as_ref());
}

fn unmount_from_parent(&mut self) {
if let Some(children) = self.children.as_mut() {
children.unmount_from_parent();
}
}

fn mount(&mut self, parent: &R::Element, marker: Option<&R::Node>) {
R::insert_node(parent, self.el.as_ref(), marker);
}
Expand Down
9 changes: 9 additions & 0 deletions tachys/src/view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,15 @@ pub trait Mountable<R: Renderer> {
/// Detaches the view from the DOM.
fn unmount(&mut self);

/// Detaches the view from the DOM, when it is a child of another element that is being
/// unmounted.
///
/// Most elements do not require any action here, but special view types that have additional
/// unmount logic do.
fn unmount_from_parent(&mut self) {
self.unmount();
}

/// Mounts a node to the interface.
fn mount(&mut self, parent: &R::Element, marker: Option<&R::Node>);

Expand Down
2 changes: 2 additions & 0 deletions tachys/src/view/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ macro_rules! render_primitive {
self.0.unmount()
}

fn unmount_from_parent(&mut self) {}

fn mount(
&mut self,
parent: &<R as Renderer>::Element,
Expand Down
10 changes: 10 additions & 0 deletions tachys/src/view/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ where
self.node.unmount()
}

fn unmount_from_parent(&mut self) {}

fn mount(
&mut self,
parent: &<R as Renderer>::Element,
Expand Down Expand Up @@ -232,6 +234,8 @@ impl<R: Renderer> Mountable<R> for StringState<R> {
self.node.unmount()
}

fn unmount_from_parent(&mut self) {}

fn mount(
&mut self,
parent: &<R as Renderer>::Element,
Expand Down Expand Up @@ -324,6 +328,8 @@ impl<R: Renderer> Mountable<R> for RcStrState<R> {
self.node.unmount()
}

fn unmount_from_parent(&mut self) {}

fn mount(
&mut self,
parent: &<R as Renderer>::Element,
Expand Down Expand Up @@ -427,6 +433,8 @@ impl<R: Renderer> Mountable<R> for ArcStrState<R> {
self.node.unmount()
}

fn unmount_from_parent(&mut self) {}

fn mount(
&mut self,
parent: &<R as Renderer>::Element,
Expand Down Expand Up @@ -530,6 +538,8 @@ impl<'a, R: Renderer> Mountable<R> for CowStrState<'a, R> {
self.node.unmount()
}

fn unmount_from_parent(&mut self) {}

fn mount(
&mut self,
parent: &<R as Renderer>::Element,
Expand Down

0 comments on commit 88dee08

Please sign in to comment.