wip: cleanup

This commit is contained in:
Jonathan Kelley committed 2021-07-21 17:05:48 -04:00
1 parent 6051b0ec86
commit 1745a44d94
8 files changed
+105 -30

No files matched your search

+25 -6
View File
@@ -205,13 +205,13 @@ Any function prefixed with "use" should not be called conditionally.
///
///
///
pub fn use_context_provider<T, F>(self, init: F) -> &'src Rc<T>
pub fn use_provide_context<T, F>(self, init: F) -> &'src Rc<T>
where
T: 'static,
F: FnOnce() -> T,
{
let mut cxs = self.scope.shared_contexts.borrow_mut();
let ty = TypeId::of::<T>();
let contains_key = self.scope.shared_contexts.borrow().contains_key(&ty);
let is_initialized = self.use_hook(
|_| false,
@@ -223,18 +223,29 @@ Any function prefixed with "use" should not be called conditionally.
|_| {},
);
match (is_initialized, cxs.contains_key(&ty)) {
match (is_initialized, contains_key) {
// Do nothing, already initialized and already exists
(true, true) => {}
// Needs to be initialized
(false, false) => {
log::debug!("Initializing context...");
cxs.insert(ty, Rc::new(init()));
let initialized = Rc::new(init());
let p = self
.scope
.shared_contexts
.borrow_mut()
.insert(ty, initialized);
log::info!(
"There are now {} shared contexts for scope {:?}",
self.scope.shared_contexts.borrow().len(),
self.scope.our_arena_idx,
);
}
_ => debug_assert!(false, "Cannot initialize two contexts of the same type"),
};
self.use_context::<T>()
}
@@ -264,9 +275,17 @@ Any function prefixed with "use" should not be called conditionally.
"Searching {:#?} for valid shared_context",
inner.our_arena_idx
);
let shared_contexts = inner.shared_contexts.borrow();
let shared_ctx = {
let shared_contexts = inner.shared_contexts.borrow();
if let Some(shared_cx) = shared_contexts.get(&ty) {
log::debug!(
"This component has {} shared contexts",
shared_contexts.len()
);
shared_contexts.get(&ty).map(|f| f.clone())
};
if let Some(shared_cx) = shared_ctx {
log::debug!("found matching cx");
let rc = shared_cx
.clone()
+12 -7
View File
@@ -50,6 +50,7 @@
use crate::{arena::SharedArena, innerlude::*, tasks::TaskQueue};
use fxhash::FxHashSet;
use smallvec::{smallvec, SmallVec};
use std::any::Any;
@@ -74,7 +75,7 @@ pub struct DiffMachine<'real, 'bump, Dom: RealDom<'bump>> {
pub edits: DomEditor<'real, 'bump>,
pub components: &'bump SharedArena,
pub task_queue: &'bump TaskQueue,
pub cur_idx: ScopeId,
pub cur_idxs: SmallVec<[ScopeId; 5]>,
pub diffed: FxHashSet<ScopeId>,
pub event_queue: EventQueue,
pub seen_nodes: FxHashSet<ScopeId>,
@@ -96,7 +97,7 @@ where
edits: DomEditor::new(edits),
components,
dom,
cur_idx,
cur_idxs: smallvec![cur_idx],
event_queue,
task_queue,
diffed: FxHashSet::default(),
@@ -156,6 +157,7 @@ where
log::warn!("diffing components? {:#?}", new.user_fc);
if old.user_fc == new.user_fc {
// Make sure we're dealing with the same component (by function pointer)
self.cur_idxs.push(old.ass_scope.get().unwrap());
// Make sure the new component vnode is referencing the right scope id
let scope_id = old.ass_scope.get();
@@ -181,6 +183,7 @@ where
} else {
//
}
self.cur_idxs.pop();
self.seen_nodes.insert(scope_id.unwrap());
} else {
@@ -399,10 +402,10 @@ where
log::debug!("Mounting a new component");
let caller = vcomponent.caller.clone();
let parent_idx = self.cur_idx;
let parent_idx = self.cur_idxs.last().unwrap().clone();
// Insert a new scope into our component list
let idx = self
let new_idx = self
.components
.with(|components| {
components.insert_with_key(|new_idx| {
@@ -435,20 +438,22 @@ where
// TODO: abstract this unsafe into the arena abstraction
let inner: &'bump mut _ = unsafe { &mut *self.components.components.get() };
let new_component = inner.get_mut(idx).unwrap();
let new_component = inner.get_mut(new_idx).unwrap();
// Actually initialize the caller's slot with the right address
vcomponent.ass_scope.set(Some(idx));
vcomponent.ass_scope.set(Some(new_idx));
// Run the scope for one iteration to initialize it
new_component.run_scope().unwrap();
// TODO: we need to delete (IE relcaim this node, otherwise the arena will grow infinitely)
let nextnode = new_component.next_frame();
self.cur_idxs.push(new_idx);
let meta = self.create(nextnode);
self.cur_idxs.pop();
// Finally, insert this node as a seen node.
self.seen_nodes.insert(idx);
self.seen_nodes.insert(new_idx);
CreateMeta::new(vcomponent.is_static, meta.added_to_stack)
}
+11
View File
@@ -533,6 +533,17 @@ impl<'a> IntoVNode<'a> for Option<VNode<'a>> {
}
}
impl IntoVNode<'_> for &'static str {
fn into_vnode<'a>(self, cx: NodeFactory<'a>) -> VNode<'a> {
NodeFactory::static_text(self)
}
}
impl IntoVNode<'_> for Arguments<'_> {
fn into_vnode<'a>(self, cx: NodeFactory<'a>) -> VNode<'a> {
cx.text(self)
}
}
impl Debug for NodeFactory<'_> {
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Ok(())