@@ -198,84 +198,92 @@ export const createFor = (
198198 pendingNews [ l1 ++ ] = [ i , item , key ]
199199 }
200200
201- pendingNews . length = l1
202- oldKeyToIndex . length = l2
201+ if ( ! l1 && ! l2 ) {
202+ for ( let i = prepareLength ; i < newLength - right ; i ++ ) {
203+ const item = getItem ( source , i )
204+ const key = getKey . apply ( null , item )
205+ mount ( source , i , item , key , defaultAnchor )
206+ }
207+ } else {
208+ pendingNews . length = l1
209+ oldKeyToIndex . length = l2
210+
211+ const oldKeyToIndexMap = new Map ( oldKeyToIndex )
212+ const pendingMounts : [
213+ index : number ,
214+ item : ReturnType < typeof getItem > ,
215+ key : any ,
216+ anchorIndex : number ,
217+ ] [ ] = [ ]
218+ const moveOrMount = (
219+ index : number ,
220+ item : ReturnType < typeof getItem > ,
221+ key : any ,
222+ anchorIndex : number ,
223+ ) => {
224+ const oldIndex = oldKeyToIndexMap . get ( key )
225+ if ( oldIndex !== undefined ) {
226+ const block = ( newBlocks [ index ] = oldBlocks [ oldIndex ] )
227+ update ( block , ...item )
228+ insert (
229+ block ,
230+ parent ! ,
231+ anchorIndex === - 1
232+ ? defaultAnchor
233+ : normalizeAnchor ( newBlocks [ anchorIndex ] . nodes ) ,
234+ )
235+ oldKeyToIndexMap . delete ( key )
236+ } else {
237+ pendingMounts . push ( [ index , item , key , anchorIndex ] )
238+ }
239+ }
203240
204- const oldKeyToIndexMap = new Map ( oldKeyToIndex )
205- const pendingMounts : [
206- index : number ,
207- item : ReturnType < typeof getItem > ,
208- key : any ,
209- anchorIndex : number ,
210- ] [ ] = [ ]
211- const moveOrMount = (
212- index : number ,
213- item : ReturnType < typeof getItem > ,
214- key : any ,
215- anchorIndex : number ,
216- ) => {
217- const oldIndex = oldKeyToIndexMap . get ( key )
218- if ( oldIndex !== undefined ) {
219- const block = ( newBlocks [ index ] = oldBlocks [ oldIndex ] )
220- update ( block , ...item )
221- insert (
222- block ,
223- parent ! ,
224- anchorIndex === - 1
225- ? defaultAnchor
226- : normalizeAnchor ( newBlocks [ anchorIndex ] . nodes ) ,
241+ for ( let i = pendingNews . length - 1 ; i >= 0 ; i -- ) {
242+ const [ index , item , key ] = pendingNews [ i ]
243+ moveOrMount (
244+ index ,
245+ item ,
246+ key ,
247+ index < prepareLength - 1 ? index + 1 : - 1 ,
227248 )
228- oldKeyToIndexMap . delete ( key )
229- } else {
230- pendingMounts . push ( [ index , item , key , anchorIndex ] )
231249 }
232- }
233250
234- for ( let i = pendingNews . length - 1 ; i >= 0 ; i -- ) {
235- const [ index , item , key ] = pendingNews [ i ]
236- moveOrMount (
237- index ,
238- item ,
239- key ,
240- index < prepareLength - 1 ? index + 1 : - 1 ,
241- )
242- }
243-
244- for ( let i = prepareLength ; i < newLength - right ; i ++ ) {
245- const item = getItem ( source , i )
246- const key = getKey . apply ( null , item )
247- moveOrMount ( i , item , key , - 1 )
248- }
251+ for ( let i = prepareLength ; i < newLength - right ; i ++ ) {
252+ const item = getItem ( source , i )
253+ const key = getKey . apply ( null , item )
254+ moveOrMount ( i , item , key , - 1 )
255+ }
249256
250- const shouldUseFastRemove = pendingMounts . length === newLength
257+ const shouldUseFastRemove = pendingMounts . length === newLength
251258
252- for ( const i of oldKeyToIndexMap . values ( ) ) {
253- unmount (
254- oldBlocks [ i ] ,
255- ! ( shouldUseFastRemove && canUseFastRemove ) ,
256- ! shouldUseFastRemove ,
257- )
258- }
259- if ( shouldUseFastRemove ) {
260- for ( const selector of selectors ) {
261- selector . cleanup ( )
259+ for ( const i of oldKeyToIndexMap . values ( ) ) {
260+ unmount (
261+ oldBlocks [ i ] ,
262+ ! ( shouldUseFastRemove && canUseFastRemove ) ,
263+ ! shouldUseFastRemove ,
264+ )
262265 }
263- if ( canUseFastRemove ) {
264- parent ! . textContent = ''
265- parent ! . appendChild ( parentAnchor )
266+ if ( shouldUseFastRemove ) {
267+ for ( const selector of selectors ) {
268+ selector . cleanup ( )
269+ }
270+ if ( canUseFastRemove ) {
271+ parent ! . textContent = ''
272+ parent ! . appendChild ( parentAnchor )
273+ }
266274 }
267- }
268275
269- for ( const [ index , item , key , anchorIndex ] of pendingMounts ) {
270- mount (
271- source ,
272- index ,
273- item ,
274- key ,
275- anchorIndex === - 1
276- ? defaultAnchor
277- : normalizeAnchor ( newBlocks [ anchorIndex ] . nodes ) ,
278- )
276+ for ( const [ index , item , key , anchorIndex ] of pendingMounts ) {
277+ mount (
278+ source ,
279+ index ,
280+ item ,
281+ key ,
282+ anchorIndex === - 1
283+ ? defaultAnchor
284+ : normalizeAnchor ( newBlocks [ anchorIndex ] . nodes ) ,
285+ )
286+ }
279287 }
280288 }
281289 }
0 commit comments