Move towards a new reconciler model, undo a lot of the weird stretch integrations I had done, separate out Layout and Appearance concepts, introduce ComponentKey and constructor lifecycle for components

This commit is contained in:
Ryan McGrath 2019-05-27 00:22:33 -07:00
parent 6fd3f79099
commit 91266cc841
No known key found for this signature in database
GPG key ID: 811674B62B666830
31 changed files with 820 additions and 941 deletions

View file

@ -3,7 +3,6 @@
//! uses these to build and alter UI; they're typically returned from `render()`
//! methods.
use std::sync::{Arc, RwLock};
use std::fmt::{Debug, Display};
mod virtual_node;
@ -15,6 +14,7 @@ pub use virtual_text::VirtualText;
mod props;
pub use props::Props;
use crate::reconciler::key::ComponentKey;
use crate::traits::Component;
/// An enum representing the types of nodes that the
@ -31,16 +31,15 @@ impl RSX {
/// this yourself; the `rsx! {}` macro handles this for you.
pub fn node(
tag: &'static str,
create_fn: fn() -> Arc<RwLock<Component>>,
props: Props
create_fn: fn(key: ComponentKey) -> Box<Component>,
props: Props,
children: Vec<RSX>
) -> RSX {
RSX::VirtualNode(VirtualNode {
tag: tag,
create_component_fn: create_fn,
instance: None,
layout_node: None,
props: props,
children: vec![]
props: Some(props),
children: children
})
}

View file

@ -39,6 +39,15 @@ pub struct Props {
}
impl Props {
pub fn new(key: String, styles: StylesList, attributes: HashMap<&'static str, AttributeType>) -> Props {
Props {
attributes: attributes,
children: vec![],
key: key,
styles: styles
}
}
/// Returns a Vec of RSX nodes, which are really just cloned pointers for the most part.
pub fn children(&self) -> Vec<RSX> {
self.children.clone()

View file

@ -1,17 +1,14 @@
//! Implements the `RSX::VirtualNode` struct, which is a bit of a recursive
//! structure.
use std::sync::{Arc, RwLock};
use std::fmt::{Display, Debug};
use alchemy_styles::node::Node;
use crate::traits::Component;
use crate::reconciler::key::ComponentKey;
use crate::rsx::{RSX, Props};
use crate::traits::Component;
/// A VirtualNode is akin to an `Element` in React terms. Here, we provide a way
/// for lazy `Component` instantiation, along with storage for things like layout nodes,
/// properties, children and so on.
/// for lazy `Component` instantiation, properties, children and so on.
#[derive(Clone)]
pub struct VirtualNode {
/// Used in debugging/printing/etc.
@ -19,22 +16,17 @@ pub struct VirtualNode {
/// `Component` instances are created on-demand, if the reconciler deems it be so. This
/// is a closure that should return an instance of the correct type.
pub create_component_fn: fn() -> Arc<RwLock<Component>>,
pub create_component_fn: fn(key: ComponentKey) -> Box<Component>,
/// A cached component instance, which is transferred between trees. Since `Component`
/// instances are lazily created, this is an `Option`, and defaults to `None`.
pub instance: Option<Arc<RwLock<Component>>>,
/// A cached `Node` for computing `Layout` with `Stretch`. Some components may not have
/// a need for layout (e.g, if they don't have a backing node), and thus this is optional.
/// `Props`, which are to be passed to this `Component` at various lifecycle methods. Once
/// the reconciler takes ownership of this VirtualNode, these props are moved to a different
/// location - thus, you shouldn't rely on them for anything unless you specifically keep
/// ownership of a VirtualNode.
///
/// The reconciler will handle bridging tree structures as necessary.
pub layout_node: Option<Node>,
/// This aspect of functionality may be pulled in a later release if it causes too many issues.
pub props: Option<Props>,
/// `Props`, which are to be passed to this `Component` at various lifecycle methods.
pub props: Props,
/// Computed children get stored here.
///
pub children: Vec<RSX>
}