document state trait

This commit is contained in:
Evan Almloff committed 2023-02-11 17:43:08 -06:00
1 parent d8da8e4275
commit fe3ad8ebe9
11 files changed
+58 -55

No files matched your search

+2 -2
View File
@@ -3,7 +3,7 @@ use std::hash::BuildHasherDefault;
pub use node_ref::NodeMask;
pub use passes::AnyMapLike;
pub use passes::{Dependancy, Pass, TypeErasedPass};
pub use passes::{Dependancy, State, TypeErasedPass};
pub use real_dom::{NodeMut, NodeRef, RealDom};
use rustc_hash::FxHasher;
pub use tree::NodeId;
@@ -22,7 +22,7 @@ pub mod utils;
pub mod prelude {
pub use crate::node::{ElementNode, FromAnyValue, NodeType, OwnedAttributeView, TextNode};
pub use crate::node_ref::{AttributeMaskBuilder, NodeMaskBuilder, NodeView};
pub use crate::passes::{Dependancy, Pass};
pub use crate::passes::{Dependancy, State};
pub use crate::real_dom::{NodeImmutable, NodeMut, NodeRef, RealDom};
pub use crate::tree::NodeId;
pub use crate::SendAnyMap;
+9 -6
View File
@@ -103,16 +103,18 @@ impl DirtyNodeStates {
}
}
pub trait Pass<V: FromAnyValue + Send + Sync = ()>: Any + Send + Sync {
/// This is a tuple of (T: Any, ..)
pub trait State<V: FromAnyValue + Send + Sync = ()>: Any + Send + Sync {
/// This is a tuple of (T: Pass, ..) of states read from the parent required to run this pass
type ParentDependencies: Dependancy;
/// This is a tuple of (T: Any, ..)
/// This is a tuple of (T: Pass, ..) of states read from the children required to run this pass
type ChildDependencies: Dependancy;
/// This is a tuple of (T: Any, ..)
/// This is a tuple of (T: Pass, ..) of states read from the node required to run this pass
type NodeDependencies: Dependancy;
/// This is a mask of what aspects of the node are required to run this pass
const NODE_MASK: NodeMaskBuilder<'static>;
fn pass<'a>(
/// Update this state in a node, returns if the state was updated
fn update<'a>(
&mut self,
node_view: NodeView<V>,
node: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
@@ -121,6 +123,7 @@ pub trait Pass<V: FromAnyValue + Send + Sync = ()>: Any + Send + Sync {
context: &SendAnyMap,
) -> bool;
/// Create a new instance of this state
fn create<'a>(
node_view: NodeView<V>,
node: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
@@ -202,7 +205,7 @@ pub trait Pass<V: FromAnyValue + Send + Sync = ()>: Any + Send + Sync {
.value
.as_mut()
.unwrap()
.pass(view, node, parent, children, context)
.update(view, node, parent, children, context)
}
},
) as PassCallback,
@@ -4,7 +4,7 @@ use dioxus_native_core::{
dioxus::DioxusState,
node_ref::{NodeMaskBuilder, NodeView},
real_dom::RealDom,
Dependancy, Pass, SendAnyMap,
Dependancy, SendAnyMap, State,
};
macro_rules! dep {
+4 -4
View File
@@ -4,7 +4,7 @@ use dioxus_native_core::{
dioxus::DioxusState,
node_ref::{AttributeMaskBuilder, NodeMaskBuilder, NodeView},
real_dom::RealDom,
Dependancy, Pass, SendAnyMap,
Dependancy, SendAnyMap, State,
};
use std::cell::Cell;
@@ -298,7 +298,7 @@ pub struct BlablaState {
count: usize,
}
impl Pass for BlablaState {
impl State for BlablaState {
type ParentDependencies = (Self,);
type ChildDependencies = ();
type NodeDependencies = ();
@@ -307,7 +307,7 @@ impl Pass for BlablaState {
.with_attrs(AttributeMaskBuilder::Some(&["blabla"]))
.with_element();
fn pass<'a>(
fn update<'a>(
&mut self,
_: NodeView,
_: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
@@ -331,7 +331,7 @@ impl Pass for BlablaState {
context: &SendAnyMap,
) -> Self {
let mut myself = Self::default();
myself.pass(node_view, node, parent, children, context);
myself.update(node_view, node, parent, children, context);
myself
}
}
+4 -4
View File
@@ -3,7 +3,7 @@ use dioxus_native_core::{
dioxus::DioxusState,
node_ref::{AttributeMaskBuilder, NodeMaskBuilder, NodeView},
real_dom::RealDom,
Dependancy, Pass, SendAnyMap,
Dependancy, SendAnyMap, State,
};
use tokio::time::sleep;
@@ -12,7 +12,7 @@ pub struct BlablaState {
count: usize,
}
impl Pass for BlablaState {
impl State for BlablaState {
type ParentDependencies = (Self,);
type ChildDependencies = ();
type NodeDependencies = ();
@@ -21,7 +21,7 @@ impl Pass for BlablaState {
.with_attrs(AttributeMaskBuilder::Some(&["blabla"]))
.with_element();
fn pass<'a>(
fn update<'a>(
&mut self,
_: NodeView,
_: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
@@ -45,7 +45,7 @@ impl Pass for BlablaState {
context: &SendAnyMap,
) -> Self {
let mut myself = Self::default();
myself.pass(node_view, node, parent, children, context);
myself.update(node_view, node, parent, children, context);
myself
}
}
+21 -21
View File
@@ -16,13 +16,13 @@ fn node_pass() {
#[derive(Debug, Default, Clone, PartialEq)]
struct Number(i32);
impl Pass for Number {
impl State for Number {
type ChildDependencies = ();
type NodeDependencies = ();
type ParentDependencies = ();
const NODE_MASK: NodeMaskBuilder<'static> = NodeMaskBuilder::new();
fn pass<'a>(
fn update<'a>(
&mut self,
_: NodeView,
_: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
@@ -42,7 +42,7 @@ fn node_pass() {
context: &SendAnyMap,
) -> Self {
let mut myself = Self::default();
myself.pass(node_view, node, parent, children, context);
myself.update(node_view, node, parent, children, context);
myself
}
}
@@ -64,13 +64,13 @@ fn dependant_node_pass() {
#[derive(Debug, Default, Clone, PartialEq)]
struct AddNumber(i32);
impl Pass for AddNumber {
impl State for AddNumber {
type ChildDependencies = ();
type NodeDependencies = (SubtractNumber,);
type ParentDependencies = ();
const NODE_MASK: NodeMaskBuilder<'static> = NodeMaskBuilder::new();
fn pass<'a>(
fn update<'a>(
&mut self,
_: NodeView,
_: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
@@ -90,7 +90,7 @@ fn dependant_node_pass() {
context: &SendAnyMap,
) -> Self {
let mut myself = Self::default();
myself.pass(node_view, node, parent, children, context);
myself.update(node_view, node, parent, children, context);
myself
}
}
@@ -98,13 +98,13 @@ fn dependant_node_pass() {
#[derive(Debug, Default, Clone, PartialEq)]
struct SubtractNumber(i32);
impl Pass for SubtractNumber {
impl State for SubtractNumber {
type ChildDependencies = ();
type NodeDependencies = ();
type ParentDependencies = ();
const NODE_MASK: NodeMaskBuilder<'static> = NodeMaskBuilder::new();
fn pass<'a>(
fn update<'a>(
&mut self,
_: NodeView,
_: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
@@ -124,7 +124,7 @@ fn dependant_node_pass() {
context: &SendAnyMap,
) -> Self {
let mut myself = Self::default();
myself.pass(node_view, node, parent, children, context);
myself.update(node_view, node, parent, children, context);
myself
}
}
@@ -163,14 +163,14 @@ fn independant_node_pass() {
#[derive(Debug, Default, Clone, PartialEq)]
struct AddNumber(i32);
impl Pass for AddNumber {
impl State for AddNumber {
type ChildDependencies = ();
type NodeDependencies = ();
type ParentDependencies = ();
const NODE_MASK: NodeMaskBuilder<'static> = NodeMaskBuilder::new();
fn pass<'a>(
fn update<'a>(
&mut self,
_: NodeView,
_: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
@@ -190,7 +190,7 @@ fn independant_node_pass() {
context: &SendAnyMap,
) -> Self {
let mut myself = Self::default();
myself.pass(node_view, node, parent, children, context);
myself.update(node_view, node, parent, children, context);
myself
}
}
@@ -198,14 +198,14 @@ fn independant_node_pass() {
#[derive(Debug, Default, Clone, PartialEq)]
struct SubtractNumber(i32);
impl Pass for SubtractNumber {
impl State for SubtractNumber {
type ChildDependencies = ();
type NodeDependencies = ();
type ParentDependencies = ();
const NODE_MASK: NodeMaskBuilder<'static> = NodeMaskBuilder::new();
fn pass<'a>(
fn update<'a>(
&mut self,
_: NodeView,
_: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
@@ -225,7 +225,7 @@ fn independant_node_pass() {
context: &SendAnyMap,
) -> Self {
let mut myself = Self::default();
myself.pass(node_view, node, parent, children, context);
myself.update(node_view, node, parent, children, context);
myself
}
}
@@ -272,14 +272,14 @@ fn down_pass() {
}
}
impl Pass for AddNumber {
impl State for AddNumber {
type ChildDependencies = ();
type NodeDependencies = ();
type ParentDependencies = (AddNumber,);
const NODE_MASK: NodeMaskBuilder<'static> = NodeMaskBuilder::new();
fn pass<'a>(
fn update<'a>(
&mut self,
_: NodeView,
_: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
@@ -301,7 +301,7 @@ fn down_pass() {
context: &SendAnyMap,
) -> Self {
let mut myself = Self::default();
myself.pass(node_view, node, parent, children, context);
myself.update(node_view, node, parent, children, context);
myself
}
}
@@ -360,14 +360,14 @@ fn up_pass() {
#[derive(Debug, Clone, PartialEq)]
struct AddNumber(i32);
impl Pass for AddNumber {
impl State for AddNumber {
type ChildDependencies = (AddNumber,);
type NodeDependencies = ();
type ParentDependencies = ();
const NODE_MASK: NodeMaskBuilder<'static> = NodeMaskBuilder::new();
fn pass<'a>(
fn update<'a>(
&mut self,
_: NodeView,
_: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
@@ -389,7 +389,7 @@ fn up_pass() {
context: &SendAnyMap,
) -> Self {
let mut myself = Self(1);
myself.pass(node_view, node, parent, children, context);
myself.update(node_view, node, parent, children, context);
myself
}
}