Skip to content

Commit 89c7c98

Browse files
committed
feat: keepAlive组件的激活和卸载调用改为parentComponent实例调用
1 parent faa9ed4 commit 89c7c98

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

packages/runtime-core/src/renderer.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ type PatchChildrenFn = (
5252

5353
type UnmountFn = (
5454
vnode: VNode,
55+
parentComponent: ComponentInternalInstance,
5556
) => void
5657

5758

@@ -64,7 +65,7 @@ export type MountComponentFn = (
6465
initialVNode: VNode,
6566
container: RendererElement,
6667
anchor: RendererNode | null,
67-
rootComponent: ComponentInternalInstance | null,
68+
parentComponent: ComponentInternalInstance | null,
6869
) => void
6970

7071
export interface RendererInternals<HostNode = RendererNode, HostElement = RendererElement> {
@@ -305,7 +306,7 @@ function baseCreateRenderer(
305306

306307
if (n2.shapeFlag & ShapeFlags.COMPONENT_KEPT_ALIVE) {
307308
//如果当前组件是被缓存的组件就激活
308-
(n2!.keepAliveInstance.ctx as KeepAliveContext).activate(
309+
(parentComponent.ctx as KeepAliveContext).activate(
309310
n2,
310311
container,
311312
anchor,
@@ -388,36 +389,36 @@ function baseCreateRenderer(
388389
* @param children
389390
* @param start 卸载的开始节点默认第一个
390391
*/
391-
const unmountChildren = (children, start = 0) => {
392+
const unmountChildren = (children, parentComponent, start = 0) => {
392393
for (let i = start; i < children.length; i++) {
393-
unmount(children[i])
394+
unmount(children[i], parentComponent)
394395
}
395396
}
396397

397398
// 卸载元素
398-
const unmount = (vnode: VNode) => {
399+
const unmount = (vnode: VNode, parentComponent) => {
399400

400401
const {
401402
el,
402403
type,
403404
shapeFlag,
404-
keepAliveInstance,
405405
component,
406406
children,
407407
transition } = vnode
408408

409409

410410
//如果是碎片就卸载它的孩子节点
411411
if (type === Fragment) {
412-
children.forEach((v) => unmount(v))
412+
children.forEach((v) => unmount(v, parentComponent))
413413
}
414414

415415

416416
if (shapeFlag & ShapeFlags.COMPONENT) {
417417

418418
//判断vnode是否应该keepAlive,如果是就不需要卸载,直接让其无效
419419
if (shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE) {
420-
(keepAliveInstance.ctx as KeepAliveContext).deactivate(vnode)
420+
421+
(parentComponent.ctx as KeepAliveContext).deactivate(vnode)
421422
return
422423
}
423424

@@ -427,7 +428,7 @@ function baseCreateRenderer(
427428
bum && invokeArrayFns(bum)
428429

429430
//卸载组件本质上是卸载subTree
430-
unmount(subTree)
431+
unmount(subTree, parentComponent)
431432
//卸载组件之后
432433
um && invokeArrayFns(um)
433434
return
@@ -568,7 +569,7 @@ function baseCreateRenderer(
568569
* @param c2 新的children
569570
* @param container
570571
*/
571-
const patchKeyedChildren = (c1, c2, container, parentAnchor) => {
572+
const patchKeyedChildren = (c1, c2, container, parentAnchor, parentComponent) => {
572573
let e1 = c1.length - 1 //c1最大的索引值
573574
let e2 = c2.length - 1 //c2最大的索引值
574575
let i = 0 //从头开始比
@@ -686,7 +687,7 @@ function baseCreateRenderer(
686687
//===========common sequence unmount==============
687688
} else if (i > e2) { //有删除元素
688689
while (i <= e1) { //删除 i到e1之间的元素
689-
unmount(c1[i])
690+
unmount(c1[i], parentComponent)
690691
i++
691692
}
692693
} else {
@@ -762,10 +763,10 @@ function baseCreateRenderer(
762763

763764
patched++ //记录当前更新个数
764765
} else { //需要删除老的
765-
unmount(prevChild)
766+
unmount(prevChild, parentComponent)
766767
}
767768
} else {
768-
unmount(prevChild)
769+
unmount(prevChild, parentComponent)
769770
}
770771
}
771772

@@ -819,7 +820,7 @@ function baseCreateRenderer(
819820
* @param container
820821
* @param parentAnchor
821822
*/
822-
const patchSimpleKeyedChild = (c1, c2, container, parentAnchor) => {
823+
const patchSimpleKeyedChild = (c1, c2, container, parentAnchor, parentComponent) => {
823824
console.log(c1, c2)
824825
const newChildren = c2
825826
const oldChildren = c1
@@ -880,7 +881,7 @@ function baseCreateRenderer(
880881
const has = newChildren.find(newNode => isSameVNodeType(newNode, oldNode))
881882
if (!has) {
882883
//没有找到删除老的节点
883-
unmount(oldNode)
884+
unmount(oldNode, parentComponent)
884885
}
885886
}
886887

@@ -894,7 +895,7 @@ function baseCreateRenderer(
894895
* @param container
895896
* @param parentAnchor
896897
*/
897-
const patchDoubleSideKeyedChild = (c1, c2, container, parentAnchor) => {
898+
const patchDoubleSideKeyedChild = (c1, c2, container, parentAnchor, parentComponent) => {
898899
let newStartIndex = 0; // 新节点开始索引
899900
let oldStartIndex = 0; // 老节点开始索引
900901
let newEndIndex = c2.length - 1; // 新节点结束索引
@@ -1008,7 +1009,7 @@ function baseCreateRenderer(
10081009
} else if (newEndIndex < newStartIndex && oldStartIndex <= oldEndIndex) {
10091010
//c1中多余节点卸载
10101011
for (let i = oldStartIndex; i <= oldEndIndex; i++) {
1011-
unmount(c1[i])
1012+
unmount(c1[i], parentComponent)
10121013
}
10131014
}
10141015
}
@@ -1118,7 +1119,7 @@ function baseCreateRenderer(
11181119
// 旧孩子是数组
11191120
if (prevShapeFlag & ShapeFlags.ARRAY_CHILDREN) {
11201121
//卸载旧孩子
1121-
unmountChildren(c1)
1122+
unmountChildren(c1, parentComponent)
11221123
}
11231124

11241125
// 旧孩子是文本、是数组一起处理, 因为textContent直接覆盖为新文本
@@ -1131,7 +1132,7 @@ function baseCreateRenderer(
11311132
// 新孩子是数组
11321133
if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
11331134
//快速diff
1134-
patchKeyedChildren(c1, c2, el, anchor)
1135+
patchKeyedChildren(c1, c2, el, anchor, parentComponent)
11351136

11361137
//简单diff
11371138
// patchSimpleKeyedChild(c1, c2, el, anchor)
@@ -1140,7 +1141,7 @@ function baseCreateRenderer(
11401141
// patchDoubleSideKeyedChild(c1, c2, el, anchor)
11411142
} else {
11421143
//卸载旧孩子
1143-
unmountChildren(c1)
1144+
unmountChildren(c1, parentComponent)
11441145
}
11451146
} else {
11461147
//旧孩子是文本 清空旧孩子
@@ -1281,7 +1282,7 @@ function baseCreateRenderer(
12811282
const patch = (n1, n2, container, anchor = null, parentComponent = null) => {
12821283
//如果新节点和老节点不相等,删除老节点
12831284
if (n1 && !isSameVNodeType(n1, n2)) {
1284-
unmount(n1)
1285+
unmount(n1, parentComponent)
12851286
n1 = null
12861287
}
12871288

0 commit comments

Comments
 (0)