use { std::{ cmp::{max}, any::Any, sync::{Arc, Weak}, hash::Hash }, std::sync::RwLock, crate::{ core::{ View, Observer, ObserverExt, port::UpdateTask, OuterViewPort, channel::{ ChannelSender, ChannelReceiver, ChannelData, set_channel } }, singleton::{SingletonView}, sequence::{SequenceView}, index::{IndexView} } }; //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> pub struct ProjectionHelper { keepalive: Vec>, proj: Arc>>>, update_hooks: Arc>>> } impl ProjectionHelper

{ pub fn new(update_hooks: Arc>>>) -> Self { ProjectionHelper { keepalive: Vec::new(), proj: Arc::new(RwLock::new(Weak::new())), update_hooks } } pub fn set_proj(&mut self, proj: &Arc>) { *self.proj.write().unwrap() = Arc::downgrade(proj); } // todo: make this functions generic over the View // this does currently not work because Observer is not implemented for ProjectionArg for *all* V. pub fn new_singleton_arg( &mut self, port: OuterViewPort>, notify: impl Fn(&mut P, &()) + Send + Sync + 'static ) -> Arc>>>> { self.update_hooks.write().unwrap().push(Arc::new(port.0.clone())); port.add_observer(self.new_arg(notify, set_channel())); port.get_view_arc() } pub fn new_sequence_arg( &mut self, port: OuterViewPort>, notify: impl Fn(&mut P, &usize) + Send + Sync + 'static ) -> Arc>>>> { self.update_hooks.write().unwrap().push(Arc::new(port.0.clone())); port.add_observer(self.new_arg(notify, set_channel())); port.get_view_arc() } pub fn new_index_arg( &mut self, port: OuterViewPort>, notify: impl Fn(&mut P, &Key) + Send + Sync + 'static ) -> Arc>>>> { self.update_hooks.write().unwrap().push(Arc::new(port.0.clone())); let arg = self.new_arg(notify, set_channel()); port.add_observer(arg); port.get_view_arc() } pub fn new_arg< V: View + ?Sized + 'static, D: ChannelData + 'static >( &mut self, notify: impl Fn(&mut P, &V::Msg) + Send + Sync + 'static, (tx, rx): (ChannelSender, ChannelReceiver) ) -> Arc>> where V::Msg: Send + Sync, D::IntoIter: Send + Sync + 'static { let arg = Arc::new(RwLock::new( ProjectionArg { src: None, notify: Box::new(notify), proj: self.proj.clone(), rx, tx })); self.keepalive.push(arg.clone()); self.update_hooks.write().unwrap().push(arg.clone()); arg } } /// Special Observer which can access the state of the projection on notify /// also handles the reset() pub struct ProjectionArg where P: Send + Sync + 'static, V: View + ?Sized, D: ChannelData, D::IntoIter: Send + Sync { src: Option>, notify: Box, proj: Arc>>>, rx: ChannelReceiver, tx: ChannelSender } impl UpdateTask for ProjectionArg where P: Send + Sync + 'static, V: View + ?Sized, D: ChannelData, D::IntoIter: Send + Sync { fn update(&self) { if let Some(p) = self.proj.read().unwrap().upgrade() { if let Some(data) = self.rx.try_recv() { for msg in data { (self.notify)( &mut *p.write().unwrap(), &msg ); } } } } } impl UpdateTask for RwLock> where P: Send + Sync + 'static, V: View + ?Sized, D: ChannelData, D::IntoIter: Send + Sync { fn update(&self) { self.read().unwrap().update(); } } //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> impl Observer> for ProjectionArg, D> where P: Send + Sync + 'static, D: ChannelData, D::IntoIter: Send + Sync { fn reset(&mut self, new_src: Option>>) { self.src = new_src; self.notify(&()); } fn notify(&mut self, msg: &()) { self.tx.send(msg.clone()); } } //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> impl Observer> for ProjectionArg, D> where P: Send + Sync + 'static, D: ChannelData, D::IntoIter: Send + Sync { fn reset(&mut self, new_src: Option>>) { let old_len = self.src.len().unwrap_or(0); self.src = new_src; let new_len = self.src.len().unwrap_or(0); self.notify_each(0 .. max(old_len, new_len)); } fn notify(&mut self, msg: &usize) { self.tx.send(*msg); } } //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> impl Observer> for ProjectionArg, D> where P: Send + Sync + 'static, Key: Clone + Send + Sync, D: ChannelData, D::IntoIter: Send + Sync { fn reset(&mut self, new_src: Option>>) { let old_area = self.src.area(); self.src = new_src; let new_area = self.src.area(); if let Some(area) = old_area { self.notify_each(area); } if let Some(area) = new_area { self.notify_each(area); } } fn notify(&mut self, msg: &Key) { self.tx.send(msg.clone()); } }