diff --git a/src/buffer/singleton.rs b/src/buffer/singleton.rs index c62d289..661b0b5 100644 --- a/src/buffer/singleton.rs +++ b/src/buffer/singleton.rs @@ -1,6 +1,7 @@ use { crate::{ view::{ + Observer, InnerViewPort, OuterViewPort, View, ViewPort, singleton::SingletonView }, @@ -14,18 +15,18 @@ use { //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> -pub struct SingletonBufferView(Arc>); +pub struct SingletonBufferView(pub Arc>); impl View for SingletonBufferView where - T: Clone + Send + Sync + 'static, + T: Clone + Send + Sync + 'static { type Msg = (); } impl SingletonView for SingletonBufferView where - T: Clone + Send + Sync + 'static, + T: Clone + Send + Sync + 'static { type Item = T; @@ -39,15 +40,39 @@ where #[derive(Clone)] pub struct SingletonBuffer where - T: Clone + Send + Sync + 'static, + T: Clone + Send + Sync + 'static { - value: Arc>, - port: InnerViewPort> + pub value: Arc>, + pub port: InnerViewPort> +} + +pub struct SingletonBufferTarget +where + T: Clone + Send + Sync + 'static +{ + buffer: SingletonBuffer, + src_view: Option>> +} + +impl Observer< dyn SingletonView > for SingletonBufferTarget +where + T: Clone + Send + Sync + 'static +{ + fn notify(&mut self, _msg: &()) { + if let Some(v) = self.src_view.clone() { + self.buffer.set( v.get() ); + } + } + + fn reset(&mut self, view: Option>>) { + self.src_view = view; + self.notify(&()); + } } impl SingletonBuffer where - T: Clone + Send + Sync + 'static, + T: Clone + Send + Sync + 'static { pub fn with_port(value: T, port: InnerViewPort>) -> Self { let value = Arc::new(RwLock::new(value)); @@ -59,6 +84,26 @@ where } } + pub fn attach_to(&self, port: OuterViewPort>) -> Arc>> { + self.port.0.add_update_hook(Arc::new(port.0.clone())); + + let target = Arc::new(RwLock::new( + SingletonBufferTarget { + buffer: self.clone(), + src_view: None + } + )); + + port.add_observer(target.clone()); + target + } + + pub fn make_in_port(&self) -> InnerViewPort> { + let port = ViewPort::new(); + self.attach_to(port.outer()); + port.into_inner() + } + pub fn new(value: T) -> Self { SingletonBuffer::with_port(value, ViewPort::new().into_inner()) } @@ -84,6 +129,10 @@ where drop(v); self.port.notify(&()); } + + pub fn into_inner(self) -> Arc> { + self.value + } } //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>