11import {
2+ type EffectScope ,
23 type ShallowRef ,
3- getCurrentScope ,
4+ effectScope ,
45 isReactive ,
56 proxyRefs ,
67 shallowRef ,
7- traverse ,
88 triggerRef ,
99} from '@vue/reactivity'
1010import { isArray , isObject , isString } from '@vue/shared'
@@ -18,17 +18,11 @@ import { type Block, type Fragment, fragmentKey } from './apiRender'
1818import { warn } from './warning'
1919import { currentInstance } from './component'
2020import { componentKey } from './component'
21- import { BlockEffectScope , isRenderEffectScope } from './blockEffectScope'
22- import {
23- createChildFragmentDirectives ,
24- invokeWithMount ,
25- invokeWithUnmount ,
26- invokeWithUpdate ,
27- } from './directivesChildFragment'
2821import type { DynamicSlot } from './componentSlots'
22+ import { renderEffect } from './renderEffect'
2923
3024interface ForBlock extends Fragment {
31- scope : BlockEffectScope
25+ scope : EffectScope
3226 state : [
3327 item : ShallowRef < any > ,
3428 key : ShallowRef < any > ,
@@ -53,34 +47,24 @@ export const createFor = (
5347 let oldBlocks : ForBlock [ ] = [ ]
5448 let newBlocks : ForBlock [ ]
5549 let parent : ParentNode | undefined | null
56- const update = getMemo ? updateWithMemo : updateWithoutMemo
57- const parentScope = getCurrentScope ( ) !
5850 const parentAnchor = __DEV__ ? createComment ( 'for' ) : createTextNode ( )
5951 const ref : Fragment = {
6052 nodes : oldBlocks ,
6153 [ fragmentKey ] : true ,
6254 }
6355
6456 const instance = currentInstance !
65- if ( __DEV__ && ( ! instance || ! isRenderEffectScope ( parentScope ) ) ) {
57+ if ( __DEV__ && ! instance ) {
6658 warn ( 'createFor() can only be used inside setup()' )
6759 }
6860
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 )
8063
8164 return ref
8265
83- function doFor ( source : any ) {
66+ function renderList ( ) {
67+ const source = src ( )
8468 const newLength = getLength ( source )
8569 const oldLength = oldBlocks . length
8670 newBlocks = new Array ( newLength )
@@ -265,7 +249,7 @@ export const createFor = (
265249 idx : number ,
266250 anchor : Node = parentAnchor ,
267251 ) : ForBlock {
268- const scope = new BlockEffectScope ( instance , parentScope )
252+ const scope = effectScope ( )
269253
270254 const [ item , key , index ] = getItem ( source , idx )
271255 const state = [
@@ -283,11 +267,9 @@ export const createFor = (
283267 } )
284268 block . nodes = scope . run ( ( ) => renderItem ( proxyRefs ( state ) ) ) !
285269
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 )
291273
292274 return block
293275 }
@@ -326,7 +308,6 @@ export const createFor = (
326308 }
327309
328310 if ( needsUpdate ) setState ( block , newItem , newKey , newIndex )
329- invokeWithUpdate ( block . scope )
330311 }
331312
332313 function updateWithoutMemo (
@@ -344,13 +325,11 @@ export const createFor = (
344325 ( ! isReactive ( newItem ) && isObject ( newItem ) )
345326
346327 if ( needsUpdate ) setState ( block , newItem , newKey , newIndex )
347- invokeWithUpdate ( block . scope )
348328 }
349329
350330 function unmount ( { nodes, scope } : ForBlock ) {
351- invokeWithUnmount ( scope , ( ) => {
352- removeBlock ( nodes , parent ! )
353- } )
331+ removeBlock ( nodes , parent ! )
332+ scope . stop ( )
354333 }
355334}
356335
0 commit comments