1
1
import {
2
+ type EffectScope ,
2
3
type ShallowRef ,
3
- getCurrentScope ,
4
+ effectScope ,
4
5
isReactive ,
5
6
proxyRefs ,
6
7
shallowRef ,
7
- traverse ,
8
8
triggerRef ,
9
9
} from '@vue/reactivity'
10
10
import { isArray , isObject , isString } from '@vue/shared'
@@ -18,17 +18,11 @@ import { type Block, type Fragment, fragmentKey } from './apiRender'
18
18
import { warn } from './warning'
19
19
import { currentInstance } from './component'
20
20
import { componentKey } from './component'
21
- import { BlockEffectScope , isRenderEffectScope } from './blockEffectScope'
22
- import {
23
- createChildFragmentDirectives ,
24
- invokeWithMount ,
25
- invokeWithUnmount ,
26
- invokeWithUpdate ,
27
- } from './directivesChildFragment'
28
21
import type { DynamicSlot } from './componentSlots'
22
+ import { renderEffect } from './renderEffect'
29
23
30
24
interface ForBlock extends Fragment {
31
- scope : BlockEffectScope
25
+ scope : EffectScope
32
26
state : [
33
27
item : ShallowRef < any > ,
34
28
key : ShallowRef < any > ,
@@ -53,34 +47,24 @@ export const createFor = (
53
47
let oldBlocks : ForBlock [ ] = [ ]
54
48
let newBlocks : ForBlock [ ]
55
49
let parent : ParentNode | undefined | null
56
- const update = getMemo ? updateWithMemo : updateWithoutMemo
57
- const parentScope = getCurrentScope ( ) !
58
50
const parentAnchor = __DEV__ ? createComment ( 'for' ) : createTextNode ( )
59
51
const ref : Fragment = {
60
52
nodes : oldBlocks ,
61
53
[ fragmentKey ] : true ,
62
54
}
63
55
64
56
const instance = currentInstance !
65
- if ( __DEV__ && ( ! instance || ! isRenderEffectScope ( parentScope ) ) ) {
57
+ if ( __DEV__ && ! instance ) {
66
58
warn ( 'createFor() can only be used inside setup()' )
67
59
}
68
60
69
- createChildFragmentDirectives (
70
- parentAnchor ,
71
- ( ) => oldBlocks . map ( b => b . scope ) ,
72
- // source getter
73
- ( ) => traverse ( src ( ) , 1 ) ,
74
- // init cb
75
- getValue => doFor ( getValue ( ) ) ,
76
- // effect cb
77
- getValue => doFor ( getValue ( ) ) ,
78
- once ,
79
- )
61
+ const update = getMemo ? updateWithMemo : updateWithoutMemo
62
+ once ? renderList ( ) : renderEffect ( renderList )
80
63
81
64
return ref
82
65
83
- function doFor ( source : any ) {
66
+ function renderList ( ) {
67
+ const source = src ( )
84
68
const newLength = getLength ( source )
85
69
const oldLength = oldBlocks . length
86
70
newBlocks = new Array ( newLength )
@@ -265,7 +249,7 @@ export const createFor = (
265
249
idx : number ,
266
250
anchor : Node = parentAnchor ,
267
251
) : ForBlock {
268
- const scope = new BlockEffectScope ( instance , parentScope )
252
+ const scope = effectScope ( )
269
253
270
254
const [ item , key , index ] = getItem ( source , idx )
271
255
const state = [
@@ -283,11 +267,9 @@ export const createFor = (
283
267
} )
284
268
block . nodes = scope . run ( ( ) => renderItem ( proxyRefs ( state ) ) ) !
285
269
286
- invokeWithMount ( scope , ( ) => {
287
- // TODO v-memo
288
- // if (getMemo) block.update()
289
- if ( parent ) insert ( block . nodes , parent , anchor )
290
- } )
270
+ // TODO v-memo
271
+ // if (getMemo) block.update()
272
+ if ( parent ) insert ( block . nodes , parent , anchor )
291
273
292
274
return block
293
275
}
@@ -326,7 +308,6 @@ export const createFor = (
326
308
}
327
309
328
310
if ( needsUpdate ) setState ( block , newItem , newKey , newIndex )
329
- invokeWithUpdate ( block . scope )
330
311
}
331
312
332
313
function updateWithoutMemo (
@@ -344,13 +325,11 @@ export const createFor = (
344
325
( ! isReactive ( newItem ) && isObject ( newItem ) )
345
326
346
327
if ( needsUpdate ) setState ( block , newItem , newKey , newIndex )
347
- invokeWithUpdate ( block . scope )
348
328
}
349
329
350
330
function unmount ( { nodes, scope } : ForBlock ) {
351
- invokeWithUnmount ( scope , ( ) => {
352
- removeBlock ( nodes , parent ! )
353
- } )
331
+ removeBlock ( nodes , parent ! )
332
+ scope . stop ( )
354
333
}
355
334
}
356
335
0 commit comments