Skip to content

Commit 8d36325

Browse files
committed
util.vdom.TreeBuilder: #buildTree() #7098
1 parent e3db2d3 commit 8d36325

1 file changed

Lines changed: 36 additions & 62 deletions

File tree

src/util/vdom/TreeBuilder.mjs

Lines changed: 36 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,41 @@ class TreeBuilder extends Base {
2424
}
2525

2626
/**
27-
* Copies a given vdom tree and replaces child component references with the vdom of their matching components
28-
* @param {Object} vdom
29-
* @param {Number} [depth=-1]
30-
* The component replacement depth.
31-
* -1 will parse the full tree, 1 top level only, 2 include children, 3 include grandchildren
32-
* @param {Set<String>|null} [mergedChildIds=null] A set of component IDs to selectively expand.
27+
* Private helper to recursively build a tree, abstracting the child node key.
28+
* @param {Object} node The vdom or vnode to process.
29+
* @param {Number} depth The current recursion depth.
30+
* @param {Set<String>|null} mergedChildIds A set of component IDs to selectively expand.
31+
* @param {String} childKey The property name for child nodes ('cn' or 'childNodes').
3332
* @returns {Object}
33+
* @private
3434
*/
35-
getVdomTree(vdom, depth = -1, mergedChildIds = null) {
36-
if (!Neo.isObject(vdom)) {
37-
return vdom
35+
#buildTree(node, depth, mergedChildIds, childKey) {
36+
if (!Neo.isObject(node)) {
37+
return node
3838
}
3939

40-
let output = {...vdom}; // Shallow copy
40+
let output = {...node};
4141

42-
if (vdom.cn) {
43-
output.cn = [];
42+
if (node[childKey]) {
43+
output[childKey] = [];
4444

45-
vdom.cn.forEach(item => {
45+
node[childKey].forEach(item => {
4646
let currentItem = item,
4747
childDepth;
4848

4949
if (currentItem.componentId) {
5050
// Prune the branch only if we are at the boundary AND the child is not part of a merged update
5151
if (depth === 1 && !mergedChildIds?.has(currentItem.componentId)) {
52-
output.cn.push({componentId: 'neo-ignore', id: item.id || item.componentId});
53-
// Stop processing this branch
54-
return
52+
output[childKey].push({componentId: 'neo-ignore', id: item.id || item.componentId});
53+
return // Stop processing this branch
5554
}
5655
// Expand the branch if it's part of a merged update, or if the depth requires it
5756
else if (depth > 1 || depth === -1 || mergedChildIds?.has(currentItem.componentId)) {
5857
const component = ComponentManager.get(currentItem.componentId);
59-
if (component?.vdom) {
60-
currentItem = component.vdom
58+
// Use the correct tree type based on the childKey
59+
const componentTree = childKey === 'cn' ? component?.vdom : component?.vnode;
60+
if (componentTree) {
61+
currentItem = componentTree
6162
}
6263
}
6364
}
@@ -68,61 +69,34 @@ class TreeBuilder extends Base {
6869
childDepth = depth
6970
}
7071

71-
output.cn.push(this.getVdomTree(currentItem, childDepth, mergedChildIds))
72+
output[childKey].push(this.#buildTree(currentItem, childDepth, mergedChildIds, childKey))
7273
})
7374
}
7475

7576
return output
7677
}
7778

79+
7880
/**
79-
* Copies a given vnode tree and replaces child component references with the vnode of their matching components
80-
* @param {Object} vnode
81+
* Copies a given vdom tree and replaces child component references with their vdom.
82+
* @param {Object} vdom
8183
* @param {Number} [depth=-1]
82-
* The component replacement depth.
83-
* -1 will parse the full tree, 1 top level only, 2 include children, 3 include grandchildren
84-
* @param {Set<String>|null} [mergedChildIds=null] A set of component IDs to selectively expand.
84+
* @param {Set<String>|null} [mergedChildIds=null]
8585
* @returns {Object}
8686
*/
87-
getVnodeTree(vnode, depth = -1, mergedChildIds = null) {
88-
let output = {...vnode}; // Shallow copy
89-
90-
if (vnode.childNodes) {
91-
output.childNodes = [];
92-
93-
vnode.childNodes.forEach(item => {
94-
let currentItem = item,
95-
childDepth, component;
96-
97-
if (currentItem.componentId) {
98-
// Prune the branch only if we are at the boundary AND the child is not part of a merged update
99-
if (depth === 1 && !mergedChildIds?.has(currentItem.componentId)) {
100-
output.childNodes.push({componentId: 'neo-ignore', id: item.id || item.componentId});
101-
// Stop processing this branch
102-
return
103-
}
104-
// Expand the branch if it's part of a merged update, or if the depth requires it
105-
else if (depth > 1 || depth === -1 || mergedChildIds?.has(currentItem.componentId)) {
106-
component = ComponentManager.get(currentItem.componentId);
107-
108-
// Keep references in case there is no vnode (e.g. component not mounted yet)
109-
if (component?.vnode) {
110-
currentItem = component.vnode
111-
}
112-
}
113-
}
114-
115-
if (item.componentId) {
116-
childDepth = (depth === -1) ? -1 : Math.max(0, depth - 1)
117-
} else {
118-
childDepth = depth
119-
}
120-
121-
output.childNodes.push(this.getVnodeTree(currentItem, childDepth, mergedChildIds))
122-
})
123-
}
87+
getVdomTree(vdom, depth=-1, mergedChildIds=null) {
88+
return this.#buildTree(vdom, depth, mergedChildIds, 'cn')
89+
}
12490

125-
return output
91+
/**
92+
* Copies a given vnode tree and replaces child component references with their vnode.
93+
* @param {Object} vnode
94+
* @param {Number} [depth=-1]
95+
* @param {Set<String>|null} [mergedChildIds=null]
96+
* @returns {Object}
97+
*/
98+
getVnodeTree(vnode, depth=-1, mergedChildIds=null) {
99+
return this.#buildTree(vnode, depth, mergedChildIds, 'childNodes')
126100
}
127101
}
128102

0 commit comments

Comments
 (0)