From bb846afc7c0402978b76ea553e2b27b239fe527f Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Thu, 6 Jun 2024 15:21:52 +0200 Subject: [PATCH] ReprTree: add detach functions & attach_to helpers for Arc --- lib-nested-core/src/repr_tree/mod.rs | 70 +++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 7 deletions(-) diff --git a/lib-nested-core/src/repr_tree/mod.rs b/lib-nested-core/src/repr_tree/mod.rs index 13c3daa..28488e5 100644 --- a/lib-nested-core/src/repr_tree/mod.rs +++ b/lib-nested-core/src/repr_tree/mod.rs @@ -15,7 +15,7 @@ use { ViewPort, OuterViewPort, AnyViewPort, AnyInnerViewPort, AnyOuterViewPort, port::UpdateTask, - View, + View, Observer, singleton::*, sequence::*, list::* @@ -85,6 +85,20 @@ impl ReprLeaf { } } + pub fn detach(&mut self) + where V: View + ?Sized + 'static, + V::Msg: Clone + { + let ip = self.in_port.clone() + .downcast::().ok() + .unwrap(); + + ip.0.update_hooks.write().unwrap().clear(); + ip.set_view(None) + .write().unwrap() + .reset(None); + } + pub fn attach_to(&mut self, src_port: OuterViewPort) where V: View + ?Sized + 'static, V::Msg: Clone @@ -109,7 +123,6 @@ impl ReprLeaf { pub fn from_vec_buffer( buffer: VecBuffer ) -> Self where T: Clone + Send + Sync + 'static { - eprintln!("ReprLeaf from vec buffer (LEN ={})", buffer.len()); let in_port = ViewPort::< dyn ListView >::new(); ReprLeaf { keepalive: Some(buffer.attach_to(in_port.outer())), @@ -179,10 +192,7 @@ impl ReprLeaf { eprintln!("LEN = {}", data_arc.read().unwrap().len()); self.data = Some(data_arc.clone() as Arc); - let buf = VecBuffer { - data: data_arc, - port: vec_port.inner() - }; + let buf = VecBuffer::with_data_arc_port(data_arc, vec_port.inner()); self.keepalive = Some(buf.attach_to( self.in_port.0.clone() .downcast::< dyn ListView >() @@ -195,7 +205,6 @@ impl ReprLeaf { } } - pub fn get_port(&self) -> Option> where V: View + ?Sized + 'static, V::Msg: Clone @@ -314,6 +323,38 @@ impl ReprTree { } } + pub fn attach_to( + &mut self, + src_port: OuterViewPort + ) + where V: View + ?Sized + 'static, + V::Msg: Clone + { + if let Some(leaf) = self.leaf.as_mut() { + leaf.attach_to(src_port); + } else { + eprintln!("cant attach branch without leaf"); + } + } + + pub fn detach(&mut self, ctx: &Arc>) { + if let Some(leaf) = self.leaf.as_mut() { + if self.type_tag == Context::parse(&ctx, "Char") { + leaf.detach::>(); + } + if self.type_tag == Context::parse(&ctx, "") { + leaf.detach::>(); + } + if self.type_tag == Context::parse(&ctx, "") { + leaf.detach::>(); + } + } + + for (t,b) in self.branches.iter_mut() { + b.write().unwrap().detach(&ctx); + } + } + pub fn insert_leaf( &mut self, mut type_ladder: impl Iterator, @@ -413,6 +454,10 @@ impl ReprTree { self.get_port::>().expect("no sequence-view available") } + pub fn view_list(&self) -> OuterViewPort> { + self.get_port::>().expect("no list-view available") + } + pub fn view_char(&self) -> OuterViewPort> { self.get_port::>().expect("no char-view available") } @@ -442,6 +487,9 @@ pub trait ReprTreeExt { fn create_branch(&mut self, rung: impl Into); fn descend(&self, target_type: impl Into) -> Option>>; + fn get_port(&self) -> Option> where V::Msg: Clone; + fn attach_leaf_to(&self, t: impl Into, v: OuterViewPort) where V::Msg: Clone; + fn view_char(&self) -> OuterViewPort>; fn view_u8(&self) -> OuterViewPort>; fn view_u64(&self) -> OuterViewPort>; @@ -478,6 +526,14 @@ impl ReprTreeExt for Arc> { } } + fn get_port(&self) -> Option> where V::Msg: Clone { + self.read().unwrap().get_port::() + } + + fn attach_leaf_to(&self, type_ladder: impl Into, v: OuterViewPort) where V::Msg: Clone { + self.write().unwrap().attach_leaf_to::(type_ladder.into().get_lnf_vec().into_iter(), v) + } + fn descend(&self, target_type: impl Into) -> Option>> { ReprTree::descend( self, target_type ) }