Skip to content

Commit a70688c

Browse files
authored
docs: add some comments for patch-context.js
1 parent ccdf3bf commit a70688c

File tree

1 file changed

+119
-16
lines changed

1 file changed

+119
-16
lines changed

src/server/patch-context.js

Lines changed: 119 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,15 @@ export class PatchContext {
100100
}
101101

102102
/**
103-
* 合并子元素节点
104-
* 注意:基于抽象语法树的推导优化依赖于稳定的模板结构,如果开发者使用了自定义 render,则无法对其进行语法树级别的推导优化
103+
* English:
104+
* Perform ast optimization on element type VNode.
105+
*
106+
* AST-based optimization relies on a stable template structure, which cannot be
107+
* optimized if the developer uses a custom render
108+
*
109+
* 中文:
110+
* 对元素类型节点执行 ast 优化
111+
* 基于 ast 的优化依赖于稳定的模板结构,如果开发者使用了自定义 render,则无法做出优化
105112
*/
106113
astShaking(patchState: PatchState) {
107114
const { endTag, staticChildren, ast } = patchState
@@ -121,10 +128,23 @@ export class PatchContext {
121128
return preV
122129
}, [])
123130

124-
/** 子节点是否全部都是静态节点 */
131+
/**
132+
* English:
133+
* Detect whether all child nodes are static
134+
*
135+
* 中文:
136+
* 检测子节点是否全部都是静态节点
137+
*/
125138
const childStatic = elements.length === 0 || (elements.length === 1 && typeof elements[0] === 'string')
126139

127-
/** 如果当前语法树并未匹配到组件 render 模板 则仅判断当前节点及其子节点是否全是静态 */
140+
/**
141+
* English:
142+
* If the current ast does not match the component's rendering template, detect
143+
* whether the current node and its children are all static.
144+
*
145+
* 中文:
146+
* 如果当前 ast 并未匹配到组件 render 模板 则仅判断当前节点及其子节点是否全是静态
147+
*/
128148
if (ast.unMatchedAst) {
129149
if (ast.ssrString !== undefined && childStatic) {
130150
const str = ast.ssrString + elements[0] + endTag
@@ -134,11 +154,15 @@ export class PatchContext {
134154
}
135155

136156
/**
157+
* English:
158+
* Resetting the current ast subtree.
159+
* If children exist and the child element is not static, the ast for children is reconstructed.
160+
*
161+
* 中文:
137162
* 重新设置当前节点抽象语法树的子树
138-
* ast 不一定有 children 子元素,但是其子元素有可能全部都是静态的,例如常量for循环
163+
* 如果 children 存在,并且子元素非静态,则重新为 children 构建抽象语法树
139164
*/
140165
const children = getVNodeAstChildren(ast)
141-
/** 如果存在 AST 子元素,并且子元素非静态,则构建抽象语法树 */
142166
if (children && !childStatic) {
143167
children.elements = elements.map(v => {
144168
if (typeof v === 'string') {
@@ -151,7 +175,13 @@ export class PatchContext {
151175
})
152176
}
153177

154-
/** 如果当前节点和子节点都是静态节点,则将节点合并 */
178+
/**
179+
* English:
180+
* If both the current node and child nodes are static, the nodes are combined.
181+
*
182+
* 中文:
183+
* 如果当前节点和子节点都是静态节点,则将节点合并。
184+
* */
155185
if (ast.ssrString !== undefined && childStatic){
156186
const str = ast.ssrString + elements[0] + endTag
157187
Object.assign(ast, callExpression(
@@ -161,22 +191,36 @@ export class PatchContext {
161191
}
162192

163193
/**
194+
* English:
195+
* Optimizing the child tree of the current node abstract syntax tree
196+
*
197+
* 中文:
164198
* 优化当前节点抽象语法树的子树
165199
*/
166200
this.reduceAstChildren(patchState)
167201
}
168202

169203
/**
204+
* English:
205+
* Merge component-level VNodes.
206+
* If the entire component is static, the result is passed up.
207+
* If the component is dynamic, retain the rendering function.
208+
*
209+
* 中文:
170210
* 合并组件级节点
171-
* 如果整个组件都是静态的,则将结果向上传递,如果组件是动态的,则保留渲染函数
172-
* * todo astComponentShaking 里面需要考虑到 unMatchedAst 静态情况
211+
* 如果整个组件都是静态的,则将结果向上传递
212+
* 如果组件是动态的,则保留渲染函数
173213
*/
174214
astComponentShaking(patchState: PatchState) {
175215
const prevAst = patchState.prevAst
176216
const ssrRenderAst = prevAst.ssrRenderAst
177217

178218
/**
179-
* 当前组件无优化
219+
* English:
220+
* Current component nodes are not optimized.
221+
*
222+
* 中文:
223+
* 当前组件节点无优化
180224
*/
181225
if (ssrRenderAst.unMatchedAst && !ssrRenderAst.ssrStatic) {
182226
return this.setSSRRenderTree(prevAst)
@@ -185,6 +229,10 @@ export class PatchContext {
185229
let renderAst = null
186230

187231
/**
232+
* English:
233+
* The current component does not match ast, but is a static component
234+
*
235+
* 中文:
188236
* 当前组件虽未匹配 ast,但是为静态组件
189237
*/
190238
if (ssrRenderAst.unMatchedAst && ssrRenderAst.ssrStatic) {
@@ -194,8 +242,12 @@ export class PatchContext {
194242
}
195243

196244
/**
197-
* 如果当前组件已被优化成静态组件
198-
* 则将当前组件的值与父组件关联
245+
* English:
246+
* If the current component has been optimized to be static, associate the
247+
* value of the current component with the parent ast.
248+
*
249+
* 中文:
250+
* 如果当前组件已被优化成静态组件,则将当前组件的值与父 ast 节点关联
199251
*/
200252
const value = getStatisAstComponentValue(renderAst)
201253
if (value) {
@@ -206,6 +258,48 @@ export class PatchContext {
206258
this.setSSRRenderTree(prevAst)
207259
}
208260

261+
/**
262+
* English:
263+
* Generate a new rendering chain based on Ast's optimization information.
264+
*
265+
* The information for the rendering chain is stored in ssrRenderTree, which
266+
* has the following structure.
267+
* {
268+
* render() {},
269+
* children: [
270+
* {
271+
* render() {},
272+
* children: []
273+
* },
274+
* {
275+
* render() {},
276+
* children: []
277+
* }
278+
* ]
279+
* }
280+
* Deep priority traverses ssrRenderTree and executes the render function to
281+
* render the application by optimized path.
282+
*
283+
* 中文:
284+
* 根据 Ast 的优化信息生成全新的渲染链
285+
* 渲染链的信息保存在 ssrRenderTree 中,ssrRenderTree 的结构如下所示:
286+
* {
287+
* render() {},
288+
* children: [
289+
* {
290+
* render() {},
291+
* children: []
292+
* },
293+
* {
294+
* render() {},
295+
* children: []
296+
* }
297+
* ]
298+
* }
299+
* 深度优先遍历 ssrRenderTree,并执行 render 函数,即可按优化路径渲染应用
300+
*
301+
* @param {Object} ast
302+
*/
209303
setSSRRenderTree(ast: Object) {
210304
let renderStr = 'function render() {}'
211305
if (ast.ssrStatic && ast.ssrString !== undefined) {
@@ -277,19 +371,28 @@ export class PatchContext {
277371
}
278372

279373
/**
374+
* English:
375+
* Optimizing the child tree of the current ast
376+
*
377+
* 中文:
280378
* 优化当前节点抽象语法树的子树
281-
* @param {*} patchState
379+
* @param {PatchState} patchState
282380
*/
283381
reduceAstChildren(patchState: PatchState) {
284382
const ast = patchState.ast
285383
const children = getVNodeAstChildren(ast)
286384
/**
385+
* English:
386+
* If the current element node is an ssrNode node, and there is only one ssrNode child,
387+
* and the child has no grandchildren, elevate the child node.
388+
*
389+
* 中文:
287390
* 如果当前元素节点为 ssrNode 节点,且只有一个 ssrNode 子节点,并且子节点没有孙节点,则把子节点提升
288391
*/
289392
if (
290-
children && children.elements.length === 1 && //只有一个子节点
291-
isSSRNodeAst(ast) && isSSRNodeAst(children.elements[0]) && //当前元素节点为 ssrNode 节点, 子节点为 ssrNode 节点
292-
children.elements[0].arguments.length === 1 // 子节点没有孙节点
393+
children && children.elements.length === 1 && // Only one child node.
394+
isSSRNodeAst(ast) && isSSRNodeAst(children.elements[0]) && // The current element node is an ssrNode node and the child node is an ssrNode node
395+
children.elements[0].arguments.length === 1 // Child node has no grand node.
293396
) {
294397
const node = children.elements[0].arguments[0]
295398
const args = ast.arguments

0 commit comments

Comments
 (0)