projection: define channel type per argument type

This commit is contained in:
Michael Sippel 2021-05-28 00:45:30 +02:00
parent d9654ab292
commit b6df1e7e41
Signed by: senvas
GPG key ID: F96CF119C34B64A6

View file

@ -2,7 +2,8 @@ use {
std::{ std::{
cmp::{max}, cmp::{max},
any::Any, any::Any,
sync::{Arc, Weak} sync::{Arc, Weak},
hash::Hash
}, },
std::sync::RwLock, std::sync::RwLock,
crate::{ crate::{
@ -14,7 +15,7 @@ use {
channel::{ channel::{
ChannelSender, ChannelReceiver, ChannelSender, ChannelReceiver,
ChannelData, ChannelData,
channel set_channel
} }
}, },
singleton::{SingletonView}, singleton::{SingletonView},
@ -53,7 +54,7 @@ impl<P: Send + Sync + 'static> ProjectionHelper<P> {
notify: impl Fn(&mut P, &()) + Send + Sync + 'static notify: impl Fn(&mut P, &()) + Send + Sync + 'static
) -> Arc<RwLock<Option<Arc<dyn SingletonView<Item = Item>>>>> { ) -> Arc<RwLock<Option<Arc<dyn SingletonView<Item = Item>>>>> {
self.update_hooks.write().unwrap().push(Arc::new(port.0.clone())); self.update_hooks.write().unwrap().push(Arc::new(port.0.clone()));
port.add_observer(self.new_arg(notify)); port.add_observer(self.new_arg(notify, set_channel()));
port.get_view_arc() port.get_view_arc()
} }
@ -63,31 +64,34 @@ impl<P: Send + Sync + 'static> ProjectionHelper<P> {
notify: impl Fn(&mut P, &usize) + Send + Sync + 'static notify: impl Fn(&mut P, &usize) + Send + Sync + 'static
) -> Arc<RwLock<Option<Arc<dyn SequenceView<Item = Item>>>>> { ) -> Arc<RwLock<Option<Arc<dyn SequenceView<Item = Item>>>>> {
self.update_hooks.write().unwrap().push(Arc::new(port.0.clone())); self.update_hooks.write().unwrap().push(Arc::new(port.0.clone()));
port.add_observer(self.new_arg(notify)); port.add_observer(self.new_arg(notify, set_channel()));
port.get_view_arc() port.get_view_arc()
} }
pub fn new_index_arg<Key: Clone + Send + Sync + 'static, Item: 'static>( pub fn new_index_arg<Key: Hash + Eq + Clone + Send + Sync + 'static, Item: 'static>(
&mut self, &mut self,
port: OuterViewPort<dyn IndexView<Key, Item = Item>>, port: OuterViewPort<dyn IndexView<Key, Item = Item>>,
notify: impl Fn(&mut P, &Key) + Send + Sync + 'static notify: impl Fn(&mut P, &Key) + Send + Sync + 'static
) -> Arc<RwLock<Option<Arc<dyn IndexView<Key, Item = Item>>>>> { ) -> Arc<RwLock<Option<Arc<dyn IndexView<Key, Item = Item>>>>> {
self.update_hooks.write().unwrap().push(Arc::new(port.0.clone())); self.update_hooks.write().unwrap().push(Arc::new(port.0.clone()));
let arg = self.new_arg(notify); let arg = self.new_arg(notify, set_channel());
port.add_observer(arg); port.add_observer(arg);
port.get_view_arc() port.get_view_arc()
} }
pub fn new_arg< pub fn new_arg<
V: View + ?Sized + 'static V: View + ?Sized + 'static,
D: ChannelData<Item = V::Msg> + 'static
>( >(
&mut self, &mut self,
notify: impl Fn(&mut P, &V::Msg) + Send + Sync + 'static notify: impl Fn(&mut P, &V::Msg) + Send + Sync + 'static,
) -> Arc<RwLock<ProjectionArg<P, V, Vec<V::Msg>>>> (tx, rx): (ChannelSender<D>, ChannelReceiver<D>)
where V::Msg: Send + Sync )
-> Arc<RwLock<ProjectionArg<P, V, D>>>
where V::Msg: Send + Sync,
D::IntoIter: Send + Sync + 'static
{ {
let (tx, rx) = channel::<Vec<V::Msg>>();
let arg = Arc::new(RwLock::new( let arg = Arc::new(RwLock::new(
ProjectionArg { ProjectionArg {
src: None, src: None,