feat: add prevent default attribute and upgrade router

This commit is contained in:
Jonathan Kelley committed 2022-01-05 16:34:24 -05:00
1 parent cb2782b4bb
commit 427b126bc1
16 files changed
+220 -181

No files matched your search

+32 -19
View File
@@ -263,7 +263,7 @@ impl<'bump> DiffState<'bump> {
};
if deadline_expired() {
log::debug!("Deadline expired before we could finish!");
log::trace!("Deadline expired before we could finish!");
return false;
}
}
@@ -423,16 +423,25 @@ impl<'bump> DiffState<'bump> {
fn create_component_node(&mut self, vcomponent: &'bump VComponent<'bump>) {
let parent_idx = self.stack.current_scope().unwrap();
// Insert a new scope into our component list
let props: Box<dyn AnyProps + 'bump> = vcomponent.props.borrow_mut().take().unwrap();
let props: Box<dyn AnyProps + 'static> = unsafe { std::mem::transmute(props) };
let new_idx = self.scopes.new_with_key(
vcomponent.user_fc,
props,
Some(parent_idx),
self.stack.element_stack.last().copied().unwrap(),
0,
);
// the component might already exist - if it does, we need to reuse it
// this makes figure out when to drop the component more complicated
let new_idx = if let Some(idx) = vcomponent.scope.get() {
assert!(self.scopes.get_scope(idx).is_some());
idx
} else {
// Insert a new scope into our component list
let props: Box<dyn AnyProps + 'bump> = vcomponent.props.borrow_mut().take().unwrap();
let props: Box<dyn AnyProps + 'static> = unsafe { std::mem::transmute(props) };
let new_idx = self.scopes.new_with_key(
vcomponent.user_fc,
props,
Some(parent_idx),
self.stack.element_stack.last().copied().unwrap(),
0,
);
new_idx
};
// Actually initialize the caller's slot with the right address
vcomponent.scope.set(Some(new_idx));
@@ -443,9 +452,6 @@ impl<'bump> DiffState<'bump> {
}
false => {
// track this component internally so we know the right drop order
// let cur_scope = self.scopes.get_scope(parent_idx).unwrap();
// let extended = unsafe { std::mem::transmute(vcomponent) };
// cur_scope.items.borrow_mut().borrowed_props.push(extended);
}
}
@@ -680,10 +686,10 @@ impl<'bump> DiffState<'bump> {
new: &'bump VComponent<'bump>,
) {
let scope_addr = old.scope.get().unwrap();
log::debug!("diff_component_nodes: {:?}", scope_addr);
log::trace!("diff_component_nodes: {:?}", scope_addr);
if std::ptr::eq(old, new) {
log::debug!("skipping component diff - component is the sames");
log::trace!("skipping component diff - component is the sames");
return;
}
@@ -734,7 +740,7 @@ impl<'bump> DiffState<'bump> {
self.scopes.fin_head(scope_addr),
);
} else {
log::debug!("memoized");
log::trace!("memoized");
// memoization has taken place
drop(new_props);
};
@@ -1239,7 +1245,10 @@ impl<'bump> DiffState<'bump> {
let scope_id = c.scope.get().unwrap();
self.scopes.try_remove(scope_id).unwrap();
// we can only remove components if they are actively being diffed
if self.stack.scope_stack.contains(&c.originator) {
self.scopes.try_remove(scope_id).unwrap();
}
}
}
}
@@ -1290,7 +1299,11 @@ impl<'bump> DiffState<'bump> {
let scope_id = c.scope.get().unwrap();
let root = self.scopes.root_node(scope_id);
self.remove_nodes(Some(root), gen_muts);
self.scopes.try_remove(scope_id).unwrap();
// we can only remove this node if the originator is actively
if self.stack.scope_stack.contains(&c.originator) {
self.scopes.try_remove(scope_id).unwrap();
}
}
}
}
+71 -54
View File
@@ -60,10 +60,7 @@ impl<'a, 'b> LazyNodes<'a, 'b> {
}
}
pub fn new<F>(_val: F) -> Self
where
F: FnOnce(NodeFactory<'a>) -> VNode<'a> + 'b,
{
pub fn new(_val: impl FnOnce(NodeFactory<'a>) -> VNode<'a> + 'b) -> Self {
// there's no way to call FnOnce without a box, so we need to store it in a slot and use static dispatch
let mut slot = Some(_val);
@@ -240,57 +237,77 @@ fn round_to_words(len: usize) -> usize {
(len + mem::size_of::<usize>() - 1) / mem::size_of::<usize>()
}
#[test]
fn it_works() {
let bump = bumpalo::Bump::new();
#[cfg(test)]
mod tests {
use super::*;
use crate::innerlude::{Element, Scope, VirtualDom};
// let factory = NodeFactory { bump: &bump };
#[test]
fn it_works() {
fn app(cx: Scope<()>) -> Element {
cx.render(LazyNodes::new_some(|f| {
f.text(format_args!("hello world!"))
}))
}
// let caller = LazyNodes::new_some(|f| {
// //
// f.text(format_args!("hello world!"))
// });
// let g = caller.call(factory);
let mut dom = VirtualDom::new(app);
dom.rebuild();
// dbg!(g);
}
#[test]
fn it_drops() {
use std::rc::Rc;
let bump = bumpalo::Bump::new();
// let factory = NodeFactory { bump: &bump };
// struct DropInner {
// id: i32,
// }
// impl Drop for DropInner {
// fn drop(&mut self) {
// log::debug!("dropping inner");
// }
// }
// let val = Rc::new(10);
// let caller = {
// let it = (0..10)
// .map(|i| {
// let val = val.clone();
// LazyNodes::new_some(move |f| {
// log::debug!("hell closure");
// let inner = DropInner { id: i };
// f.text(format_args!("hello world {:?}, {:?}", inner.id, val))
// })
// })
// .collect::<Vec<_>>();
// LazyNodes::new_some(|f| {
// log::debug!("main closure");
// f.fragment_from_iter(it)
// })
// };
// let _ = caller.call(factory);
// assert_eq!(Rc::strong_count(&val), 1);
let g = dom.base_scope().root_node();
dbg!(g);
}
#[test]
fn it_drops() {
use std::rc::Rc;
struct AppProps {
inner: Rc<i32>,
}
fn app(cx: Scope<AppProps>) -> Element {
struct DropInner {
id: i32,
}
impl Drop for DropInner {
fn drop(&mut self) {
log::debug!("dropping inner");
}
}
let caller = {
let it = (0..10)
.map(|i| {
let val = cx.props.inner.clone();
LazyNodes::new_some(move |f| {
log::debug!("hell closure");
let inner = DropInner { id: i };
f.text(format_args!("hello world {:?}, {:?}", inner.id, val))
})
})
.collect::<Vec<_>>();
LazyNodes::new_some(|f| {
log::debug!("main closure");
f.fragment_from_iter(it)
})
};
cx.render(caller)
}
let inner = Rc::new(0);
let mut dom = VirtualDom::new_with_props(
app,
AppProps {
inner: inner.clone(),
},
);
dom.rebuild();
drop(dom);
assert_eq!(Rc::strong_count(&inner), 1);
}
}
+5 -8
View File
@@ -350,6 +350,7 @@ impl Clone for EventHandler<'_> {
/// Only supports the functional syntax
pub struct VComponent<'src> {
pub key: Option<&'src str>,
pub originator: ScopeId,
pub scope: Cell<Option<ScopeId>>,
pub can_memoize: bool,
pub user_fc: *const (),
@@ -526,6 +527,7 @@ impl<'a> NodeFactory<'a> {
scope: Default::default(),
can_memoize: P::IS_STATIC,
user_fc: component as *const (),
originator: self.scope.scope_id(),
props: RefCell::new(Some(Box::new(VComponentProps {
// local_props: RefCell::new(Some(props)),
// heap_props: RefCell::new(None),
@@ -749,19 +751,14 @@ impl IntoVNode<'_> for Arguments<'_> {
impl<'a> IntoVNode<'a> for &Option<VNode<'a>> {
fn into_vnode(self, cx: NodeFactory<'a>) -> VNode<'a> {
self.as_ref()
.map(|f| f.decouple())
.unwrap_or(VNode::Placeholder(
cx.bump.alloc(VPlaceholder { id: empty_cell() }),
))
.map(|f| f.into_vnode(cx))
.unwrap_or_else(|| cx.fragment_from_iter(None as Option<VNode>))
}
}
impl<'a> IntoVNode<'a> for &VNode<'a> {
fn into_vnode(self, _cx: NodeFactory<'a>) -> VNode<'a> {
// borrowed nodes are strange
self.decouple()
}
}
trait IntoAcceptedVnode<'a> {
fn into_accepted_vnode(self, cx: NodeFactory<'a>) -> VNode<'a>;
}
+7 -7
View File
@@ -143,7 +143,7 @@ impl ScopeArena {
// Removes a scope and its descendents from the arena
pub fn try_remove(&self, id: ScopeId) -> Option<()> {
log::debug!("removing scope {:?}", id);
log::trace!("removing scope {:?}", id);
self.ensure_drop_safety(id);
// Safety:
@@ -189,7 +189,7 @@ impl ScopeArena {
/// This also makes sure that drop order is consistent and predictable. All resources that rely on being dropped will
/// be dropped.
pub(crate) fn ensure_drop_safety(&self, scope_id: ScopeId) {
log::debug!("Ensuring drop safety for scope {:?}", scope_id);
log::trace!("Ensuring drop safety for scope {:?}", scope_id);
if let Some(scope) = self.get_scope(scope_id) {
let mut items = scope.items.borrow_mut();
@@ -220,7 +220,7 @@ impl ScopeArena {
// Cycle to the next frame and then reset it
// This breaks any latent references, invalidating every pointer referencing into it.
// Remove all the outdated listeners
log::debug!("Running scope {:?}", id);
log::trace!("Running scope {:?}", id);
self.ensure_drop_safety(id);
// todo: we *know* that this is aliased by the contents of the scope itself
@@ -282,18 +282,18 @@ impl ScopeArena {
let nodes = self.nodes.borrow();
let mut cur_el = Some(element);
log::debug!("calling listener {:?}, {:?}", event, element);
log::trace!("calling listener {:?}, {:?}", event, element);
let state = Rc::new(BubbleState::new());
while let Some(id) = cur_el.take() {
if let Some(el) = nodes.get(id.0) {
log::debug!("Found valid receiver element");
log::trace!("Found valid receiver element");
let real_el = unsafe { &**el };
if let VNode::Element(real_el) = real_el {
for listener in real_el.listeners.borrow().iter() {
if listener.event == event.name {
log::debug!("Found valid receiver event");
log::trace!("Found valid receiver event");
if state.canceled.get() {
// stop bubbling if canceled
@@ -912,7 +912,7 @@ impl TaskQueue {
} else {
// todo: it should be okay to remote a fut while the queue is being polled
// However, it's not currently possible to do that.
log::debug!("Unable to remove task from task queue. This is probably a bug.");
log::trace!("Unable to remove task from task queue. This is probably a bug.");
}
}
pub(crate) fn has_tasks(&self) -> bool {