forked from neomjs/neo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeBuilder.mjs
More file actions
123 lines (106 loc) · 5.02 KB
/
Copy pathTreeBuilder.mjs
File metadata and controls
123 lines (106 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import Base from '../../core/Base.mjs';
import ComponentManager from '../../manager/Component.mjs';
/**
* A singleton utility class responsible for recursively building VDOM and VNode trees.
* It can expand component references within a tree structure into their full VDOM/VNode representations,
* supporting selective (asymmetric) tree expansion for optimized updates.
* @class Neo.util.vdom.TreeBuilder
* @extends Neo.core.Base
* @singleton
*/
class TreeBuilder extends Base {
static config = {
/**
* @member {String} className='Neo.util.vdom.TreeBuilder'
* @protected
*/
className: 'Neo.util.vdom.TreeBuilder',
/**
* @member {Boolean} singleton=true
* @protected
*/
singleton: true
}
/**
* Private helper to recursively build a tree, abstracting the child node key.
* @param {Object} node The vdom or vnode to process.
* @param {Number} depth The current recursion depth.
* @param {Set<String>|null} mergedChildIds A set of component IDs to selectively expand.
* @param {String} childKey The property name for child nodes ('cn' or 'childNodes').
* @returns {Object}
* @private
*/
#buildTree(node, depth, mergedChildIds, childKey) {
// We can not use Neo.isObject() here, since inside unit-test scenarios, we will import vdom.Helper into main threads.
// Inside this scenario, Neo.isObject() returns false for VNode instances
if (typeof node !== 'object' || node === null) {
return node
}
// JIT ID Generation (App Authority)
// If we are processing a VDOM tree (childKey === 'cn') and the node has no ID,
// we must generate one now to ensure deterministic identity before the VDOM leaves the App Worker.
if (childKey === 'cn' && !node.id) {
node.id = Neo.getId(node.vtype === 'text' ? 'vtext' : 'vnode')
}
let output = {...node}; // Shallow copy
if (node[childKey]) {
output[childKey] = [];
for (let i = 0, len = node[childKey].length; i < len; i++) {
let item = node[childKey][i],
currentItem = item,
childDepth;
if (currentItem.componentId) {
const component = ComponentManager.get(currentItem.componentId);
// Sparse Tree Generation & Scoped Updates
// We prune the branch (send a placeholder) if:
// 1. We are at the depth boundary (depth === 1) AND it's not a merged update.
// 2. We are in a Merged Update (mergedChildIds exists) AND this component is not in the AllowList (not dirty/bridge).
// Exception: We never prune if depth is -1 (Full Tree) or if the component is not mounted yet.
if (depth !== -1 && component?.vnode) {
const isExpandable = mergedChildIds?.has(currentItem.componentId);
if ((depth === 1 && !isExpandable) || (mergedChildIds && !isExpandable)) {
output[childKey].push({...currentItem, neoIgnore: true});
continue // Stop processing this branch, move to next item
}
}
// Expand the branch if it's part of a merged update, or if the depth requires it, OR if the vnode is missing
if (depth > 1 || depth === -1 || mergedChildIds?.has(currentItem.componentId) || !component?.vnode) {
// Use the correct tree type based on the childKey
const componentTree = childKey === 'cn' ? component?.vdom : component?.vnode;
if (componentTree) {
currentItem = componentTree
}
}
}
if (item.componentId) {
childDepth = (depth === -1) ? -1 : Math.max(0, depth - 1)
} else {
childDepth = depth
}
output[childKey].push(this.#buildTree(currentItem, childDepth, mergedChildIds, childKey))
}
}
return output
}
/**
* Copies a given vdom tree and replaces child component references with their vdom.
* @param {Object} vdom
* @param {Number} [depth=-1]
* @param {Set<String>|null} [mergedChildIds=null]
* @returns {Object}
*/
getVdomTree(vdom, depth=-1, mergedChildIds=null) {
return this.#buildTree(vdom, depth, mergedChildIds, 'cn')
}
/**
* Copies a given vnode tree and replaces child component references with their vnode.
* @param {Object} vnode
* @param {Number} [depth=-1]
* @param {Set<String>|null} [mergedChildIds=null]
* @returns {Object}
*/
getVnodeTree(vnode, depth=-1, mergedChildIds=null) {
return this.#buildTree(vnode, depth, mergedChildIds, 'childNodes')
}
}
export default Neo.setupClass(TreeBuilder);