wip: refactor a bit
This commit is contained in:
1 parent
8c541f66d5
commit
2eeb8f2386
9 files changed
+553
-297
No files matched your search
@@ -1,4 +1,4 @@
|
||||
use crate::{nodebuilder::IntoDomTree, prelude::*};
|
||||
use crate::{nodebuilder::IntoDomTree, prelude::*, scope::Scope};
|
||||
use crate::{nodebuilder::LazyNodes, nodes::VNode};
|
||||
use bumpalo::Bump;
|
||||
use hooks::Hook;
|
||||
@@ -27,15 +27,12 @@ use std::{cell::RefCell, future::Future, ops::Deref, pin::Pin, rc::Rc, sync::ato
|
||||
pub struct Context<'src> {
|
||||
pub idx: RefCell<usize>,
|
||||
|
||||
pub scope: ScopeIdx,
|
||||
|
||||
// Borrowed from scope
|
||||
// pub(crate) arena: &'src typed_arena::Arena<Hook>,
|
||||
pub(crate) hooks: &'src RefCell<Vec<Hook>>,
|
||||
// pub(crate) hooks: &'src RefCell<Vec<*mut Hook>>,
|
||||
pub(crate) bump: &'src Bump,
|
||||
|
||||
pub listeners: &'src RefCell<Vec<*const dyn Fn(crate::events::VirtualEvent)>>,
|
||||
// pub scope: ScopeIdx,
|
||||
pub scope: &'src Scope,
|
||||
// // Borrowed from scope
|
||||
// pub(crate) hooks: &'src RefCell<Vec<Hook>>,
|
||||
// pub(crate) bump: &'src Bump,
|
||||
// pub listeners: &'src RefCell<Vec<*const dyn Fn(crate::events::VirtualEvent)>>,
|
||||
|
||||
// holder for the src lifetime
|
||||
// todo @jon remove this
|
||||
@@ -101,10 +98,10 @@ impl<'a> Context<'a> {
|
||||
lazy_nodes: LazyNodes<'a, F>,
|
||||
) -> DomTree {
|
||||
let ctx = NodeCtx {
|
||||
bump: self.bump,
|
||||
scope: self.scope,
|
||||
bump: &self.scope.cur_frame().bump,
|
||||
scope: self.scope.myidx,
|
||||
idx: 0.into(),
|
||||
listeners: self.listeners,
|
||||
listeners: &self.scope.listeners,
|
||||
};
|
||||
|
||||
let safe_nodes = lazy_nodes.into_vnode(&ctx);
|
||||
@@ -145,7 +142,7 @@ pub mod hooks {
|
||||
let idx = *self.idx.borrow();
|
||||
|
||||
// Mutate hook list if necessary
|
||||
let mut hooks = self.hooks.borrow_mut();
|
||||
let mut hooks = self.scope.hooks.borrow_mut();
|
||||
|
||||
// Initialize the hook by allocating it in the typed arena.
|
||||
// We get a reference from the arena which is owned by the component scope
|
||||
|
||||
+39
-13
@@ -44,7 +44,7 @@ pub struct Scope {
|
||||
// - is self-refenrential and therefore needs to point into the bump
|
||||
// Stores references into the listeners attached to the vnodes
|
||||
// NEEDS TO BE PRIVATE
|
||||
listeners: RefCell<Vec<*const dyn Fn(VirtualEvent)>>,
|
||||
pub(crate) listeners: RefCell<Vec<*const dyn Fn(VirtualEvent)>>,
|
||||
}
|
||||
|
||||
impl Scope {
|
||||
@@ -85,21 +85,26 @@ impl Scope {
|
||||
/// Props is ?Sized because we borrow the props and don't need to know the size. P (sized) is used as a marker (unsized)
|
||||
pub fn run_scope<'b>(&'b mut self) -> Result<()> {
|
||||
// pub fn run_scope<'bump>(&'bump mut self) -> Result<()> {
|
||||
let frame = {
|
||||
// let frame = {
|
||||
{
|
||||
let frame = self.frames.next();
|
||||
frame.bump.reset();
|
||||
frame
|
||||
};
|
||||
}
|
||||
// frame
|
||||
// };
|
||||
// self.new_frame()
|
||||
|
||||
let ctx: Context<'b> = Context {
|
||||
let ctx = Context {
|
||||
// arena: &self.hook_arena,
|
||||
hooks: &self.hooks,
|
||||
bump: &frame.bump,
|
||||
// hooks: &self.hooks,
|
||||
// bump: &frame.bump,
|
||||
idx: 0.into(),
|
||||
_p: PhantomData {},
|
||||
scope: self.myidx,
|
||||
listeners: &self.listeners,
|
||||
scope: self,
|
||||
// scope: self.myidx,
|
||||
// listeners: &self.listeners,
|
||||
};
|
||||
|
||||
let caller = self.caller.upgrade().expect("Failed to get caller");
|
||||
|
||||
/*
|
||||
@@ -114,12 +119,16 @@ impl Scope {
|
||||
- Public API cannot drop or destructure VNode
|
||||
*/
|
||||
|
||||
frame.head_node = unsafe {
|
||||
let caller2: Rc<OpaqueComponent<'b>> = std::mem::transmute(caller);
|
||||
let tree = (caller2.as_ref())(ctx);
|
||||
tree.root
|
||||
let new_head = unsafe {
|
||||
// frame.head_node = unsafe {
|
||||
// // use the same type, just manipulate the lifetime
|
||||
type ComComp<'c> = Rc<OpaqueComponent<'c>>;
|
||||
let caller = std::mem::transmute::<ComComp<'static>, ComComp<'b>>(caller);
|
||||
let r: DomTree = (caller.as_ref())(ctx);
|
||||
r
|
||||
};
|
||||
|
||||
self.frames.cur_frame_mut().head_node = new_head.root;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -161,6 +170,10 @@ impl Scope {
|
||||
pub fn old_frame<'bump>(&'bump self) -> &'bump VNode<'bump> {
|
||||
self.frames.prev_head_node()
|
||||
}
|
||||
|
||||
pub fn cur_frame(&self) -> &BumpFrame {
|
||||
self.frames.cur_frame()
|
||||
}
|
||||
}
|
||||
|
||||
// ==========================
|
||||
@@ -203,6 +216,19 @@ impl ActiveFrame {
|
||||
}
|
||||
}
|
||||
|
||||
fn cur_frame(&self) -> &BumpFrame {
|
||||
match *self.idx.borrow() & 1 == 0 {
|
||||
true => &self.frames[0],
|
||||
false => &self.frames[1],
|
||||
}
|
||||
}
|
||||
fn cur_frame_mut(&mut self) -> &mut BumpFrame {
|
||||
match *self.idx.borrow() & 1 == 0 {
|
||||
true => &mut self.frames[0],
|
||||
false => &mut self.frames[1],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn current_head_node<'b>(&'b self) -> &'b VNode<'b> {
|
||||
let raw_node = match *self.idx.borrow() & 1 == 0 {
|
||||
true => &self.frames[0],
|
||||
|
||||
@@ -27,7 +27,7 @@ pub struct VirtualDom {
|
||||
/// Will not be ready if the dom is fresh
|
||||
pub base_scope: ScopeIdx,
|
||||
|
||||
pub(crate) update_schedule: Rc<RefCell<Vec<HierarchyMarker>>>,
|
||||
pub(crate) update_schedule: UpdateFunnel,
|
||||
|
||||
// a strong allocation to the "caller" for the original props
|
||||
#[doc(hidden)]
|
||||
@@ -74,7 +74,7 @@ impl VirtualDom {
|
||||
components,
|
||||
_root_caller: root_caller,
|
||||
base_scope,
|
||||
update_schedule: Rc::new(RefCell::new(Vec::new())),
|
||||
update_schedule: UpdateFunnel::default(),
|
||||
_root_prop_type: TypeId::of::<P>(),
|
||||
}
|
||||
}
|
||||
@@ -211,5 +211,21 @@ impl VirtualDom {
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct HierarchyMarker {
|
||||
height: u32,
|
||||
source: ScopeIdx,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, PartialEq, Clone)]
|
||||
pub struct UpdateFunnel(Rc<RefCell<Vec<HierarchyMarker>>>);
|
||||
|
||||
impl UpdateFunnel {
|
||||
fn schedule_update(&self, source: ScopeIdx) -> impl Fn() {
|
||||
let inner = self.clone();
|
||||
move || {
|
||||
inner
|
||||
.0
|
||||
.as_ref()
|
||||
.borrow_mut()
|
||||
.push(HierarchyMarker { source })
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user