WIP: remove FC

This commit is contained in:
Jonathan Kelley committed 2021-03-07 21:28:20 -05:00
1 parent 3740f81383
commit 92d9521a73
24 files changed
+801 -451

No files matched your search

+21 -21
View File
@@ -39,31 +39,31 @@ mod tests {
use crate::prelude::bumpalo::Bump;
use crate::prelude::*;
fn test_static_fn<'a, P>(b: &'a Bump, r: FC<P>) -> VNode<'a> {
todo!()
}
// fn test_static_fn<'a, P>(b: &'a Bump, r: FC<P>) -> VNode<'a> {
// todo!()
// }
static TestComponent: FC<()> = |ctx, props| {
//
// static TestComponent: FC<()> = |ctx, props| {
// //
ctx.render(html! {
<div>
// ctx.render(html! {
// <div>
</div>
})
};
// </div>
// })
// };
static TestComponent2: FC<()> = |ctx, props| {
//
ctx.render(|ctx| VNode::text("blah"))
};
// static TestComponent2: FC<()> = |ctx, props| {
// //
// ctx.render(|ctx| VNode::text("blah"))
// };
#[test]
fn ensure_types_work() {
let bump = Bump::new();
// #[test]
// fn ensure_types_work() {
// let bump = Bump::new();
// Happiness! The VNodes are now allocated onto the bump vdom
let _ = test_static_fn(&bump, TestComponent);
let _ = test_static_fn(&bump, TestComponent2);
}
// // Happiness! The VNodes are now allocated onto the bump vdom
// let _ = test_static_fn(&bump, TestComponent);
// let _ = test_static_fn(&bump, TestComponent2);
// }
}
+10 -5
View File
@@ -25,15 +25,20 @@ impl DebugRenderer {
mod tests {
use super::*;
use crate::prelude::*;
use crate::scope::FC;
#[test]
fn ensure_creation() -> Result<(), ()> {
let mut dom = VirtualDom::new(|ctx, props| {
//
ctx.render(html! { <div>"hello world" </div> })
});
#[derive(PartialEq)]
struct Creation {}
impl FC for Creation {
fn render(ctx: Context, props: &Self) -> DomTree {
ctx.render(html! { <div>"hello world" </div> })
}
}
let mut dom = VirtualDom::new_with_props(Creation {});
// dom.progress()?;
Ok(())
}
}
+9 -27
View File
@@ -53,23 +53,19 @@ use std::{cell::RefCell, cmp::Ordering, collections::VecDeque, rc::Rc};
/// The order of these re-entrances is stored in the DiffState itself. The DiffState comes pre-loaded with a set of components
/// that were modified by the eventtrigger. This prevents doubly evaluating components if they wereboth updated via
/// subscriptions and props changes.
pub struct DiffMachine<'a, 'b> {
pub struct DiffMachine<'a> {
pub change_list: EditMachine<'a>,
pub vdom: &'b VirtualDom,
pub cur_idx: ScopeIdx,
pub diffed: FxHashSet<ScopeIdx>,
pub need_to_diff: FxHashSet<ScopeIdx>,
}
impl<'a, 'b> DiffMachine<'a, 'b> {
pub fn new(vdom: &'b VirtualDom, bump: &'a Bump, idx: ScopeIdx) -> Self {
impl<'a> DiffMachine<'a> {
pub fn new(bump: &'a Bump) -> Self {
Self {
cur_idx: idx,
change_list: EditMachine::new(bump),
diffed: FxHashSet::default(),
need_to_diff: FxHashSet::default(),
vdom,
}
}
@@ -118,10 +114,10 @@ impl<'a, 'b> DiffMachine<'a, 'b> {
}
(VNode::Component(cold), VNode::Component(cnew)) => {
if cold.comp != cnew.comp {
// queue an event to mount this new component
return;
}
// if cold.comp != cnew.comp {
// // queue an event to mount this new component
// return;
// }
// compare props.... somehow.....
@@ -843,23 +839,9 @@ impl<'a, 'b> DiffMachine<'a, 'b> {
}
listeners.iter().enumerate().for_each(|(id, listener)| {
// if let Some(index) = self.current_idx {
self.change_list.new_event_listener(
listener.event,
listener.scope,
listener.id,
);
// } else {
// Don't panic
// Used for testing
// log::trace!("Failed to set listener, create was not called in the context of the virtual dom");
// }
self.change_list
.new_event_listener(listener.event, listener.scope, listener.id)
});
// for l in listeners {
// unsafe {
// registry.add(l);
// }
// }
for attr in attributes {
self.change_list
+24 -24
View File
@@ -183,30 +183,30 @@ mod use_reducer_def {
Decr,
}
#[allow(unused)]
static Example: FC<()> = |ctx, props| {
let (count, reduce) = use_reducer(
&ctx,
|| 0,
|count, action| match action {
Actions::Incr => *count += 1,
Actions::Decr => *count -= 1,
},
);
// #[allow(unused)]
// static Example: FC<()> = |ctx, props| {
// let (count, reduce) = use_reducer(
// &ctx,
// || 0,
// |count, action| match action {
// Actions::Incr => *count += 1,
// Actions::Decr => *count -= 1,
// },
// );
ctx.render(rsx! {
div {
h1 {"Count: {count}"}
button {
"Increment"
onclick: move |_| reduce(Actions::Incr)
}
button {
"Decrement"
onclick: move |_| reduce(Actions::Decr)
}
}
})
};
// ctx.render(rsx! {
// div {
// h1 {"Count: {count}"}
// button {
// "Increment"
// onclick: move |_| reduce(Actions::Incr)
// }
// button {
// "Decrement"
// onclick: move |_| reduce(Actions::Decr)
// }
// }
// })
// };
}
}
+5 -4
View File
@@ -106,7 +106,7 @@ pub(crate) mod innerlude {
// pub use nodes::iterables::IterableNodes;
/// This type alias is an internal way of abstracting over the static functions that represent components.
pub type FC<P> = for<'scope> fn(Context<'scope>, &'scope P) -> DomTree;
// pub type FC<P> = for<'scope> fn(Context<'scope>, &'scope P) -> DomTree;
mod fc2 {}
// pub type FC<'a, P: 'a> = for<'scope> fn(Context<'scope>, &'scope P) -> DomTree;
@@ -122,7 +122,8 @@ pub(crate) mod innerlude {
// Re-export the FC macro
pub use crate as dioxus;
pub use crate::nodebuilder as builder;
pub use dioxus_core_macro::{fc, html, rsx};
pub use dioxus_core_macro::{html, rsx};
// pub use dioxus_core_macro::{fc, html, rsx};
}
/// Re-export common types for ease of development use.
@@ -136,7 +137,7 @@ pub mod prelude {
// pub use nodes::iterables::IterableNodes;
/// This type alias is an internal way of abstracting over the static functions that represent components.
pub use crate::innerlude::FC;
// pub use crate::innerlude::FC;
// TODO @Jon, fix this
// hack the VNode type until VirtualNode is fixed in the macro crate
@@ -151,7 +152,7 @@ pub mod prelude {
pub use crate::nodebuilder as builder;
// pub use dioxus_core_macro::fc;
pub use dioxus_core_macro::format_args_f;
pub use dioxus_core_macro::{fc, html, rsx};
pub use dioxus_core_macro::{html, rsx};
pub use crate::component::ScopeIdx;
pub use crate::diff::DiffMachine;
+3 -1
View File
@@ -524,7 +524,9 @@ pub fn attr<'a>(name: &'static str, value: &'a str) -> Attribute<'a> {
// }
// }
pub fn virtual_child<'a, T>(_bump: &'a Bump, _props: T, _f: crate::innerlude::FC<T>) -> VNode<'a> {
// _f: crate::innerlude::FC<T>,
// _props: T
pub fn virtual_child<'a, T: crate::scope::FC>(ctx: &'a NodeCtx<'a>, p: T) -> VNode<'a> {
todo!()
// VNode::Component()
}
+43 -10
View File
@@ -296,8 +296,16 @@ mod vtext {
/// Virtual Components for custom user-defined components
/// Only supports the functional syntax
mod vcomponent {
use crate::innerlude::{Context, ScopeIdx, FC};
use std::{any::TypeId, cell::RefCell, marker::PhantomData, rc::Rc};
use crate::{
innerlude::{Context, ScopeIdx},
scope::FC,
};
use std::{
any::{Any, TypeId},
cell::RefCell,
marker::PhantomData,
rc::Rc,
};
use super::DomTree;
@@ -306,16 +314,18 @@ mod vcomponent {
#[derive(Debug)]
pub struct VComponent<'src> {
_p: PhantomData<&'src ()>,
pub(crate) props: Box<dyn std::any::Any>,
pub(crate) props_type: TypeId,
pub(crate) comp: *const (),
pub(crate) caller: Caller,
// pub(crate) props: Box<dyn std::any::Any>,
// pub(crate) props_type: TypeId,
// pub(crate) comp: *const (),
// pub(crate) caller: Caller,
pub(crate) comparator: Comparator,
// once a component gets mounted, its parent gets a stable address.
// this way we can carry the scope index from between renders
// genius, really!
pub assigned_scope: StableScopeAddres,
// pub assigned_scope: StableScopeAddres,
}
pub struct Comparator(Box<dyn Fn(&dyn Any) -> bool>);
// pub struct Comparator<'src>(&'src dyn Fn(&dyn Any) -> bool);
pub struct Caller(Box<dyn Fn(Context) -> DomTree>);
impl std::fmt::Debug for Caller {
@@ -324,12 +334,35 @@ mod vcomponent {
}
}
impl std::fmt::Debug for Comparator {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
todo!()
}
}
impl<'a> VComponent<'a> {
// use the type parameter on props creation and move it into a portable context
// this lets us keep scope generic *and* downcast its props when we need to:
// - perform comparisons when diffing (memoization)
// -
pub fn new<P>(comp: FC<P>, props: P) -> Self {
pub fn new<P: FC + 'static>(comp: P) -> Self {
todo!()
// let p = Rc::new(props);
// let props_comparator = move |new_props: &dyn Any| -> bool {
// new_props
// .downcast_ref::<P>()
// .map(|new_p| p.as_ref() == new_p)
// .unwrap_or_else(|| {
// log::debug!("downcasting failed, this receovered but SHOULD NOT FAIL");
// false
// })
// };
// Self {
// _p: PhantomData,
// comparator: Comparator(Box::new(props_comparator)),
// }
// let caller = move |ctx: Context| {
// let t = comp(ctx, &props);
// t
@@ -355,7 +388,7 @@ mod vcomponent {
// }
// }
todo!()
// todo!()
// Self {
// _p: PhantomData {},
// props,
+73 -77
View File
@@ -11,9 +11,9 @@ use std::{
ops::Deref,
};
pub trait Properties: PartialEq {}
// just for now
impl<T: PartialEq> Properties for T {}
pub trait FC: PartialEq {
fn render(ctx: Context, props: &Self) -> DomTree;
}
pub trait Scoped {
fn run(&mut self);
@@ -30,15 +30,13 @@ pub trait Scoped {
///
/// Scopes are allocated in a generational arena. As components are mounted/unmounted, they will replace slots of dead components.
/// The actual contents of the hooks, though, will be allocated with the standard allocator. These should not allocate as frequently.
pub struct Scope<P: Properties> {
pub struct Scope<P: FC> {
// Map to the parent
pub parent: Option<ScopeIdx>,
// our own index
pub myidx: ScopeIdx,
pub caller: FC<P>,
pub props: P,
// ==========================
@@ -63,8 +61,7 @@ pub struct Scope<P: Properties> {
// instead of having it as a trait method, we use a single function
// todo: do the unsafety magic stuff to erase the type of p
pub fn create_scoped<P: Properties + 'static>(
caller: FC<P>,
pub fn create_scoped<P: FC + 'static>(
props: P,
myidx: ScopeIdx,
parent: Option<ScopeIdx>,
@@ -90,7 +87,6 @@ pub fn create_scoped<P: Properties + 'static>(
myidx,
hook_arena,
hooks,
caller,
frames,
listeners,
parent,
@@ -98,7 +94,7 @@ pub fn create_scoped<P: Properties + 'static>(
})
}
impl<P: Properties + 'static> Scoped for Scope<P> {
impl<P: FC + 'static> Scoped for Scope<P> {
/// Create a new context and run the component with references from the Virtual Dom
/// This function downcasts the function pointer based on the stored props_type
///
@@ -125,7 +121,8 @@ impl<P: Properties + 'static> Scoped for Scope<P> {
// Note that the actual modification of the vnode head element occurs during this call
// let _: DomTree = caller(ctx, props);
let _: DomTree = (self.caller)(ctx, &self.props);
let _: DomTree = P::render(ctx, &self.props);
// let _: DomTree = (self.caller)(ctx, &self.props);
/*
SAFETY ALERT
@@ -264,90 +261,89 @@ mod tests {
use super::*;
use crate::prelude::*;
static ListenerTest: FC<()> = |ctx, props| {
ctx.render(html! {
<div onclick={|_| println!("Hell owlrld")}>
"hello"
</div>
})
};
// static ListenerTest: FC<()> = |ctx, props| {
// ctx.render(html! {
// <div onclick={|_| println!("Hell owlrld")}>
// "hello"
// </div>
// })
// };
#[test]
fn test_scope() {
let example: FC<()> = |ctx, props| {
use crate::builder::*;
ctx.render(|ctx| {
builder::ElementBuilder::new(ctx, "div")
.child(text("a"))
.finish()
})
};
#[derive(PartialEq)]
struct Example {}
impl FC for Example {
fn render(ctx: Context<'_>, _: &Self) -> DomTree {
use crate::builder::*;
ctx.render(|ctx| {
builder::ElementBuilder::new(ctx, "div")
.child(text("a"))
.finish()
})
}
}
let props = ();
let parent = None;
let mut nodes = generational_arena::Arena::new();
nodes.insert_with(|myidx| {
let scope = create_scoped(example, props, myidx, parent);
let scope = create_scoped(Example {}, myidx, None);
});
}
#[derive(Debug)]
struct ExampleProps<'src> {
name: &'src String,
}
#[derive(Debug)]
struct EmptyProps<'src> {
name: &'src String,
}
use crate::{builder::*, hooks::use_ref};
fn example_fc<'a>(ctx: Context<'a>, props: &'a EmptyProps) -> DomTree {
let (content, _): (&'a String, _) = crate::hooks::use_state(&ctx, || "abcd".to_string());
let childprops: ExampleProps<'a> = ExampleProps { name: content };
ctx.render(move |c| {
builder::ElementBuilder::new(c, "div")
.child(text(props.name))
.child(virtual_child::<ExampleProps>(
c.bump,
childprops,
child_example,
))
.finish()
})
#[derive(Debug, PartialEq)]
struct EmptyProps<'src> {
name: &'src String,
}
fn child_example<'b>(ctx: Context<'b>, props: &'b ExampleProps) -> DomTree {
ctx.render(move |ctx| {
builder::ElementBuilder::new(ctx, "div")
.child(text(props.name))
.finish()
})
impl FC for EmptyProps<'_> {
fn render(ctx: Context, props: &Self) -> DomTree {
let (content, _): (&String, _) = crate::hooks::use_state(&ctx, || "abcd".to_string());
let childprops: ExampleProps<'_> = ExampleProps { name: content };
todo!()
// ctx.render(move |c| {
// builder::ElementBuilder::new(c, "div")
// .child(text(props.name))
// .child(virtual_child(c, childprops))
// .finish()
// })
}
}
static CHILD: FC<ExampleProps> = |ctx, props: &'_ ExampleProps| {
ctx.render(move |ctx| {
builder::ElementBuilder::new(ctx, "div")
.child(text(props.name))
.finish()
})
};
#[derive(Debug, PartialEq)]
struct ExampleProps<'src> {
name: &'src String,
}
impl FC for ExampleProps<'_> {
fn render(ctx: Context, props: &Self) -> DomTree {
ctx.render(move |ctx| {
builder::ElementBuilder::new(ctx, "div")
.child(text(props.name))
.finish()
})
}
}
#[test]
fn test_borrowed_scope() {
let example: FC<EmptyProps> = |ctx, props| {
ctx.render(move |b| {
builder::ElementBuilder::new(b, "div")
.child(virtual_child(
b.bump,
ExampleProps { name: props.name },
CHILD,
))
.finish()
})
};
#[derive(Debug, PartialEq)]
struct Example {
name: String,
}
impl FC for Example {
fn render(ctx: Context, props: &Self) -> DomTree {
todo!()
// ctx.render(move |c| {
// builder::ElementBuilder::new(c, "div")
// .child(virtual_child(c, ExampleProps { name: &props.name }))
// .finish()
// })
}
}
let source_text = "abcd123".to_string();
let props = ExampleProps { name: &source_text };
+19 -18
View File
@@ -1,6 +1,6 @@
// use crate::{changelist::EditList, nodes::VNode};
use crate::{innerlude::*, scope::Properties};
use crate::{innerlude::*, scope::FC};
use crate::{
patch::Edit,
scope::{create_scoped, Scoped},
@@ -60,8 +60,9 @@ impl VirtualDom {
///
/// This means that the root component must either consumes its own context, or statics are used to generate the page.
/// The root component can access things like routing in its context.
pub fn new(root: FC<()>) -> Self {
Self::new_with_props(root, ())
pub fn new(root: impl Fn(Context) -> DomTree) -> Self {
todo!()
// Self::new_with_props(root)
}
/// Start a new VirtualDom instance with a dependent props.
@@ -69,13 +70,13 @@ impl VirtualDom {
///
/// This is useful when a component tree can be driven by external state (IE SSR) but it would be too expensive
/// to toss out the entire tree.
pub fn new_with_props<P: Properties + 'static>(root: FC<P>, root_props: P) -> Self {
let mut components = Arena::new();
pub fn new_with_props<P: FC + 'static>(root: P) -> Self {
// let mut components = Arena::new();
// Create a reference to the component in the arena
// Note: we are essentially running the "Mount" lifecycle event manually while the vdom doesnt yet exist
// This puts the dom in a usable state on creation, rather than being potentially invalid
let base_scope = components.insert_with(|id| create_scoped(root, root_props, id, None));
// let base_scope = components.insert_with(|id| create_scoped(root, root_props, id, None));
todo!()
// Self {
@@ -106,19 +107,16 @@ impl VirtualDom {
let b = Bump::new();
let mut diff_machine = DiffMachine::new(self, &b, self.base_scope);
let mut diff_machine = DiffMachine::new(&self.diff_bump);
// let mut diff_machine = DiffMachine::new(self, &self.diff_bump, self.base_scope);
todo!()
// let component = self.components.get(self.base_scope).unwrap();
// diff_machine.diff_node(component.old_frame(), component.new_frame());
// let edits = diff_machine.consume();
// todo!()
let component = self.components.get(self.base_scope).unwrap();
diff_machine.diff_node(component.old_frame(), component.new_frame());
let edits = diff_machine.consume();
// self.diff_bump = b;
// Ok(edits)
Ok(edits)
}
/// This method is the most sophisticated way of updating the virtual dom after an external event has been triggered.
@@ -187,6 +185,9 @@ impl VirtualDom {
}
}
// struct LockedEdits<'src> {
// edits:
// }
enum LifeCycleEvent {
// Mount {
// props: &dyn Properties,
// // f: FC<dyn Properties>,
// },
}