Skip to content

Commit bcde660

Browse files
committed
work on naming: ancestor -> bundle
1 parent 031976c commit bcde660

File tree

11 files changed

+111
-107
lines changed

11 files changed

+111
-107
lines changed

packages/yew/src/dom_bundle/attributes.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ impl<T: AccessValue> Apply for Value<T> {
4747
self
4848
}
4949

50-
fn apply_diff(self, el: &Self::Element, ancestor: &mut Self) {
51-
match (&self.0, &ancestor.0) {
50+
fn apply_diff(self, el: &Self::Element, bundle: &mut Self) {
51+
match (&self.0, &bundle.0) {
5252
(Some(new), Some(_)) => {
5353
// Refresh value from the DOM. It might have changed.
5454
if new.as_ref() != el.value() {
@@ -96,8 +96,8 @@ pub(crate) trait Apply {
9696
/// Apply contained values to [Element] with no ancestor
9797
fn apply(self, el: &Self::Element) -> Self::Bundle;
9898

99-
/// Apply diff between [self] and `ancestor` to [Element].
100-
fn apply_diff(self, el: &Self::Element, ancestor: &mut Self::Bundle);
99+
/// Apply diff between [self] and `bundle` to [Element].
100+
fn apply_diff(self, el: &Self::Element, bundle: &mut Self::Bundle);
101101
}
102102

103103
/// Fields specific to
@@ -159,12 +159,12 @@ impl Apply for InputFields {
159159
self
160160
}
161161

162-
fn apply_diff(self, el: &Self::Element, ancestor: &mut Self) {
162+
fn apply_diff(self, el: &Self::Element, bundle: &mut Self) {
163163
// IMPORTANT! This parameter has to be set every time
164164
// to prevent strange behaviour in the browser when the DOM changes
165165
el.set_checked(self.checked);
166166

167-
self.value.apply_diff(el, &mut ancestor.value);
167+
self.value.apply_diff(el, &mut bundle.value);
168168
}
169169
}
170170

packages/yew/src/dom_bundle/bcomp.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,18 @@ impl Reconcilable for VComp {
160160
parent_scope: &AnyScope,
161161
parent: &Element,
162162
next_sibling: NodeRef,
163-
ancestor: &mut BNode,
163+
bundle: &mut BNode,
164164
) -> NodeRef {
165-
let bcomp = match ancestor {
166-
// If the ancestor is the same type, reuse it and update its properties
165+
let bcomp = match bundle {
166+
// If the existing bundle is the same type, reuse it and update its properties
167167
BNode::BComp(ref mut bcomp)
168168
if self.type_id == bcomp.type_id && self.key == bcomp.key =>
169169
{
170170
bcomp
171171
}
172172
_ => {
173173
let (node_ref, self_) = self.attach(parent_scope, parent, next_sibling);
174-
ancestor.replace(parent, self_.into());
174+
bundle.replace(parent, self_.into());
175175
return node_ref;
176176
}
177177
};
@@ -239,16 +239,16 @@ mod tests {
239239
let parent_scope: AnyScope = AnyScope::test();
240240
let parent_element = document.create_element("div").unwrap();
241241

242-
let ancestor = html! { <Comp></Comp> };
243-
let (_, mut comp) = ancestor.attach(&parent_scope, &parent_element, NodeRef::default());
242+
let comp = html! { <Comp></Comp> };
243+
let (_, mut bundle) = comp.attach(&parent_scope, &parent_element, NodeRef::default());
244244

245245
for _ in 0..10000 {
246246
let node = html! { <Comp></Comp> };
247247
node.reconcile(
248248
&parent_scope,
249249
&parent_element,
250250
NodeRef::default(),
251-
&mut comp,
251+
&mut bundle,
252252
);
253253
}
254254
}

packages/yew/src/dom_bundle/blist.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ impl<'s> ElementWriter<'s> {
5353
)
5454
}
5555

56-
fn patch(self, node: VNode, ancestor: &mut BNode) -> Self {
57-
test_log!("patching: {:?} -> {:?}", ancestor, node);
56+
fn patch(self, node: VNode, bundle: &mut BNode) -> Self {
57+
test_log!("patching: {:?} -> {:?}", bundle, node);
5858
test_log!(
5959
" parent={:?}, next_sibling={:?}",
6060
self.parent.outer_html(),
6161
self.next_sibling
6262
);
6363
// Advance the next sibling reference (from right to left)
64-
ancestor.shift(self.parent, self.next_sibling.clone());
65-
let next = node.reconcile(self.parent_scope, self.parent, self.next_sibling, ancestor);
64+
bundle.shift(self.parent, self.next_sibling.clone());
65+
let next = node.reconcile(self.parent_scope, self.parent, self.next_sibling, bundle);
6666
test_log!(" next_position: {:?}", next);
6767
Self {
6868
next_sibling: next,
@@ -306,15 +306,15 @@ impl Reconcilable for VList {
306306
parent_scope: &AnyScope,
307307
parent: &Element,
308308
next_sibling: NodeRef,
309-
ancestor: &mut BNode,
309+
bundle: &mut BNode,
310310
) -> NodeRef {
311311
// Here, we will try to diff the previous list elements with the new
312312
// ones we want to insert. For that, we will use two lists:
313313
// - lefts: new elements to render in the DOM
314314
// - rights: previously rendered elements.
315315
//
316316
// The left items are known since we want to insert them
317-
// (self.children). For the right ones, we will look at the ancestor,
317+
// (self.children). For the right ones, we will look at the bundle,
318318
// i.e. the current DOM list element that we want to replace with self.
319319

320320
if self.children.is_empty() {
@@ -325,7 +325,9 @@ impl Reconcilable for VList {
325325
}
326326

327327
let lefts = self.children;
328-
let blist = ancestor.make_list();
328+
// 'Forcefully' create a pretend the existing node is a list. Creates a
329+
// singleton list if it isn't already.
330+
let blist = bundle.make_list();
329331
let rights = &mut blist.rev_children;
330332
test_log!("lefts: {:?}", lefts);
331333
test_log!("rights: {:?}", rights);

packages/yew/src/dom_bundle/bnode.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,29 +133,30 @@ impl Reconcilable for VNode {
133133
parent_scope: &AnyScope,
134134
parent: &Element,
135135
next_sibling: NodeRef,
136-
ancestor: &mut BNode,
136+
bundle: &mut BNode,
137137
) -> NodeRef {
138138
match self {
139-
VNode::VTag(vtag) => vtag.reconcile(parent_scope, parent, next_sibling, ancestor),
140-
VNode::VText(vtext) => vtext.reconcile(parent_scope, parent, next_sibling, ancestor),
141-
VNode::VComp(vcomp) => vcomp.reconcile(parent_scope, parent, next_sibling, ancestor),
142-
VNode::VList(vlist) => vlist.reconcile(parent_scope, parent, next_sibling, ancestor),
139+
VNode::VTag(vtag) => vtag.reconcile(parent_scope, parent, next_sibling, bundle),
140+
VNode::VText(vtext) => vtext.reconcile(parent_scope, parent, next_sibling, bundle),
141+
VNode::VComp(vcomp) => vcomp.reconcile(parent_scope, parent, next_sibling, bundle),
142+
VNode::VList(vlist) => vlist.reconcile(parent_scope, parent, next_sibling, bundle),
143143
VNode::VRef(node) => {
144-
if let BNode::BRef(ref n) = ancestor {
145-
if &node == n {
146-
return NodeRef::new(node);
144+
let _existing = match bundle {
145+
BNode::BRef(ref n) if &node == n => n,
146+
_ => {
147+
let (node_ref, self_) =
148+
VNode::VRef(node).attach(parent_scope, parent, next_sibling);
149+
bundle.replace(parent, self_);
150+
return node_ref;
147151
}
148-
}
149-
let (node_ref, self_) =
150-
VNode::VRef(node).attach(parent_scope, parent, next_sibling);
151-
ancestor.replace(parent, self_);
152-
node_ref
152+
};
153+
NodeRef::new(node)
153154
}
154155
VNode::VPortal(vportal) => {
155-
vportal.reconcile(parent_scope, parent, next_sibling, ancestor)
156+
vportal.reconcile(parent_scope, parent, next_sibling, bundle)
156157
}
157158
VNode::VSuspense(vsuspsense) => {
158-
vsuspsense.reconcile(parent_scope, parent, next_sibling, ancestor)
159+
vsuspsense.reconcile(parent_scope, parent, next_sibling, bundle)
159160
}
160161
}
161162
}

packages/yew/src/dom_bundle/bportal.rs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::dom_bundle::{DomBundle, Reconcilable};
66
use crate::html::{AnyScope, NodeRef};
77
use crate::virtual_dom::Key;
88
use crate::virtual_dom::VPortal;
9-
use std::borrow::BorrowMut;
109
use web_sys::Element;
1110

1211
/// The bundle implementation to [VPortal].
@@ -15,7 +14,7 @@ pub struct BPortal {
1514
/// The element under which the content is inserted.
1615
host: Element,
1716
/// The next sibling after the inserted content
18-
next_sibling: NodeRef,
17+
inner_sibling: NodeRef,
1918
/// The inserted node
2019
node: Box<BNode>,
2120
}
@@ -41,18 +40,18 @@ impl Reconcilable for VPortal {
4140
_parent: &Element,
4241
host_next_sibling: NodeRef,
4342
) -> (NodeRef, Self::Bundle) {
44-
let VPortal {
43+
let Self {
4544
host,
46-
next_sibling,
45+
inner_sibling,
4746
node,
4847
} = self;
49-
let (_, inner) = node.attach(parent_scope, &host, next_sibling.clone());
48+
let (_, inner) = node.attach(parent_scope, &host, inner_sibling.clone());
5049
(
5150
host_next_sibling,
5251
BPortal {
5352
host,
5453
node: Box::new(inner),
55-
next_sibling,
54+
inner_sibling,
5655
},
5756
)
5857
}
@@ -62,25 +61,33 @@ impl Reconcilable for VPortal {
6261
parent_scope: &AnyScope,
6362
parent: &Element,
6463
next_sibling: NodeRef,
65-
ancestor: &mut BNode,
64+
bundle: &mut BNode,
6665
) -> NodeRef {
67-
if let BNode::BPortal(portal) = ancestor {
68-
let old_host = std::mem::replace(&mut portal.host, self.host);
69-
let old_sibling = std::mem::replace(&mut portal.next_sibling, self.next_sibling);
70-
let node = &mut portal.node;
71-
if old_host != portal.host || old_sibling != portal.next_sibling {
72-
// Remount the inner node somewhere else instead of diffing
73-
// Move the node, but keep the state
74-
node.shift(&portal.host, portal.next_sibling.clone());
66+
let portal = match bundle {
67+
BNode::BPortal(portal) => portal,
68+
_ => {
69+
let (self_ref, self_) = self.attach(parent_scope, parent, next_sibling);
70+
bundle.replace(parent, self_.into());
71+
return self_ref;
7572
}
76-
let inner_ancestor = node.borrow_mut();
77-
self.node
78-
.reconcile(parent_scope, parent, next_sibling.clone(), inner_ancestor);
79-
return next_sibling;
80-
}
73+
};
74+
let Self {
75+
host,
76+
inner_sibling,
77+
node,
78+
} = self;
79+
80+
let old_host = std::mem::replace(&mut portal.host, host);
81+
let old_inner_sibling = std::mem::replace(&mut portal.inner_sibling, inner_sibling);
8182

82-
let (_, self_) = self.attach(parent_scope, parent, next_sibling.clone());
83-
ancestor.replace(parent, self_.into());
83+
if old_host != portal.host || old_inner_sibling != portal.inner_sibling {
84+
// Remount the inner node somewhere else instead of diffing
85+
// Move the node, but keep the state
86+
portal
87+
.node
88+
.shift(&portal.host, portal.inner_sibling.clone());
89+
}
90+
node.reconcile(parent_scope, parent, next_sibling.clone(), &mut portal.node);
8491
next_sibling
8592
}
8693
}

packages/yew/src/dom_bundle/bsuspense.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ impl Reconcilable for VSuspense {
9090
parent_scope: &AnyScope,
9191
parent: &Element,
9292
next_sibling: NodeRef,
93-
ancestor: &mut BNode,
93+
bundle: &mut BNode,
9494
) -> NodeRef {
95-
let suspense = match ancestor {
95+
let suspense = match bundle {
9696
// We only preserve the child state if they are the same suspense.
9797
BNode::BSuspense(m)
9898
if m.key == self.key && self.detached_parent == m.detached_parent =>
@@ -101,41 +101,41 @@ impl Reconcilable for VSuspense {
101101
}
102102
_ => {
103103
let (self_ref, self_) = self.attach(parent_scope, parent, next_sibling);
104-
ancestor.replace(parent, self_.into());
104+
bundle.replace(parent, self_.into());
105105
return self_ref;
106106
}
107107
};
108-
let children_ancestor = &mut suspense.children;
108+
let children_bundle = &mut suspense.children;
109109
// no need to update key & detached_parent
110110

111111
// When it's suspended, we render children into an element that is detached from the dom
112112
// tree while rendering fallback UI into the original place where children resides in.
113113
match (self.suspended, &mut suspense.fallback) {
114-
(true, Some(fallback_ancestor)) => {
114+
(true, Some(fallback_bundle)) => {
115115
self.children.reconcile(
116116
parent_scope,
117117
&self.detached_parent,
118118
NodeRef::default(),
119-
children_ancestor,
119+
children_bundle,
120120
);
121121

122122
self.fallback
123-
.reconcile(parent_scope, parent, next_sibling, fallback_ancestor)
123+
.reconcile(parent_scope, parent, next_sibling, fallback_bundle)
124124
}
125125

126126
(false, None) => {
127127
self.children
128-
.reconcile(parent_scope, parent, next_sibling, children_ancestor)
128+
.reconcile(parent_scope, parent, next_sibling, children_bundle)
129129
}
130130

131131
(true, None) => {
132-
children_ancestor.shift(&self.detached_parent, NodeRef::default());
132+
children_bundle.shift(&self.detached_parent, NodeRef::default());
133133

134134
self.children.reconcile(
135135
parent_scope,
136136
&self.detached_parent,
137137
NodeRef::default(),
138-
children_ancestor,
138+
children_bundle,
139139
);
140140
// first render of fallback
141141
let (fallback_ref, fallback) =
@@ -147,9 +147,9 @@ impl Reconcilable for VSuspense {
147147
(false, Some(_)) => {
148148
suspense.fallback.take().unwrap().detach(parent);
149149

150-
children_ancestor.shift(parent, next_sibling.clone());
150+
children_bundle.shift(parent, next_sibling.clone());
151151
self.children
152-
.reconcile(parent_scope, parent, next_sibling, children_ancestor)
152+
.reconcile(parent_scope, parent, next_sibling, children_bundle)
153153
}
154154
}
155155
}

packages/yew/src/dom_bundle/btag.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@ impl Reconcilable for VTag {
136136
parent_scope: &AnyScope,
137137
parent: &Element,
138138
next_sibling: NodeRef,
139-
node_bundle: &mut BNode,
139+
bundle: &mut BNode,
140140
) -> NodeRef {
141141
// This kind of branching patching routine reduces branch predictor misses and the need to
142142
// unpack the enums (including `Option`s) all the time, resulting in a more streamlined
143143
// patching flow
144-
let is_matching_tag = match node_bundle {
144+
let is_matching_tag = match bundle {
145145
BNode::BTag(ex) if self.key == ex.key => match (&self.inner, &ex.inner) {
146146
(VTagInner::Input(_), BTagInner::Input(_)) => true,
147147
(VTagInner::Textarea { .. }, BTagInner::Textarea { .. }) => true,
@@ -155,7 +155,7 @@ impl Reconcilable for VTag {
155155
// If the ancestor is a tag of the same type, don't recreate, keep the
156156
// old tag and update its attributes and children.
157157
let tag = if is_matching_tag {
158-
match node_bundle {
158+
match bundle {
159159
BNode::BTag(a) => {
160160
// Preserve the reference that already exists
161161
a.deref_mut()
@@ -164,7 +164,7 @@ impl Reconcilable for VTag {
164164
}
165165
} else {
166166
let (self_ref, self_) = self.attach(parent_scope, parent, next_sibling);
167-
node_bundle.replace(parent, self_.into());
167+
bundle.replace(parent, self_.into());
168168
return self_ref;
169169
};
170170

packages/yew/src/dom_bundle/btext.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ impl Reconcilable for VText {
5555
parent_scope: &AnyScope,
5656
parent: &Element,
5757
next_sibling: NodeRef,
58-
ancestor: &mut BNode,
58+
bundle: &mut BNode,
5959
) -> NodeRef {
60-
let btext = match ancestor {
60+
let btext = match bundle {
6161
BNode::BText(btext) => btext,
6262
_ => {
63-
let (node_ref, self_) = self.attach(parent_scope, parent, next_sibling);
64-
ancestor.replace(parent, self_.into());
65-
return node_ref;
63+
let (self_ref, self_) = self.attach(parent_scope, parent, next_sibling);
64+
bundle.replace(parent, self_.into());
65+
return self_ref;
6666
}
6767
};
6868
let Self { text } = self;

0 commit comments

Comments
 (0)