@@ -72,52 +72,53 @@ export const _for = (el: Element, exp: string, ctx: Context) => {
72
72
const map : KeyToIndexMap = new Map ( )
73
73
const scopes : ChildScope [ ] = [ ]
74
74
75
- const createScope = (
76
- value : any ,
77
- index : number ,
78
- objKey ?: string
79
- ) : ChildScope => {
80
- const data : any = { }
81
- if ( destructureBindings ) {
82
- destructureBindings . forEach (
83
- ( b , i ) => ( data [ b ] = value [ isArrayDestructure ? i : b ] )
84
- )
85
- } else {
86
- data [ valueExp ] = value
87
- }
88
- if ( objKey ) {
89
- indexExp && ( data [ indexExp ] = objKey )
90
- objIndexExp && ( data [ objIndexExp ] = index )
91
- } else {
92
- indexExp && ( data [ indexExp ] = index )
93
- }
94
- const childCtx = createScopedContext ( ctx , data )
95
- const key = keyExp ? evaluate ( childCtx . scope , keyExp ) : index
96
- map . set ( key , index )
97
- return {
98
- ctx : childCtx ,
99
- key
100
- }
101
- }
102
-
103
75
if ( isArray ( source ) ) {
104
76
for ( let i = 0 ; i < source . length ; i ++ ) {
105
- scopes . push ( createScope ( source [ i ] , i ) )
77
+ scopes . push ( createScope ( map , source [ i ] , i ) )
106
78
}
107
79
} else if ( typeof source === 'number' ) {
108
80
for ( let i = 0 ; i < source ; i ++ ) {
109
- scopes . push ( createScope ( i + 1 , i ) )
81
+ scopes . push ( createScope ( map , i + 1 , i ) )
110
82
}
111
83
} else if ( isObject ( source ) ) {
112
84
let i = 0
113
85
for ( const key in source ) {
114
- scopes . push ( createScope ( source [ key ] , i ++ , key ) )
86
+ scopes . push ( createScope ( map , source [ key ] , i ++ , key ) )
115
87
}
116
88
}
117
89
118
90
return [ scopes , map ]
119
91
}
120
92
93
+ const createScope = (
94
+ map : KeyToIndexMap ,
95
+ value : any ,
96
+ index : number ,
97
+ objKey ?: string
98
+ ) : ChildScope => {
99
+ const data : any = { }
100
+ if ( destructureBindings ) {
101
+ destructureBindings . forEach (
102
+ ( b , i ) => ( data [ b ] = value [ isArrayDestructure ? i : b ] )
103
+ )
104
+ } else {
105
+ data [ valueExp ] = value
106
+ }
107
+ if ( objKey ) {
108
+ indexExp && ( data [ indexExp ] = objKey )
109
+ objIndexExp && ( data [ objIndexExp ] = index )
110
+ } else {
111
+ indexExp && ( data [ indexExp ] = index )
112
+ }
113
+ const childCtx = createScopedContext ( ctx , data )
114
+ const key = keyExp ? evaluate ( childCtx . scope , keyExp ) : index
115
+ map . set ( key , index )
116
+ return {
117
+ ctx : childCtx ,
118
+ key
119
+ }
120
+ }
121
+
121
122
const mountBlock = ( { ctx, key } : ChildScope , ref : Node ) => {
122
123
const block = new Block ( el , ctx )
123
124
block . key = key
@@ -130,13 +131,10 @@ export const _for = (el: Element, exp: string, ctx: Context) => {
130
131
const prevKeyToIndexMap = keyToIndexMap
131
132
; [ scopes , keyToIndexMap ] = createChildScopes ( source )
132
133
if ( ! mounted ) {
133
- blocks = [ ]
134
- for ( const scope of scopes ) {
135
- blocks . push ( mountBlock ( scope , anchor ) )
136
- }
134
+ blocks = scopes . map ( ( s ) => mountBlock ( s , anchor ) )
137
135
mounted = true
138
136
} else {
139
- const nextBlocks : Block [ ] = new Array ( scopes . length )
137
+ const nextBlocks : Block [ ] = [ ]
140
138
for ( let i = 0 ; i < blocks . length ; i ++ ) {
141
139
if ( ! keyToIndexMap . has ( blocks [ i ] . key ) ) {
142
140
blocks [ i ] . remove ( )
0 commit comments