From 6037e5f519cdfef94210ee5577b5b81a6b30f0d2 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Fri, 15 Mar 2024 19:21:26 +0100 Subject: [PATCH] singleton buffer: add SingletonBufferTarget observer for attaching it to another port --- src/buffer/singleton.rs | 63 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 7 deletions(-) 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 + } } //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>