@@ -38,14 +38,19 @@ export const createFor = (
38
38
renderItem : ( block : ForBlock [ 'state' ] ) => Block ,
39
39
getKey ?: ( item : any , key : any , index ?: number ) => any ,
40
40
getMemo ?: ( item : any , key : any , index ?: number ) => any [ ] ,
41
+ container ?: ParentNode ,
41
42
hydrationNode ?: Node ,
42
43
once ?: boolean ,
43
44
) : Fragment => {
44
45
let isMounted = false
45
46
let oldBlocks : ForBlock [ ] = [ ]
46
47
let newBlocks : ForBlock [ ]
47
48
let parent : ParentNode | undefined | null
48
- const parentAnchor = __DEV__ ? createComment ( 'for' ) : createTextNode ( )
49
+ const parentAnchor = container
50
+ ? undefined
51
+ : __DEV__
52
+ ? createComment ( 'for' )
53
+ : createTextNode ( )
49
54
const ref : Fragment = {
50
55
nodes : oldBlocks ,
51
56
[ fragmentKey ] : true ,
@@ -71,14 +76,22 @@ export const createFor = (
71
76
isMounted = true
72
77
mountList ( source )
73
78
} else {
74
- parent = parent || parentAnchor . parentNode
79
+ parent = parent || container || parentAnchor ! . parentNode
75
80
if ( ! oldLength ) {
76
81
// fast path for all new
77
82
mountList ( source )
78
83
} else if ( ! newLength ) {
79
- // fast path for clearing
80
- for ( let i = 0 ; i < oldLength ; i ++ ) {
81
- unmount ( oldBlocks [ i ] )
84
+ // fast path for all removed
85
+ if ( container ) {
86
+ container . textContent = ''
87
+ for ( let i = 0 ; i < oldLength ; i ++ ) {
88
+ oldBlocks [ i ] . scope . stop ( )
89
+ }
90
+ } else {
91
+ // fast path for clearing
92
+ for ( let i = 0 ; i < oldLength ; i ++ ) {
93
+ unmount ( oldBlocks [ i ] )
94
+ }
82
95
}
83
96
} else if ( ! getKey ) {
84
97
// unkeyed fast path
@@ -239,13 +252,16 @@ export const createFor = (
239
252
}
240
253
}
241
254
242
- ref . nodes = [ ( oldBlocks = newBlocks ) , parentAnchor ]
255
+ ref . nodes = [ ( oldBlocks = newBlocks ) ]
256
+ if ( parentAnchor ) {
257
+ ref . nodes . push ( parentAnchor )
258
+ }
243
259
}
244
260
245
261
function mount (
246
262
source : any ,
247
263
idx : number ,
248
- anchor : Node = parentAnchor ,
264
+ anchor : Node | undefined = parentAnchor ,
249
265
) : ForBlock {
250
266
const scope = effectScope ( )
251
267
0 commit comments