Skip to content

Commit

Permalink
Clean up and drop stale events
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Jan 21, 2024
1 parent 535eaa8 commit 0179a3d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
4 changes: 4 additions & 0 deletions crates/concoct/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ pub struct TextViewContext {
}

impl TextViewContext {
/// Create a text view context from a view function.
///
/// Text-based views, such as `&str` or `String` will call
/// this view function on when rendered.
pub fn new(view: impl FnMut(Cow<'static, str>) + 'static) -> Self {
Self {
view: RefCell::new(Box::new(view)),
Expand Down
7 changes: 4 additions & 3 deletions crates/concoct/src/rt.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use crate::Tree;
use rustc_hash::FxHashMap;
use slotmap::{DefaultKey, SlotMap};
use std::{
any::{Any, TypeId},
cell::RefCell,
collections::{HashMap, VecDeque},
collections::VecDeque,
rc::Rc,
task::Waker,
time::Instant,
};

#[derive(Default)]
pub(crate) struct ScopeInner {
pub(crate) contexts: HashMap<TypeId, Rc<dyn Any>>,
pub(crate) contexts: FxHashMap<TypeId, Rc<dyn Any>>,
pub(crate) hooks: Vec<Rc<dyn Any>>,
pub(crate) hook_idx: usize,
pub(crate) droppers: Vec<Box<dyn FnMut()>>,
Expand All @@ -29,7 +30,7 @@ pub(crate) struct RuntimeInner {
pub(crate) scope: Option<Scope>,
pub(crate) nodes: SlotMap<DefaultKey, *mut dyn Tree>,
pub(crate) waker: Option<Waker>,
pub(crate) contexts: HashMap<TypeId, Rc<dyn Any>>,
pub(crate) contexts: FxHashMap<TypeId, Rc<dyn Any>>,
pub(crate) limit: Option<Instant>,
}

Expand Down
13 changes: 8 additions & 5 deletions crates/concoct/src/vdom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct VirtualDom<T> {
}

impl<T> VirtualDom<T> {
/// Create a new virtual dom from a tree.
pub fn new(tree: T) -> Self {
VirtualDom {
cx: Runtime::default(),
Expand All @@ -25,6 +26,7 @@ impl<T> VirtualDom<T> {
{
self.cx.enter();

// Safety: Context is dropped when the tree is
unsafe { self.tree.build() }
}

Expand Down Expand Up @@ -62,13 +64,14 @@ impl<T> VirtualDom<T> {
inner.waker = waker;

if let Some(key) = inner.pending.pop_front() {
let raw = inner.nodes[key];
drop(inner);
if let Some(raw) = inner.nodes.get(key).copied() {
drop(inner);

self.cx.enter();
self.cx.enter();

let pending = unsafe { &mut *raw };
unsafe { pending.build() };
// Safety: `raw` is guaranteed to be an `&mut dyn Tree`.
unsafe { (&mut *raw).build() };
}
}
}
}
2 changes: 1 addition & 1 deletion crates/concoct/src/view/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<V: View> Tree for ViewCell<V> {
self.tree.borrow_mut().as_tree().build()
}

unsafe fn rebuild(&mut self, last: &mut dyn Any,is_changed: bool) {
unsafe fn rebuild(&mut self, last: &mut dyn Any, is_changed: bool) {
let last = last.downcast_mut::<Self>().unwrap();
self.tree
.borrow_mut()
Expand Down

0 comments on commit 0179a3d

Please sign in to comment.