polish: move debugging virtualdom into its own feature
This commit is contained in:
1 parent
9c1343610b
commit
d1b294fff0
6 files changed
+182
-173
No files matched your search
@@ -55,6 +55,7 @@ dioxus-core-macro = { path = "../core-macro", version = "0.1.2" }
|
|||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
serialize = ["serde", "serde_repr"]
|
serialize = ["serde", "serde_repr"]
|
||||||
|
debug_vdom = []
|
||||||
|
|
||||||
[[bench]]
|
[[bench]]
|
||||||
name = "create"
|
name = "create"
|
||||||
|
|||||||
@@ -0,0 +1,128 @@
|
|||||||
|
use crate::{innerlude::ScopeInner, virtual_dom::VirtualDom, VNode};
|
||||||
|
|
||||||
|
impl std::fmt::Display for VirtualDom {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
let base = self.base_scope();
|
||||||
|
let root = base.root_node();
|
||||||
|
|
||||||
|
let renderer = ScopeRenderer {
|
||||||
|
show_fragments: false,
|
||||||
|
skip_components: false,
|
||||||
|
|
||||||
|
_scope: base,
|
||||||
|
_pre_render: false,
|
||||||
|
_newline: true,
|
||||||
|
_indent: true,
|
||||||
|
_max_depth: usize::MAX,
|
||||||
|
};
|
||||||
|
|
||||||
|
renderer.render(self, root, f, 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// render the scope to a string using the rsx! syntax
|
||||||
|
pub(crate) struct ScopeRenderer<'a> {
|
||||||
|
pub skip_components: bool,
|
||||||
|
pub show_fragments: bool,
|
||||||
|
pub _scope: &'a ScopeInner,
|
||||||
|
pub _pre_render: bool,
|
||||||
|
pub _newline: bool,
|
||||||
|
pub _indent: bool,
|
||||||
|
pub _max_depth: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
// this is more or less a debug tool, but it'll render the entire tree to the terminal
|
||||||
|
impl<'a> ScopeRenderer<'a> {
|
||||||
|
pub fn render(
|
||||||
|
&self,
|
||||||
|
vdom: &VirtualDom,
|
||||||
|
node: &VNode,
|
||||||
|
f: &mut std::fmt::Formatter,
|
||||||
|
il: u16,
|
||||||
|
) -> std::fmt::Result {
|
||||||
|
const INDENT: &str = " ";
|
||||||
|
let write_indent = |_f: &mut std::fmt::Formatter, le| {
|
||||||
|
for _ in 0..le {
|
||||||
|
write!(_f, "{}", INDENT).unwrap();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match &node {
|
||||||
|
VNode::Text(text) => {
|
||||||
|
write_indent(f, il);
|
||||||
|
writeln!(f, "\"{}\"", text.text)?
|
||||||
|
}
|
||||||
|
VNode::Anchor(_anchor) => {
|
||||||
|
write_indent(f, il);
|
||||||
|
writeln!(f, "Anchor {{}}")?;
|
||||||
|
}
|
||||||
|
VNode::Element(el) => {
|
||||||
|
write_indent(f, il);
|
||||||
|
writeln!(f, "{} {{", el.tag_name)?;
|
||||||
|
// write!(f, "element: {}", el.tag_name)?;
|
||||||
|
let mut attr_iter = el.attributes.iter().peekable();
|
||||||
|
|
||||||
|
while let Some(attr) = attr_iter.next() {
|
||||||
|
match attr.namespace {
|
||||||
|
None => {
|
||||||
|
//
|
||||||
|
write_indent(f, il + 1);
|
||||||
|
writeln!(f, "{}: \"{}\"", attr.name, attr.value)?
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(ns) => {
|
||||||
|
// write the opening tag
|
||||||
|
write_indent(f, il + 1);
|
||||||
|
write!(f, " {}:\"", ns)?;
|
||||||
|
let mut cur_ns_el = attr;
|
||||||
|
'ns_parse: loop {
|
||||||
|
write!(f, "{}:{};", cur_ns_el.name, cur_ns_el.value)?;
|
||||||
|
match attr_iter.peek() {
|
||||||
|
Some(next_attr) if next_attr.namespace == Some(ns) => {
|
||||||
|
cur_ns_el = attr_iter.next().unwrap();
|
||||||
|
}
|
||||||
|
_ => break 'ns_parse,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// write the closing tag
|
||||||
|
write!(f, "\"")?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for child in el.children {
|
||||||
|
self.render(vdom, child, f, il + 1)?;
|
||||||
|
}
|
||||||
|
write_indent(f, il);
|
||||||
|
|
||||||
|
writeln!(f, "}}")?;
|
||||||
|
}
|
||||||
|
VNode::Fragment(frag) => {
|
||||||
|
if self.show_fragments {
|
||||||
|
write_indent(f, il);
|
||||||
|
writeln!(f, "Fragment {{")?;
|
||||||
|
for child in frag.children {
|
||||||
|
self.render(vdom, child, f, il + 1)?;
|
||||||
|
}
|
||||||
|
write_indent(f, il);
|
||||||
|
writeln!(f, "}}")?;
|
||||||
|
} else {
|
||||||
|
for child in frag.children {
|
||||||
|
self.render(vdom, child, f, il)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
VNode::Component(vcomp) => {
|
||||||
|
let idx = vcomp.associated_scope.get().unwrap();
|
||||||
|
if !self.skip_components {
|
||||||
|
let new_node = vdom.get_scope(idx).unwrap().root_node();
|
||||||
|
self.render(vdom, new_node, f, il)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
VNode::Suspended { .. } => {
|
||||||
|
// we can't do anything with suspended nodes
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -34,6 +34,9 @@ pub mod threadsafe;
|
|||||||
pub mod util;
|
pub mod util;
|
||||||
pub mod virtual_dom;
|
pub mod virtual_dom;
|
||||||
|
|
||||||
|
#[cfg(feature = "debug_vdom")]
|
||||||
|
pub mod debug_dom;
|
||||||
|
|
||||||
pub(crate) mod innerlude {
|
pub(crate) mod innerlude {
|
||||||
pub(crate) use crate::bumpframe::*;
|
pub(crate) use crate::bumpframe::*;
|
||||||
pub(crate) use crate::childiter::*;
|
pub(crate) use crate::childiter::*;
|
||||||
|
|||||||
+29
-31
@@ -173,6 +173,29 @@ impl<'src> VNode<'src> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Debug for VNode<'_> {
|
||||||
|
fn fmt(&self, s: &mut Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
|
||||||
|
match &self {
|
||||||
|
VNode::Element(el) => s
|
||||||
|
.debug_struct("VElement")
|
||||||
|
.field("name", &el.tag_name)
|
||||||
|
.field("key", &el.key)
|
||||||
|
.finish(),
|
||||||
|
|
||||||
|
VNode::Text(t) => write!(s, "VText {{ text: {} }}", t.text),
|
||||||
|
VNode::Anchor(_) => write!(s, "VAnchor"),
|
||||||
|
|
||||||
|
VNode::Fragment(frag) => write!(s, "VFragment {{ children: {:?} }}", frag.children),
|
||||||
|
VNode::Suspended { .. } => write!(s, "VSuspended"),
|
||||||
|
VNode::Component(comp) => write!(
|
||||||
|
s,
|
||||||
|
"VComponent {{ fc: {:?}, children: {:?} }}",
|
||||||
|
comp.user_fc, comp.children
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A placeholder node only generated when Fragments don't have any children.
|
/// A placeholder node only generated when Fragments don't have any children.
|
||||||
pub struct VAnchor {
|
pub struct VAnchor {
|
||||||
pub dom_id: Cell<Option<ElementId>>,
|
pub dom_id: Cell<Option<ElementId>>,
|
||||||
@@ -611,6 +634,12 @@ impl<'a> NodeFactory<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Debug for NodeFactory<'_> {
|
||||||
|
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Trait implementations for use in the rsx! and html! macros.
|
/// Trait implementations for use in the rsx! and html! macros.
|
||||||
///
|
///
|
||||||
/// ## Details
|
/// ## Details
|
||||||
@@ -668,7 +697,6 @@ impl<'a> IntoVNode<'a> for Option<VNode<'a>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> IntoVNode<'a> for Option<LazyNodes<'a, '_>> {
|
impl<'a> IntoVNode<'a> for Option<LazyNodes<'a, '_>> {
|
||||||
// impl<'a> IntoVNode<'a> for Option<Box<dyn FnOnce(NodeFactory<'a>) -> VNode<'a> + '_>> {
|
|
||||||
fn into_vnode(self, cx: NodeFactory<'a>) -> VNode<'a> {
|
fn into_vnode(self, cx: NodeFactory<'a>) -> VNode<'a> {
|
||||||
match self {
|
match self {
|
||||||
Some(lazy) => lazy.call(cx),
|
Some(lazy) => lazy.call(cx),
|
||||||
@@ -682,7 +710,6 @@ impl<'a> IntoVNode<'a> for Option<LazyNodes<'a, '_>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> IntoVNode<'a> for LazyNodes<'a, '_> {
|
impl<'a> IntoVNode<'a> for LazyNodes<'a, '_> {
|
||||||
// impl<'a> IntoVNode<'a> for Box<dyn FnOnce(NodeFactory<'a>) -> VNode<'a> + '_> {
|
|
||||||
fn into_vnode(self, cx: NodeFactory<'a>) -> VNode<'a> {
|
fn into_vnode(self, cx: NodeFactory<'a>) -> VNode<'a> {
|
||||||
self.call(cx)
|
self.call(cx)
|
||||||
}
|
}
|
||||||
@@ -699,32 +726,3 @@ impl IntoVNode<'_> for Arguments<'_> {
|
|||||||
cx.text(self)
|
cx.text(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Debug for NodeFactory<'_> {
|
|
||||||
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for VNode<'_> {
|
|
||||||
fn fmt(&self, s: &mut Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
|
|
||||||
match &self {
|
|
||||||
VNode::Element(el) => s
|
|
||||||
.debug_struct("VElement")
|
|
||||||
.field("name", &el.tag_name)
|
|
||||||
.field("key", &el.key)
|
|
||||||
.finish(),
|
|
||||||
|
|
||||||
VNode::Text(t) => write!(s, "VText {{ text: {} }}", t.text),
|
|
||||||
VNode::Anchor(_) => write!(s, "VAnchor"),
|
|
||||||
|
|
||||||
VNode::Fragment(frag) => write!(s, "VFragment {{ children: {:?} }}", frag.children),
|
|
||||||
VNode::Suspended { .. } => write!(s, "VSuspended"),
|
|
||||||
VNode::Component(comp) => write!(
|
|
||||||
s,
|
|
||||||
"VComponent {{ fc: {:?}, children: {:?} }}",
|
|
||||||
comp.user_fc, comp.children
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -358,110 +358,3 @@ impl ScopeInner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// render the scope to a string using the rsx! syntax
|
|
||||||
pub(crate) struct ScopeRenderer<'a> {
|
|
||||||
pub skip_components: bool,
|
|
||||||
pub show_fragments: bool,
|
|
||||||
pub _scope: &'a ScopeInner,
|
|
||||||
pub _pre_render: bool,
|
|
||||||
pub _newline: bool,
|
|
||||||
pub _indent: bool,
|
|
||||||
pub _max_depth: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
// this is more or less a debug tool, but it'll render the entire tree to the terminal
|
|
||||||
impl<'a> ScopeRenderer<'a> {
|
|
||||||
pub fn render(
|
|
||||||
&self,
|
|
||||||
vdom: &VirtualDom,
|
|
||||||
node: &VNode,
|
|
||||||
f: &mut std::fmt::Formatter,
|
|
||||||
il: u16,
|
|
||||||
) -> std::fmt::Result {
|
|
||||||
const INDENT: &str = " ";
|
|
||||||
let write_indent = |_f: &mut std::fmt::Formatter, le| {
|
|
||||||
for _ in 0..le {
|
|
||||||
write!(_f, "{}", INDENT).unwrap();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
match &node {
|
|
||||||
VNode::Text(text) => {
|
|
||||||
write_indent(f, il);
|
|
||||||
writeln!(f, "\"{}\"", text.text)?
|
|
||||||
}
|
|
||||||
VNode::Anchor(_anchor) => {
|
|
||||||
write_indent(f, il);
|
|
||||||
writeln!(f, "Anchor {{}}")?;
|
|
||||||
}
|
|
||||||
VNode::Element(el) => {
|
|
||||||
write_indent(f, il);
|
|
||||||
writeln!(f, "{} {{", el.tag_name)?;
|
|
||||||
// write!(f, "element: {}", el.tag_name)?;
|
|
||||||
let mut attr_iter = el.attributes.iter().peekable();
|
|
||||||
|
|
||||||
while let Some(attr) = attr_iter.next() {
|
|
||||||
match attr.namespace {
|
|
||||||
None => {
|
|
||||||
//
|
|
||||||
write_indent(f, il + 1);
|
|
||||||
writeln!(f, "{}: \"{}\"", attr.name, attr.value)?
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(ns) => {
|
|
||||||
// write the opening tag
|
|
||||||
write_indent(f, il + 1);
|
|
||||||
write!(f, " {}:\"", ns)?;
|
|
||||||
let mut cur_ns_el = attr;
|
|
||||||
'ns_parse: loop {
|
|
||||||
write!(f, "{}:{};", cur_ns_el.name, cur_ns_el.value)?;
|
|
||||||
match attr_iter.peek() {
|
|
||||||
Some(next_attr) if next_attr.namespace == Some(ns) => {
|
|
||||||
cur_ns_el = attr_iter.next().unwrap();
|
|
||||||
}
|
|
||||||
_ => break 'ns_parse,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// write the closing tag
|
|
||||||
write!(f, "\"")?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for child in el.children {
|
|
||||||
self.render(vdom, child, f, il + 1)?;
|
|
||||||
}
|
|
||||||
write_indent(f, il);
|
|
||||||
|
|
||||||
writeln!(f, "}}")?;
|
|
||||||
}
|
|
||||||
VNode::Fragment(frag) => {
|
|
||||||
if self.show_fragments {
|
|
||||||
write_indent(f, il);
|
|
||||||
writeln!(f, "Fragment {{")?;
|
|
||||||
for child in frag.children {
|
|
||||||
self.render(vdom, child, f, il + 1)?;
|
|
||||||
}
|
|
||||||
write_indent(f, il);
|
|
||||||
writeln!(f, "}}")?;
|
|
||||||
} else {
|
|
||||||
for child in frag.children {
|
|
||||||
self.render(vdom, child, f, il)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
VNode::Component(vcomp) => {
|
|
||||||
let idx = vcomp.associated_scope.get().unwrap();
|
|
||||||
if !self.skip_components {
|
|
||||||
let new_node = vdom.get_scope(idx).unwrap().root_node();
|
|
||||||
self.render(vdom, new_node, f, il)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
VNode::Suspended { .. } => {
|
|
||||||
// we can't do anything with suspended nodes
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -368,44 +368,30 @@ impl VirtualDom {
|
|||||||
use futures_util::StreamExt;
|
use futures_util::StreamExt;
|
||||||
|
|
||||||
// Wait for any new events if we have nothing to do
|
// Wait for any new events if we have nothing to do
|
||||||
todo!("wait for work without select macro")
|
|
||||||
|
|
||||||
// futures_util::select! {
|
let tasks_fut = self.scheduler.async_tasks.next();
|
||||||
// _ = self.scheduler.async_tasks.next() => {}
|
let scheduler_fut = self.scheduler.receiver.next();
|
||||||
// msg = self.scheduler.receiver.next() => {
|
|
||||||
// match msg.unwrap() {
|
|
||||||
// SchedulerMsg::Task(t) => {
|
|
||||||
// self.scheduler.handle_task(t);
|
|
||||||
// },
|
|
||||||
// SchedulerMsg::Immediate(im) => {
|
|
||||||
// self.scheduler.dirty_scopes.insert(im);
|
|
||||||
// }
|
|
||||||
// SchedulerMsg::UiEvent(evt) => {
|
|
||||||
// self.scheduler.ui_events.push_back(evt);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// },
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Display for VirtualDom {
|
use futures_util::future::{select, Either};
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
match select(tasks_fut, scheduler_fut).await {
|
||||||
let base = self.base_scope();
|
// poll the internal futures
|
||||||
let root = base.root_node();
|
Either::Left((_id, _)) => {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
let renderer = ScopeRenderer {
|
// wait for an external event
|
||||||
show_fragments: false,
|
Either::Right((msg, _)) => match msg.unwrap() {
|
||||||
skip_components: false,
|
SchedulerMsg::Task(t) => {
|
||||||
|
self.scheduler.handle_task(t);
|
||||||
_scope: base,
|
}
|
||||||
_pre_render: false,
|
SchedulerMsg::Immediate(im) => {
|
||||||
_newline: true,
|
self.scheduler.dirty_scopes.insert(im);
|
||||||
_indent: true,
|
}
|
||||||
_max_depth: usize::MAX,
|
SchedulerMsg::UiEvent(evt) => {
|
||||||
};
|
self.scheduler.ui_events.push_back(evt);
|
||||||
|
}
|
||||||
renderer.render(self, root, f, 0)
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user