Compare commits

...

2 commits

4 changed files with 15 additions and 16 deletions

View file

@ -83,8 +83,7 @@ impl<T> VecBuffer<T>
where where
T: Clone + Send + Sync + 'static, T: Clone + Send + Sync + 'static,
{ {
pub fn with_data_port(data: Vec<T>, port: InnerViewPort<RwLock<Vec<T>>>) -> Self { pub fn with_data_arc_port(data: Arc<RwLock<Vec<T>>>, port: InnerViewPort<RwLock<Vec<T>>>) -> Self {
let data = Arc::new(RwLock::new(data));
port.set_view(Some(data.clone())); port.set_view(Some(data.clone()));
for x in data.read().unwrap().iter().cloned() { for x in data.read().unwrap().iter().cloned() {
@ -97,6 +96,11 @@ where
} }
} }
pub fn with_data_port(data: Vec<T>, port: InnerViewPort<RwLock<Vec<T>>>) -> Self {
let data = Arc::new(RwLock::new(data));
Self::with_data_arc_port( data, port )
}
pub fn attach_to(&self, port: OuterViewPort< dyn ListView<T> >) -> Arc<RwLock<VecBufferTarget<T>>> { pub fn attach_to(&self, port: OuterViewPort< dyn ListView<T> >) -> Arc<RwLock<VecBufferTarget<T>>> {
self.port.0.add_update_hook(Arc::new(port.0.clone())); self.port.0.add_update_hook(Arc::new(port.0.clone()));
@ -271,7 +275,7 @@ mod tests {
buf.push('b'); buf.push('b');
list_view.0.update(); buf2.get_port().0.update();
assert_eq!(buf2.len(), 2); assert_eq!(buf2.len(), 2);
assert_eq!(buf2.get(0), 'a'); assert_eq!(buf2.get(0), 'a');
assert_eq!(buf2.get(1), 'b'); assert_eq!(buf2.get(1), 'b');
@ -279,7 +283,7 @@ mod tests {
buf.push('c'); buf.push('c');
buf.remove(0); buf.remove(0);
list_view.0.update(); buf2.get_port().0.update();
assert_eq!(buf2.len(), 2); assert_eq!(buf2.len(), 2);
assert_eq!(buf2.get(0), 'b'); assert_eq!(buf2.get(0), 'b');
assert_eq!(buf2.get(1), 'c'); assert_eq!(buf2.get(1), 'c');

View file

@ -78,24 +78,20 @@ where Item: Clone + Send + Sync + 'static,
if let Some(v) = self.src_view.as_ref() { if let Some(v) = self.src_view.as_ref() {
self.end = v.len().unwrap(); self.end = v.len().unwrap();
for idx in 0 .. self.end { for idx in 0 .. self.end {
if idx < self.end {
let val = v.get( &(self.end - idx - 1) ).unwrap(); let val = v.get( &(self.end - idx - 1) ).unwrap();
self.cast.notify(&ListDiff::Insert{ idx: idx, val }); self.cast.notify(&ListDiff::Insert{ idx: idx, val });
} }
} } else {
self.end = 0;
} }
} }
fn notify(&mut self, msg: &ListDiff<Item>) { fn notify(&mut self, msg: &ListDiff<Item>) {
/* todo optimize
*/
//let len = self.src_view.len().unwrap();
self.cast.notify(&match msg { self.cast.notify(&match msg {
ListDiff::Clear => { ListDiff::Clear => {
self.end = 0; self.end = 0;
ListDiff::Clear ListDiff::Clear
}, }
ListDiff::Remove(mut idx) => { ListDiff::Remove(mut idx) => {
self.end -= 1; self.end -= 1;
idx = self.end - idx; idx = self.end - idx;

View file

@ -67,6 +67,7 @@ where
fn notify(&mut self, diff: &VecDiff<T>) { fn notify(&mut self, diff: &VecDiff<T>) {
match diff { match diff {
VecDiff::Clear => { VecDiff::Clear => {
self.cur_len = 0;
self.cast.notify(&ListDiff::Clear); self.cast.notify(&ListDiff::Clear);
} }
VecDiff::Push(val) => { VecDiff::Push(val) => {

View file

@ -107,8 +107,6 @@ where V::Msg: Clone
pub fn attach_to_port(&self, other_port: ViewPort<V>) { pub fn attach_to_port(&self, other_port: ViewPort<V>) {
self.set_view( other_port.view.read().unwrap().clone() ); self.set_view( other_port.view.read().unwrap().clone() );
other_port.add_observer( self.cast.clone() ); other_port.add_observer( self.cast.clone() );
// todo: forward reset() ?
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) );
} }