wip: rename fc to component
This commit is contained in:
1 parent
85ab8b91c6
commit
1e4a599d14
61 files changed
+524
-145
No files matched your search
@@ -23,7 +23,7 @@ criterion_group!(mbenches, create_rows);
|
||||
criterion_main!(mbenches);
|
||||
|
||||
fn create_rows(c: &mut Criterion) {
|
||||
static App: FC<()> = |cx, _| {
|
||||
static App: Component<()> = |cx, _| {
|
||||
let mut rng = SmallRng::from_entropy();
|
||||
let rows = (0..10_000_usize).map(|f| {
|
||||
let label = Label::new(&mut rng);
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use dioxus::prelude::*;
|
||||
use dioxus_core as dioxus;
|
||||
use dioxus_core_macro::*;
|
||||
use dioxus_html as dioxus_elements;
|
||||
|
||||
fn main() {
|
||||
let _ = VirtualDom::new(App);
|
||||
}
|
||||
|
||||
fn App(cx: Context, _props: &()) -> Element {
|
||||
//
|
||||
cx.render(rsx!(
|
||||
div {
|
||||
Child {}
|
||||
}
|
||||
))
|
||||
}
|
||||
|
||||
struct ChildProps<'a> {
|
||||
click_handler: EventHandler<'a>,
|
||||
}
|
||||
|
||||
fn Child(cx: Context, _props: &()) -> Element {
|
||||
//
|
||||
cx.render(rsx!(
|
||||
div {
|
||||
h1 {
|
||||
"Hello, World!"
|
||||
}
|
||||
}
|
||||
))
|
||||
}
|
||||
@@ -20,7 +20,7 @@ use dioxus_html as dioxus_elements;
|
||||
use rand::prelude::*;
|
||||
|
||||
fn main() {
|
||||
static App: FC<()> = |cx, _| {
|
||||
static App: Component<()> = |cx, _| {
|
||||
let mut rng = SmallRng::from_entropy();
|
||||
let rows = (0..10_000_usize).map(|f| {
|
||||
let label = Label::new(&mut rng);
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use dioxus::prelude::*;
|
||||
use dioxus_core as dioxus;
|
||||
use dioxus_core_macro::*;
|
||||
@@ -7,8 +9,8 @@ fn main() {
|
||||
let _ = VirtualDom::new(Parent);
|
||||
}
|
||||
|
||||
fn Parent(cx: Context, props: &()) -> Element {
|
||||
let value = cx.use_hook(|_| String::new(), |f| &*f);
|
||||
fn Parent(cx: Context, _props: &()) -> Element {
|
||||
let value = cx.use_hook(|_| String::new(), |f| f);
|
||||
|
||||
cx.render(rsx! {
|
||||
div {
|
||||
|
||||
@@ -32,19 +32,21 @@ pub(crate) mod innerlude {
|
||||
pub use crate::virtual_dom::*;
|
||||
|
||||
pub type Element = Option<VPortal>;
|
||||
pub type FC<P> = for<'a> fn(Context<'a>, &'a P) -> Element;
|
||||
pub type Component<P> = for<'a> fn(Context<'a>, &'a P) -> Element;
|
||||
}
|
||||
|
||||
pub use crate::innerlude::{
|
||||
Attribute, Context, DioxusElement, DomEdit, Element, ElementId, EventPriority, IntoVNode,
|
||||
LazyNodes, Listener, MountType, Mutations, NodeFactory, Properties, SchedulerMsg, ScopeId,
|
||||
UserEvent, VElement, VFragment, VNode, VirtualDom, FC,
|
||||
Attribute, Component, Context, DioxusElement, DomEdit, Element, ElementId, EventHandler,
|
||||
EventPriority, IntoVNode, LazyNodes, Listener, MountType, Mutations, NodeFactory, Properties,
|
||||
SchedulerMsg, ScopeId, UserEvent, VElement, VFragment, VNode, VirtualDom,
|
||||
};
|
||||
|
||||
pub mod prelude {
|
||||
pub use crate::component::{fc_to_builder, Fragment, Properties};
|
||||
pub use crate::innerlude::Context;
|
||||
pub use crate::innerlude::{DioxusElement, Element, LazyNodes, NodeFactory, Scope, FC};
|
||||
pub use crate::innerlude::{
|
||||
Component, DioxusElement, Element, EventHandler, LazyNodes, NodeFactory, Scope,
|
||||
};
|
||||
pub use crate::nodes::VNode;
|
||||
pub use crate::VirtualDom;
|
||||
}
|
||||
|
||||
@@ -338,9 +338,32 @@ pub struct Listener<'bump> {
|
||||
pub event: &'static str,
|
||||
|
||||
/// The actual callback that the user specified
|
||||
#[allow(clippy::type_complexity)]
|
||||
pub(crate) callback:
|
||||
RefCell<Option<BumpBox<'bump, dyn FnMut(std::sync::Arc<dyn Any + Send + Sync>) + 'bump>>>,
|
||||
pub(crate) callback: EventHandler<'bump>,
|
||||
}
|
||||
|
||||
pub struct EventHandler<'bump> {
|
||||
pub callback: &'bump RefCell<Option<ListenerCallback<'bump>>>,
|
||||
}
|
||||
|
||||
impl EventHandler<'_> {
|
||||
pub fn call(&self, event: Arc<dyn Any + Send + Sync>) {
|
||||
if let Some(callback) = self.callback.borrow_mut().as_mut() {
|
||||
callback(event);
|
||||
}
|
||||
}
|
||||
pub fn release(&self) {
|
||||
self.callback.replace(None);
|
||||
}
|
||||
}
|
||||
type ListenerCallback<'bump> = BumpBox<'bump, dyn FnMut(Arc<dyn Any + Send + Sync>) + 'bump>;
|
||||
|
||||
impl Copy for EventHandler<'_> {}
|
||||
impl Clone for EventHandler<'_> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
callback: self.callback,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A cached node is a "pointer" to a "rendered" node in a particular scope
|
||||
@@ -608,15 +631,11 @@ impl<'a> NodeFactory<'a> {
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn listener(
|
||||
self,
|
||||
event: &'static str,
|
||||
callback: BumpBox<'a, dyn FnMut(Arc<dyn Any + Send + Sync>) + 'a>,
|
||||
) -> Listener<'a> {
|
||||
pub fn listener(self, event: &'static str, callback: EventHandler<'a>) -> Listener<'a> {
|
||||
Listener {
|
||||
event,
|
||||
mounted_node: Cell::new(None),
|
||||
callback: RefCell::new(Some(callback)),
|
||||
callback,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -274,6 +274,7 @@ impl Scope {
|
||||
let mut search_parent = self.parent_scope;
|
||||
|
||||
while let Some(parent_ptr) = search_parent {
|
||||
// safety: all parent pointers are valid thanks to the bump arena
|
||||
let parent = unsafe { &*parent_ptr };
|
||||
if let Some(shared) = parent.shared_contexts.borrow().get(&TypeId::of::<T>()) {
|
||||
return Some(shared.clone().downcast::<T>().unwrap());
|
||||
|
||||
@@ -281,30 +281,30 @@ 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) {
|
||||
let scope = self.get_scope(scope_id).unwrap();
|
||||
if let Some(scope) = self.get_scope(scope_id) {
|
||||
let mut items = scope.items.borrow_mut();
|
||||
|
||||
let mut items = scope.items.borrow_mut();
|
||||
// make sure we drop all borrowed props manually to guarantee that their drop implementation is called before we
|
||||
// run the hooks (which hold an &mut Reference)
|
||||
// recursively call ensure_drop_safety on all children
|
||||
items.borrowed_props.drain(..).for_each(|comp| {
|
||||
let scope_id = comp
|
||||
.associated_scope
|
||||
.get()
|
||||
.expect("VComponents should be associated with a valid Scope");
|
||||
|
||||
// make sure we drop all borrowed props manually to guarantee that their drop implementation is called before we
|
||||
// run the hooks (which hold an &mut Reference)
|
||||
// recursively call ensure_drop_safety on all children
|
||||
items.borrowed_props.drain(..).for_each(|comp| {
|
||||
let scope_id = comp
|
||||
.associated_scope
|
||||
.get()
|
||||
.expect("VComponents should be associated with a valid Scope");
|
||||
self.ensure_drop_safety(&scope_id);
|
||||
|
||||
self.ensure_drop_safety(&scope_id);
|
||||
let mut drop_props = comp.drop_props.borrow_mut().take().unwrap();
|
||||
drop_props();
|
||||
});
|
||||
|
||||
let mut drop_props = comp.drop_props.borrow_mut().take().unwrap();
|
||||
drop_props();
|
||||
});
|
||||
|
||||
// Now that all the references are gone, we can safely drop our own references in our listeners.
|
||||
items
|
||||
.listeners
|
||||
.drain(..)
|
||||
.for_each(|listener| drop(listener.callback.borrow_mut().take()));
|
||||
// Now that all the references are gone, we can safely drop our own references in our listeners.
|
||||
items
|
||||
.listeners
|
||||
.drain(..)
|
||||
.for_each(|listener| drop(listener.callback.callback.borrow_mut().take()));
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn run_scope(&self, id: &ScopeId) -> bool {
|
||||
@@ -375,7 +375,7 @@ impl ScopeArena {
|
||||
//
|
||||
for listener in real_el.listeners.borrow().iter() {
|
||||
if listener.event == event.name {
|
||||
let mut cb = listener.callback.borrow_mut();
|
||||
let mut cb = listener.callback.callback.borrow_mut();
|
||||
if let Some(cb) = cb.as_mut() {
|
||||
(cb)(event.data.clone());
|
||||
}
|
||||
|
||||
@@ -140,7 +140,7 @@ impl VirtualDom {
|
||||
/// ```
|
||||
///
|
||||
/// Note: the VirtualDOM is not progressed, you must either "run_with_deadline" or use "rebuild" to progress it.
|
||||
pub fn new(root: FC<()>) -> Self {
|
||||
pub fn new(root: Component<()>) -> Self {
|
||||
Self::new_with_props(root, ())
|
||||
}
|
||||
|
||||
@@ -174,7 +174,7 @@ impl VirtualDom {
|
||||
/// let mut dom = VirtualDom::new_with_props(Example, SomeProps { name: "jane" });
|
||||
/// let mutations = dom.rebuild();
|
||||
/// ```
|
||||
pub fn new_with_props<P: 'static>(root: FC<P>, root_props: P) -> Self {
|
||||
pub fn new_with_props<P: 'static>(root: Component<P>, root_props: P) -> Self {
|
||||
let (sender, receiver) = futures_channel::mpsc::unbounded::<SchedulerMsg>();
|
||||
Self::new_with_props_and_scheduler(root, root_props, sender, receiver)
|
||||
}
|
||||
@@ -184,7 +184,7 @@ impl VirtualDom {
|
||||
/// This is useful when the VirtualDom must be driven from outside a thread and it doesn't make sense to wait for the
|
||||
/// VirtualDom to be created just to retrieve its channel receiver.
|
||||
pub fn new_with_props_and_scheduler<P: 'static>(
|
||||
root: FC<P>,
|
||||
root: Component<P>,
|
||||
root_props: P,
|
||||
sender: UnboundedSender<SchedulerMsg>,
|
||||
receiver: UnboundedReceiver<SchedulerMsg>,
|
||||
|
||||
@@ -10,7 +10,7 @@ fn test_borrowed_state() {
|
||||
let _ = VirtualDom::new(Parent);
|
||||
}
|
||||
|
||||
fn Parent(cx: Context, props: &()) -> Element {
|
||||
fn Parent(cx: Context, _props: &()) -> Element {
|
||||
let value = cx.use_hook(|_| String::new(), |f| &*f);
|
||||
|
||||
cx.render(rsx! {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#![allow(unused, non_upper_case_globals)]
|
||||
#![allow(unused, non_upper_case_globals, non_snake_case)]
|
||||
|
||||
//! Prove that the dom works normally through virtualdom methods.
|
||||
//!
|
||||
@@ -13,7 +13,7 @@ use dioxus_html as dioxus_elements;
|
||||
mod test_logging;
|
||||
use DomEdit::*;
|
||||
|
||||
fn new_dom<P: 'static + Send>(app: FC<P>, props: P) -> VirtualDom {
|
||||
fn new_dom<P: 'static + Send>(app: Component<P>, props: P) -> VirtualDom {
|
||||
const IS_LOGGING_ENABLED: bool = false;
|
||||
test_logging::set_up_logging(IS_LOGGING_ENABLED);
|
||||
VirtualDom::new_with_props(app, props)
|
||||
@@ -21,7 +21,7 @@ fn new_dom<P: 'static + Send>(app: FC<P>, props: P) -> VirtualDom {
|
||||
|
||||
#[test]
|
||||
fn test_original_diff() {
|
||||
static APP: FC<()> = |cx, props| {
|
||||
static APP: Component<()> = |cx, props| {
|
||||
cx.render(rsx! {
|
||||
div {
|
||||
div {
|
||||
@@ -57,7 +57,7 @@ fn test_original_diff() {
|
||||
|
||||
#[test]
|
||||
fn create() {
|
||||
static APP: FC<()> = |cx, props| {
|
||||
static APP: Component<()> = |cx, props| {
|
||||
cx.render(rsx! {
|
||||
div {
|
||||
div {
|
||||
@@ -120,7 +120,7 @@ fn create() {
|
||||
|
||||
#[test]
|
||||
fn create_list() {
|
||||
static APP: FC<()> = |cx, props| {
|
||||
static APP: Component<()> = |cx, props| {
|
||||
cx.render(rsx! {
|
||||
{(0..3).map(|f| rsx!{ div {
|
||||
"hello"
|
||||
@@ -169,7 +169,7 @@ fn create_list() {
|
||||
|
||||
#[test]
|
||||
fn create_simple() {
|
||||
static APP: FC<()> = |cx, props| {
|
||||
static APP: Component<()> = |cx, props| {
|
||||
cx.render(rsx! {
|
||||
div {}
|
||||
div {}
|
||||
@@ -207,7 +207,7 @@ fn create_simple() {
|
||||
}
|
||||
#[test]
|
||||
fn create_components() {
|
||||
static App: FC<()> = |cx, props| {
|
||||
static App: Component<()> = |cx, props| {
|
||||
cx.render(rsx! {
|
||||
Child { "abc1" }
|
||||
Child { "abc2" }
|
||||
@@ -273,7 +273,7 @@ fn create_components() {
|
||||
}
|
||||
#[test]
|
||||
fn anchors() {
|
||||
static App: FC<()> = |cx, props| {
|
||||
static App: Component<()> = |cx, props| {
|
||||
cx.render(rsx! {
|
||||
{true.then(|| rsx!{ div { "hello" } })}
|
||||
{false.then(|| rsx!{ div { "goodbye" } })}
|
||||
|
||||
@@ -22,7 +22,7 @@ fn manual_diffing() {
|
||||
value: Shared<&'static str>,
|
||||
}
|
||||
|
||||
static App: FC<AppProps> = |cx, props| {
|
||||
static App: Component<AppProps> = |cx, props| {
|
||||
let val = props.value.lock().unwrap();
|
||||
cx.render(rsx! { div { "{val}" } })
|
||||
};
|
||||
@@ -46,7 +46,7 @@ fn manual_diffing() {
|
||||
|
||||
#[test]
|
||||
fn events_generate() {
|
||||
static App: FC<()> = |cx, _| {
|
||||
static App: Component<()> = |cx, _| {
|
||||
let mut count = use_state(cx, || 0);
|
||||
|
||||
let inner = match *count {
|
||||
@@ -105,7 +105,7 @@ fn events_generate() {
|
||||
|
||||
#[test]
|
||||
fn components_generate() {
|
||||
static App: FC<()> = |cx, _| {
|
||||
static App: Component<()> = |cx, _| {
|
||||
let mut render_phase = use_state(cx, || 0);
|
||||
render_phase += 1;
|
||||
|
||||
@@ -122,7 +122,7 @@ fn components_generate() {
|
||||
})
|
||||
};
|
||||
|
||||
static Child: FC<()> = |cx, _| {
|
||||
static Child: Component<()> = |cx, _| {
|
||||
cx.render(rsx! {
|
||||
h1 {}
|
||||
})
|
||||
@@ -222,7 +222,7 @@ fn components_generate() {
|
||||
#[test]
|
||||
fn component_swap() {
|
||||
// simple_logger::init();
|
||||
static App: FC<()> = |cx, _| {
|
||||
static App: Component<()> = |cx, _| {
|
||||
let mut render_phase = use_state(cx, || 0);
|
||||
render_phase += 1;
|
||||
|
||||
@@ -261,7 +261,7 @@ fn component_swap() {
|
||||
})
|
||||
};
|
||||
|
||||
static NavBar: FC<()> = |cx, _| {
|
||||
static NavBar: Component<()> = |cx, _| {
|
||||
println!("running navbar");
|
||||
cx.render(rsx! {
|
||||
h1 {
|
||||
@@ -271,7 +271,7 @@ fn component_swap() {
|
||||
})
|
||||
};
|
||||
|
||||
static NavLink: FC<()> = |cx, _| {
|
||||
static NavLink: Component<()> = |cx, _| {
|
||||
println!("running navlink");
|
||||
cx.render(rsx! {
|
||||
h1 {
|
||||
@@ -280,7 +280,7 @@ fn component_swap() {
|
||||
})
|
||||
};
|
||||
|
||||
static Dashboard: FC<()> = |cx, _| {
|
||||
static Dashboard: Component<()> = |cx, _| {
|
||||
println!("running dashboard");
|
||||
cx.render(rsx! {
|
||||
div {
|
||||
@@ -289,7 +289,7 @@ fn component_swap() {
|
||||
})
|
||||
};
|
||||
|
||||
static Results: FC<()> = |cx, _| {
|
||||
static Results: Component<()> = |cx, _| {
|
||||
println!("running results");
|
||||
cx.render(rsx! {
|
||||
div {
|
||||
|
||||
@@ -13,12 +13,12 @@ mod test_logging;
|
||||
fn shared_state_test() {
|
||||
struct MySharedState(&'static str);
|
||||
|
||||
static App: FC<()> = |cx, props| {
|
||||
static App: Component<()> = |cx, props| {
|
||||
cx.provide_state(MySharedState("world!"));
|
||||
cx.render(rsx!(Child {}))
|
||||
};
|
||||
|
||||
static Child: FC<()> = |cx, props| {
|
||||
static Child: Component<()> = |cx, props| {
|
||||
let shared = cx.consume_state::<MySharedState>()?;
|
||||
cx.render(rsx!("Hello, {shared.0}"))
|
||||
};
|
||||
|
||||
@@ -19,7 +19,7 @@ use DomEdit::*;
|
||||
|
||||
#[test]
|
||||
fn app_runs() {
|
||||
static App: FC<()> = |cx, props| rsx!(cx, div{"hello"} );
|
||||
static App: Component<()> = |cx, props| rsx!(cx, div{"hello"} );
|
||||
|
||||
let mut vdom = VirtualDom::new(App);
|
||||
let edits = vdom.rebuild();
|
||||
@@ -27,7 +27,7 @@ fn app_runs() {
|
||||
|
||||
#[test]
|
||||
fn fragments_work() {
|
||||
static App: FC<()> = |cx, props| {
|
||||
static App: Component<()> = |cx, props| {
|
||||
cx.render(rsx!(
|
||||
div{"hello"}
|
||||
div{"goodbye"}
|
||||
@@ -41,7 +41,7 @@ fn fragments_work() {
|
||||
|
||||
#[test]
|
||||
fn lists_work() {
|
||||
static App: FC<()> = |cx, props| {
|
||||
static App: Component<()> = |cx, props| {
|
||||
cx.render(rsx!(
|
||||
h1 {"hello"}
|
||||
{(0..6).map(|f| rsx!(span{ "{f}" }))}
|
||||
@@ -54,7 +54,7 @@ fn lists_work() {
|
||||
|
||||
#[test]
|
||||
fn conditional_rendering() {
|
||||
static App: FC<()> = |cx, props| {
|
||||
static App: Component<()> = |cx, props| {
|
||||
cx.render(rsx!(
|
||||
h1 {"hello"}
|
||||
{true.then(|| rsx!(span{ "a" }))}
|
||||
@@ -87,13 +87,13 @@ fn conditional_rendering() {
|
||||
|
||||
#[test]
|
||||
fn child_components() {
|
||||
static App: FC<()> = |cx, props| {
|
||||
static App: Component<()> = |cx, props| {
|
||||
cx.render(rsx!(
|
||||
{true.then(|| rsx!(Child { }))}
|
||||
{false.then(|| rsx!(Child { }))}
|
||||
))
|
||||
};
|
||||
static Child: FC<()> = |cx, props| {
|
||||
static Child: Component<()> = |cx, props| {
|
||||
cx.render(rsx!(
|
||||
h1 {"hello"}
|
||||
h1 {"goodbye"}
|
||||
|
||||
Reference in new issue
Block a user