@@ -124,38 +124,43 @@ export const _for = (el: Element, exp: string, ctx: Context) => {
124
124
blocks = childCtxs . map ( ( s ) => mountBlock ( s , anchor ) )
125
125
mounted = true
126
126
} else {
127
- const nextBlocks : Block [ ] = [ ]
128
127
for ( let i = 0 ; i < blocks . length ; i ++ ) {
129
128
if ( ! keyToIndexMap . has ( blocks [ i ] . key ) ) {
130
129
blocks [ i ] . remove ( )
131
130
}
132
131
}
133
-
132
+
133
+ const nextBlocks : Block [ ] = [ ]
134
134
let i = childCtxs . length
135
+ let nextBlock : Block | undefined
136
+ let prevMovedBlock : Block | undefined
135
137
while ( i -- ) {
136
138
const childCtx = childCtxs [ i ]
137
139
const oldIndex = prevKeyToIndexMap . get ( childCtx . key )
138
- const next = childCtxs [ i + 1 ]
139
- const nextBlockOldIndex = next && prevKeyToIndexMap . get ( next . key )
140
- const nextBlock =
141
- nextBlockOldIndex == null ? undefined : blocks [ nextBlockOldIndex ]
140
+ let block
142
141
if ( oldIndex == null ) {
143
142
// new
144
- nextBlocks [ i ] = mountBlock (
143
+ block = mountBlock (
145
144
childCtx ,
146
145
nextBlock ? nextBlock . el : anchor
147
146
)
148
147
} else {
149
148
// update
150
- const block = ( nextBlocks [ i ] = blocks [ oldIndex ] )
149
+ block = blocks [ oldIndex ]
151
150
Object . assign ( block . ctx . scope , childCtx . scope )
152
151
if ( oldIndex !== i ) {
153
152
// moved
154
- if ( blocks [ oldIndex + 1 ] !== nextBlock ) {
153
+ if (
154
+ blocks [ oldIndex + 1 ] !== nextBlock ||
155
+ // If the next has moved, it must move too
156
+ prevMovedBlock === nextBlock
157
+ ) {
158
+ prevMovedBlock = block
155
159
block . insert ( parent , nextBlock ? nextBlock . el : anchor )
156
160
}
157
161
}
158
162
}
163
+ nextBlocks . unshift ( nextBlock = block )
159
164
}
160
165
blocks = nextBlocks
161
166
}
0 commit comments