Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Jan 20, 2024
1 parent 20bc84f commit 50576e9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
10 changes: 9 additions & 1 deletion crates/concoct/src/body.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{Node, Tree, View};
use std::{cell::RefCell, rc::Rc};
use std::{cell::RefCell, hash::Hash, rc::Rc};

pub trait Body: 'static {
fn into_tree(self) -> impl Tree;
Expand All @@ -11,6 +11,14 @@ impl<B: Body> Body for Option<B> {
}
}

impl<K: Hash + Eq + 'static, B: Body> Body for Vec<(K, B)> {
fn into_tree(self) -> impl Tree {
self.into_iter()
.map(|(key, body)| (key, body.into_tree()))
.collect::<Vec<_>>()
}
}

pub struct Child<B> {
cell: Rc<RefCell<Option<B>>>,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/concoct/src/hook/use_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::use_ref;
use crate::Context;
use std::{cell::RefCell, rc::Rc};

pub fn use_state<T: Clone + 'static>(make_value: impl FnOnce() -> T) -> (T, Rc<impl Fn(T)>) {
pub fn use_state<T: Clone + 'static>(make_value: impl FnOnce() -> T) -> (T, Rc<dyn Fn(T)>) {
let cell = use_ref(|| RefCell::new(make_value()));
let getter = cell.borrow().clone();

Expand Down
37 changes: 36 additions & 1 deletion crates/concoct/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use slotmap::{DefaultKey, SlotMap};
use std::any::{Any, TypeId};
use std::borrow::Cow;
use std::collections::{HashMap, VecDeque};
use std::collections::{HashMap, HashSet, VecDeque};
use std::hash::Hash;
use std::task::{Poll, Waker};
use std::{cell::RefCell, mem, rc::Rc};

Expand Down Expand Up @@ -224,6 +225,40 @@ impl Tree for Empty {
fn remove(&mut self) {}
}

impl<K: Hash + Eq + 'static, T: Tree> Tree for Vec<(K, T)> {
fn build(&mut self) {
for (_, body) in self.iter_mut() {
body.build()
}
}

fn rebuild(&mut self, last: &mut dyn Any) {
let mut visited = HashSet::new();
let last = last.downcast_mut::<Self>().unwrap();

for (key, body) in self.iter_mut() {
if let Some((_, last_body)) = last.iter_mut().find(|(last_key, _)| last_key == key) {
body.rebuild(last_body);
visited.insert(key);
} else {
body.build();
}
}

for (key, body) in last.iter_mut() {
if !visited.contains(key) {
body.remove();
}
}
}

fn remove(&mut self) {
for (_, body) in self.iter_mut() {
body.remove()
}
}
}

impl<V, B, F> Tree for Node<V, B, F>
where
V: View,
Expand Down

0 comments on commit 50576e9

Please sign in to comment.