@@ -52,6 +52,7 @@ type PatchChildrenFn = (
52
52
53
53
type UnmountFn = (
54
54
vnode : VNode ,
55
+ parentComponent : ComponentInternalInstance ,
55
56
) => void
56
57
57
58
@@ -64,7 +65,7 @@ export type MountComponentFn = (
64
65
initialVNode : VNode ,
65
66
container : RendererElement ,
66
67
anchor : RendererNode | null ,
67
- rootComponent : ComponentInternalInstance | null ,
68
+ parentComponent : ComponentInternalInstance | null ,
68
69
) => void
69
70
70
71
export interface RendererInternals < HostNode = RendererNode , HostElement = RendererElement > {
@@ -305,7 +306,7 @@ function baseCreateRenderer(
305
306
306
307
if ( n2 . shapeFlag & ShapeFlags . COMPONENT_KEPT_ALIVE ) {
307
308
//如果当前组件是被缓存的组件就激活
308
- ( n2 ! . keepAliveInstance . ctx as KeepAliveContext ) . activate (
309
+ ( parentComponent . ctx as KeepAliveContext ) . activate (
309
310
n2 ,
310
311
container ,
311
312
anchor ,
@@ -388,36 +389,36 @@ function baseCreateRenderer(
388
389
* @param children
389
390
* @param start 卸载的开始节点默认第一个
390
391
*/
391
- const unmountChildren = ( children , start = 0 ) => {
392
+ const unmountChildren = ( children , parentComponent , start = 0 ) => {
392
393
for ( let i = start ; i < children . length ; i ++ ) {
393
- unmount ( children [ i ] )
394
+ unmount ( children [ i ] , parentComponent )
394
395
}
395
396
}
396
397
397
398
// 卸载元素
398
- const unmount = ( vnode : VNode ) => {
399
+ const unmount = ( vnode : VNode , parentComponent ) => {
399
400
400
401
const {
401
402
el,
402
403
type,
403
404
shapeFlag,
404
- keepAliveInstance,
405
405
component,
406
406
children,
407
407
transition } = vnode
408
408
409
409
410
410
//如果是碎片就卸载它的孩子节点
411
411
if ( type === Fragment ) {
412
- children . forEach ( ( v ) => unmount ( v ) )
412
+ children . forEach ( ( v ) => unmount ( v , parentComponent ) )
413
413
}
414
414
415
415
416
416
if ( shapeFlag & ShapeFlags . COMPONENT ) {
417
417
418
418
//判断vnode是否应该keepAlive,如果是就不需要卸载,直接让其无效
419
419
if ( shapeFlag & ShapeFlags . COMPONENT_SHOULD_KEEP_ALIVE ) {
420
- ( keepAliveInstance . ctx as KeepAliveContext ) . deactivate ( vnode )
420
+
421
+ ( parentComponent . ctx as KeepAliveContext ) . deactivate ( vnode )
421
422
return
422
423
}
423
424
@@ -427,7 +428,7 @@ function baseCreateRenderer(
427
428
bum && invokeArrayFns ( bum )
428
429
429
430
//卸载组件本质上是卸载subTree
430
- unmount ( subTree )
431
+ unmount ( subTree , parentComponent )
431
432
//卸载组件之后
432
433
um && invokeArrayFns ( um )
433
434
return
@@ -568,7 +569,7 @@ function baseCreateRenderer(
568
569
* @param c2 新的children
569
570
* @param container
570
571
*/
571
- const patchKeyedChildren = ( c1 , c2 , container , parentAnchor ) => {
572
+ const patchKeyedChildren = ( c1 , c2 , container , parentAnchor , parentComponent ) => {
572
573
let e1 = c1 . length - 1 //c1最大的索引值
573
574
let e2 = c2 . length - 1 //c2最大的索引值
574
575
let i = 0 //从头开始比
@@ -686,7 +687,7 @@ function baseCreateRenderer(
686
687
//===========common sequence unmount==============
687
688
} else if ( i > e2 ) { //有删除元素
688
689
while ( i <= e1 ) { //删除 i到e1之间的元素
689
- unmount ( c1 [ i ] )
690
+ unmount ( c1 [ i ] , parentComponent )
690
691
i ++
691
692
}
692
693
} else {
@@ -762,10 +763,10 @@ function baseCreateRenderer(
762
763
763
764
patched ++ //记录当前更新个数
764
765
} else { //需要删除老的
765
- unmount ( prevChild )
766
+ unmount ( prevChild , parentComponent )
766
767
}
767
768
} else {
768
- unmount ( prevChild )
769
+ unmount ( prevChild , parentComponent )
769
770
}
770
771
}
771
772
@@ -819,7 +820,7 @@ function baseCreateRenderer(
819
820
* @param container
820
821
* @param parentAnchor
821
822
*/
822
- const patchSimpleKeyedChild = ( c1 , c2 , container , parentAnchor ) => {
823
+ const patchSimpleKeyedChild = ( c1 , c2 , container , parentAnchor , parentComponent ) => {
823
824
console . log ( c1 , c2 )
824
825
const newChildren = c2
825
826
const oldChildren = c1
@@ -880,7 +881,7 @@ function baseCreateRenderer(
880
881
const has = newChildren . find ( newNode => isSameVNodeType ( newNode , oldNode ) )
881
882
if ( ! has ) {
882
883
//没有找到删除老的节点
883
- unmount ( oldNode )
884
+ unmount ( oldNode , parentComponent )
884
885
}
885
886
}
886
887
@@ -894,7 +895,7 @@ function baseCreateRenderer(
894
895
* @param container
895
896
* @param parentAnchor
896
897
*/
897
- const patchDoubleSideKeyedChild = ( c1 , c2 , container , parentAnchor ) => {
898
+ const patchDoubleSideKeyedChild = ( c1 , c2 , container , parentAnchor , parentComponent ) => {
898
899
let newStartIndex = 0 ; // 新节点开始索引
899
900
let oldStartIndex = 0 ; // 老节点开始索引
900
901
let newEndIndex = c2 . length - 1 ; // 新节点结束索引
@@ -1008,7 +1009,7 @@ function baseCreateRenderer(
1008
1009
} else if ( newEndIndex < newStartIndex && oldStartIndex <= oldEndIndex ) {
1009
1010
//c1中多余节点卸载
1010
1011
for ( let i = oldStartIndex ; i <= oldEndIndex ; i ++ ) {
1011
- unmount ( c1 [ i ] )
1012
+ unmount ( c1 [ i ] , parentComponent )
1012
1013
}
1013
1014
}
1014
1015
}
@@ -1118,7 +1119,7 @@ function baseCreateRenderer(
1118
1119
// 旧孩子是数组
1119
1120
if ( prevShapeFlag & ShapeFlags . ARRAY_CHILDREN ) {
1120
1121
//卸载旧孩子
1121
- unmountChildren ( c1 )
1122
+ unmountChildren ( c1 , parentComponent )
1122
1123
}
1123
1124
1124
1125
// 旧孩子是文本、是数组一起处理, 因为textContent直接覆盖为新文本
@@ -1131,7 +1132,7 @@ function baseCreateRenderer(
1131
1132
// 新孩子是数组
1132
1133
if ( shapeFlag & ShapeFlags . ARRAY_CHILDREN ) {
1133
1134
//快速diff
1134
- patchKeyedChildren ( c1 , c2 , el , anchor )
1135
+ patchKeyedChildren ( c1 , c2 , el , anchor , parentComponent )
1135
1136
1136
1137
//简单diff
1137
1138
// patchSimpleKeyedChild(c1, c2, el, anchor)
@@ -1140,7 +1141,7 @@ function baseCreateRenderer(
1140
1141
// patchDoubleSideKeyedChild(c1, c2, el, anchor)
1141
1142
} else {
1142
1143
//卸载旧孩子
1143
- unmountChildren ( c1 )
1144
+ unmountChildren ( c1 , parentComponent )
1144
1145
}
1145
1146
} else {
1146
1147
//旧孩子是文本 清空旧孩子
@@ -1281,7 +1282,7 @@ function baseCreateRenderer(
1281
1282
const patch = ( n1 , n2 , container , anchor = null , parentComponent = null ) => {
1282
1283
//如果新节点和老节点不相等,删除老节点
1283
1284
if ( n1 && ! isSameVNodeType ( n1 , n2 ) ) {
1284
- unmount ( n1 )
1285
+ unmount ( n1 , parentComponent )
1285
1286
n1 = null
1286
1287
}
1287
1288
0 commit comments