fix detach/attach

This commit is contained in:
Michael Sippel 2024-07-21 17:46:58 +02:00
parent 879478942a
commit 2261c22f3a
Signed by: senvas
GPG key ID: F96CF119C34B64A6

View file

@ -38,8 +38,8 @@ where
} }
pub fn set_view(&self, view: Option<Arc<V>>) { pub fn set_view(&self, view: Option<Arc<V>>) {
self.update(); let mut v = self.view.write().unwrap();
*self.view.write().unwrap() = view.clone(); *v = view.clone();
self.cast.write().unwrap().reset(view); self.cast.write().unwrap().reset(view);
} }
@ -101,14 +101,37 @@ impl<V: View + ?Sized + 'static> ViewPort<V>
where V::Msg: Clone where V::Msg: Clone
{ {
// make the view of `other_port` accessible from `self` // make the view of `other_port` accessible from `self`
pub fn attach_to(&self, other_port: OuterViewPort<V>) { pub fn attach_to(&mut self, other_port: OuterViewPort<V>) -> Arc<RwLock<InnerViewPort<V>>> {
self.attach_to_port(other_port.0); self.attach_to_port(other_port.0)
} }
pub fn attach_to_port(&self, other_port: ViewPort<V>) {
self.set_view( other_port.view.read().unwrap().clone() ); pub fn attach_to_port(&mut self, other_port: ViewPort<V>) -> Arc<RwLock<InnerViewPort<V>>> {
other_port.add_observer( self.cast.clone() ); /* 1 . replace broad cast to remove it as observer
* from other port when re-attaching a port
*/
let keepalive = Arc::new(RwLock::new( self.inner() ));
other_port.update(); // todo: required?
other_port.add_observer( keepalive.clone() );
self.update_hooks.write().unwrap().clear(); self.update_hooks.write().unwrap().clear();
self.add_update_hook( Arc::new(other_port) ); self.add_update_hook( Arc::new(other_port) );
self.update();
keepalive
}
pub fn detach(&self) {
self.update_hooks.write().unwrap().clear();
self.set_view(None);
}
}
impl<V: View + ?Sized + 'static> Observer<V> for InnerViewPort<V> where V::Msg: Clone + Send + Sync + 'static {
fn reset(&mut self, new_view: Option<Arc<V>>) {
self.set_view(new_view);
}
fn notify(&mut self, msg: &V::Msg) {
self.0.cast.write().unwrap().notify(msg);
} }
} }
@ -125,7 +148,6 @@ where
for hook in v { for hook in v {
hook.update(); hook.update();
} }
self.cast.read().unwrap().update(); self.cast.read().unwrap().update();
} }
} }