Skip to content

Commit

Permalink
implement event handling with multiple subtree roots
Browse files Browse the repository at this point in the history
  • Loading branch information
WorldSEnder committed Mar 11, 2022
1 parent 2cffeb1 commit 9cf69cd
Show file tree
Hide file tree
Showing 17 changed files with 422 additions and 244 deletions.
4 changes: 2 additions & 2 deletions packages/yew/src/dom_bundle/app_handle.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! [AppHandle] contains the state Yew keeps to bootstrap a component in an isolated scope.

use super::{BundleRoot, ComponentRenderState, Scoped};
use super::{BSubtree, ComponentRenderState, Scoped};
use crate::html::{BaseComponent, Scope};
use crate::NodeRef;
use std::{ops::Deref, rc::Rc};
Expand All @@ -27,7 +27,7 @@ where
scope: Scope::new(None),
};
let node_ref = NodeRef::default();
let hosting_root = BundleRoot::create_root(&host);
let hosting_root = BSubtree::create_root(&host);
let initial_render_state =
ComponentRenderState::new(hosting_root, host, NodeRef::default(), &node_ref);
app.scope
Expand Down
32 changes: 16 additions & 16 deletions packages/yew/src/dom_bundle/bcomp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This module contains the bundle implementation of a virtual component [BComp].

use super::{insert_node, BNode, BundleRoot, DomBundle, Reconcilable};
use super::{insert_node, BNode, BSubtree, DomBundle, Reconcilable};
use crate::html::{AnyScope, BaseComponent, Scope};
use crate::virtual_dom::{Key, VComp, VNode};
use crate::NodeRef;
Expand Down Expand Up @@ -40,11 +40,11 @@ impl fmt::Debug for BComp {
}

impl DomBundle for BComp {
fn detach(self, _root: &BundleRoot, _parent: &Element, parent_to_detach: bool) {
fn detach(self, _root: &BSubtree, _parent: &Element, parent_to_detach: bool) {
self.scope.destroy_boxed(parent_to_detach);
}

fn shift(&self, next_root: &BundleRoot, next_parent: &Element, next_sibling: NodeRef) {
fn shift(&self, next_root: &BSubtree, next_parent: &Element, next_sibling: NodeRef) {
self.scope
.shift_node(next_root, next_parent.clone(), next_sibling);
}
Expand All @@ -55,7 +55,7 @@ impl Reconcilable for VComp {

fn attach(
self,
root: &BundleRoot,
root: &BSubtree,
parent_scope: &AnyScope,
parent: &Element,
next_sibling: NodeRef,
Expand Down Expand Up @@ -88,7 +88,7 @@ impl Reconcilable for VComp {

fn reconcile_node(
self,
root: &BundleRoot,
root: &BSubtree,
parent_scope: &AnyScope,
parent: &Element,
next_sibling: NodeRef,
Expand All @@ -107,7 +107,7 @@ impl Reconcilable for VComp {

fn reconcile(
self,
_root: &BundleRoot,
_root: &BSubtree,
_parent_scope: &AnyScope,
_parent: &Element,
next_sibling: NodeRef,
Expand All @@ -133,7 +133,7 @@ pub trait Mountable {
fn mount(
self: Box<Self>,
node_ref: NodeRef,
root: &BundleRoot,
root: &BSubtree,
parent_scope: &AnyScope,
parent: Element,
next_sibling: NodeRef,
Expand Down Expand Up @@ -169,7 +169,7 @@ impl<COMP: BaseComponent> Mountable for PropsWrapper<COMP> {
fn mount(
self: Box<Self>,
node_ref: NodeRef,
root: &BundleRoot,
root: &BSubtree,
parent_scope: &AnyScope,
parent: Element,
next_sibling: NodeRef,
Expand Down Expand Up @@ -202,7 +202,7 @@ impl<COMP: BaseComponent> Mountable for PropsWrapper<COMP> {
}

pub struct ComponentRenderState {
hosting_root: BundleRoot,
hosting_root: BSubtree,
view_node: BNode,
/// When a component has no parent, it means that it should not be rendered.
parent: Option<Element>,
Expand All @@ -221,7 +221,7 @@ impl std::fmt::Debug for ComponentRenderState {
impl ComponentRenderState {
/// Prepare a place in the DOM to hold the eventual [VNode] from rendering a component
pub(crate) fn new(
hosting_root: BundleRoot,
hosting_root: BSubtree,
parent: Element,
next_sibling: NodeRef,
node_ref: &NodeRef,
Expand All @@ -247,7 +247,7 @@ impl ComponentRenderState {
use super::blist::BList;

Self {
hosting_root: BundleRoot::create_ssr(),
hosting_root: BSubtree::create_ssr(),
view_node: BNode::List(BList::new()),
parent: None,
next_sibling: NodeRef::default(),
Expand All @@ -261,7 +261,7 @@ impl ComponentRenderState {
/// Shift the rendered content to a new DOM position
pub(crate) fn shift(
&mut self,
next_root: &BundleRoot,
next_root: &BSubtree,
new_parent: Element,
next_sibling: NodeRef,
) {
Expand Down Expand Up @@ -310,7 +310,7 @@ pub trait Scoped {
/// Get the render state if it hasn't already been destroyed
fn render_state(&self) -> Option<Ref<'_, ComponentRenderState>>;
/// Shift the node associated with this scope to a new place
fn shift_node(&self, next_root: &BundleRoot, parent: Element, next_sibling: NodeRef);
fn shift_node(&self, next_root: &BSubtree, parent: Element, next_sibling: NodeRef);
/// Process an event to destroy a component
fn destroy(self, parent_to_detach: bool);
fn destroy_boxed(self: Box<Self>, parent_to_detach: bool);
Expand Down Expand Up @@ -516,17 +516,17 @@ mod tests {
}
}

fn setup_parent() -> (BundleRoot, AnyScope, Element) {
fn setup_parent() -> (BSubtree, AnyScope, Element) {
let scope = AnyScope::test();
let parent = document().create_element("div").unwrap();
let root = BundleRoot::create_root(&parent);
let root = BSubtree::create_root(&parent);

document().body().unwrap().append_child(&parent).unwrap();

(root, scope, parent)
}

fn get_html(node: Html, root: &BundleRoot, scope: &AnyScope, parent: &Element) -> String {
fn get_html(node: Html, root: &BSubtree, scope: &AnyScope, parent: &Element) -> String {
// clear parent
parent.set_inner_html("");

Expand Down
18 changes: 9 additions & 9 deletions packages/yew/src/dom_bundle/blist.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! This module contains fragments bundles, a [BList]
use super::{test_log, BNode, BundleRoot};
use super::{test_log, BNode, BSubtree};
use crate::dom_bundle::{DomBundle, Reconcilable};
use crate::html::{AnyScope, NodeRef};
use crate::virtual_dom::{Key, VList, VNode, VText};
Expand Down Expand Up @@ -31,7 +31,7 @@ impl Deref for BList {
/// Helper struct, that keeps the position where the next element is to be placed at
#[derive(Clone)]
struct NodeWriter<'s> {
root: &'s BundleRoot,
root: &'s BSubtree,
parent_scope: &'s AnyScope,
parent: &'s Element,
next_sibling: NodeRef,
Expand Down Expand Up @@ -143,7 +143,7 @@ impl BList {

/// Diff and patch unkeyed child lists
fn apply_unkeyed(
root: &BundleRoot,
root: &BSubtree,
parent_scope: &AnyScope,
parent: &Element,
next_sibling: NodeRef,
Expand Down Expand Up @@ -184,7 +184,7 @@ impl BList {
/// Optimized for node addition or removal from either end of the list and small changes in the
/// middle.
fn apply_keyed(
root: &BundleRoot,
root: &BSubtree,
parent_scope: &AnyScope,
parent: &Element,
next_sibling: NodeRef,
Expand Down Expand Up @@ -367,13 +367,13 @@ impl BList {
}

impl DomBundle for BList {
fn detach(self, root: &BundleRoot, parent: &Element, parent_to_detach: bool) {
fn detach(self, root: &BSubtree, parent: &Element, parent_to_detach: bool) {
for child in self.rev_children.into_iter() {
child.detach(root, parent, parent_to_detach);
}
}

fn shift(&self, next_root: &BundleRoot, next_parent: &Element, next_sibling: NodeRef) {
fn shift(&self, next_root: &BSubtree, next_parent: &Element, next_sibling: NodeRef) {
for node in self.rev_children.iter().rev() {
node.shift(next_root, next_parent, next_sibling.clone());
}
Expand All @@ -385,7 +385,7 @@ impl Reconcilable for VList {

fn attach(
self,
root: &BundleRoot,
root: &BSubtree,
parent_scope: &AnyScope,
parent: &Element,
next_sibling: NodeRef,
Expand All @@ -397,7 +397,7 @@ impl Reconcilable for VList {

fn reconcile_node(
self,
root: &BundleRoot,
root: &BSubtree,
parent_scope: &AnyScope,
parent: &Element,
next_sibling: NodeRef,
Expand All @@ -411,7 +411,7 @@ impl Reconcilable for VList {

fn reconcile(
mut self,
root: &BundleRoot,
root: &BSubtree,
parent_scope: &AnyScope,
parent: &Element,
next_sibling: NodeRef,
Expand Down
12 changes: 6 additions & 6 deletions packages/yew/src/dom_bundle/bnode.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This module contains the bundle version of an abstract node [BNode]

use super::{BComp, BList, BPortal, BSuspense, BTag, BText, BundleRoot};
use super::{BComp, BList, BPortal, BSubtree, BSuspense, BTag, BText};
use crate::dom_bundle::{DomBundle, Reconcilable};
use crate::html::{AnyScope, NodeRef};
use crate::virtual_dom::{Key, VNode};
Expand Down Expand Up @@ -43,7 +43,7 @@ impl BNode {

impl DomBundle for BNode {
/// Remove VNode from parent.
fn detach(self, root: &BundleRoot, parent: &Element, parent_to_detach: bool) {
fn detach(self, root: &BSubtree, parent: &Element, parent_to_detach: bool) {
match self {
Self::Tag(vtag) => vtag.detach(root, parent, parent_to_detach),
Self::Text(btext) => btext.detach(root, parent, parent_to_detach),
Expand All @@ -60,7 +60,7 @@ impl DomBundle for BNode {
}
}

fn shift(&self, next_root: &BundleRoot, next_parent: &Element, next_sibling: NodeRef) {
fn shift(&self, next_root: &BSubtree, next_parent: &Element, next_sibling: NodeRef) {
match self {
Self::Tag(ref vtag) => vtag.shift(next_root, next_parent, next_sibling),
Self::Text(ref btext) => btext.shift(next_root, next_parent, next_sibling),
Expand All @@ -82,7 +82,7 @@ impl Reconcilable for VNode {

fn attach(
self,
root: &BundleRoot,
root: &BSubtree,
parent_scope: &AnyScope,
parent: &Element,
next_sibling: NodeRef,
Expand Down Expand Up @@ -122,7 +122,7 @@ impl Reconcilable for VNode {

fn reconcile_node(
self,
root: &BundleRoot,
root: &BSubtree,
parent_scope: &AnyScope,
parent: &Element,
next_sibling: NodeRef,
Expand All @@ -133,7 +133,7 @@ impl Reconcilable for VNode {

fn reconcile(
self,
root: &BundleRoot,
root: &BSubtree,
parent_scope: &AnyScope,
parent: &Element,
next_sibling: NodeRef,
Expand Down
18 changes: 9 additions & 9 deletions packages/yew/src/dom_bundle/bportal.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This module contains the bundle implementation of a portal [BPortal].

use super::{test_log, BNode, BundleRoot};
use super::{test_log, BNode, BSubtree};
use crate::dom_bundle::{DomBundle, Reconcilable};
use crate::html::{AnyScope, NodeRef};
use crate::virtual_dom::Key;
Expand All @@ -11,7 +11,7 @@ use web_sys::Element;
#[derive(Debug)]
pub struct BPortal {
// The inner root
inner_root: BundleRoot,
inner_root: BSubtree,
/// The element under which the content is inserted.
host: Element,
/// The next sibling after the inserted content
Expand All @@ -21,12 +21,12 @@ pub struct BPortal {
}

impl DomBundle for BPortal {
fn detach(self, _root: &BundleRoot, _parent: &Element, _parent_to_detach: bool) {
fn detach(self, _root: &BSubtree, _parent: &Element, _parent_to_detach: bool) {
test_log!("Detaching portal from host",);
self.node.detach(&self.inner_root, &self.host, false);
}

fn shift(&self, _next_root: &BundleRoot, _next_parent: &Element, _next_sibling: NodeRef) {
fn shift(&self, _next_root: &BSubtree, _next_parent: &Element, _next_sibling: NodeRef) {
// portals have nothing in it's original place of DOM, we also do nothing.
}
}
Expand All @@ -36,17 +36,17 @@ impl Reconcilable for VPortal {

fn attach(
self,
_root: &BundleRoot,
root: &BSubtree,
parent_scope: &AnyScope,
_parent: &Element,
parent: &Element,
host_next_sibling: NodeRef,
) -> (NodeRef, Self::Bundle) {
let Self {
host,
inner_sibling,
node,
} = self;
let inner_root = BundleRoot::create_root(&host);
let inner_root = root.create_subroot(parent.clone(), &host);
let (_, inner) = node.attach(&inner_root, parent_scope, &host, inner_sibling.clone());
(
host_next_sibling,
Expand All @@ -61,7 +61,7 @@ impl Reconcilable for VPortal {

fn reconcile_node(
self,
root: &BundleRoot,
root: &BSubtree,
parent_scope: &AnyScope,
parent: &Element,
next_sibling: NodeRef,
Expand All @@ -77,7 +77,7 @@ impl Reconcilable for VPortal {

fn reconcile(
self,
_root: &BundleRoot,
_root: &BSubtree,
parent_scope: &AnyScope,
parent: &Element,
next_sibling: NodeRef,
Expand Down

0 comments on commit 9cf69cd

Please sign in to comment.