Skip to content

Commit

Permalink
fix(cdk:scroll): pool item not recycled index isn't right (#1926)
Browse files Browse the repository at this point in the history
  • Loading branch information
sallerli1 authored May 20, 2024
1 parent 19078ee commit c177f36
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions packages/cdk/scroll/src/virtual/composables/useRenderPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,41 +91,42 @@ export function useRenderPool(

updated = !!inactiveKeys.size

const poolKeyMap = new Map(items.map(item => [item.itemKey, item.key]))
const poolMap = new Map(items.map(item => [item.itemKey, item]))
const poolKeySet = new Set(items.map(item => item.key))
const newlyAppendedItems: { item: unknown; index: number; itemKey: VKey }[] = []

const updatePoolItem = (poolItem: PoolItem, item: unknown, index: number, itemKey: VKey) => {
poolItem.item = item
poolItem.index = index
poolItem.itemKey = itemKey

inactiveKeys.delete(poolItem.key)
updated = true

// if an inactive item is resued, remove it from deleted item list
if (!poolKeySet.has(poolItem.key)) {
items.push(poolItem)
}
}

for (let index = start; index <= end; index++) {
const item = data[index]
if (!item) {
continue
}

const itemKey = getKey.value(item)
const existedPoolItemKey = poolKeyMap.get(itemKey)
const existedPoolItem = poolMap.get(itemKey)

// item still active, continue
if (!isNil(existedPoolItemKey) && !inactiveKeys.has(existedPoolItemKey)) {
if (!isNil(existedPoolItem) && !inactiveKeys.has(existedPoolItem.key)) {
updatePoolItem(existedPoolItem, item, index, itemKey)
continue
}

newlyAppendedItems.push({ item, index, itemKey })
}

const updatePoolItem = (poolItem: PoolItem, item: unknown, index: number, itemKey: VKey) => {
poolItem.item = item
poolItem.index = index
poolItem.itemKey = itemKey

inactiveKeys.delete(poolItem.key)
updated = true

// if an inactive item is resued, remove it from deleted item list
if (!poolKeySet.has(poolItem.key)) {
items.push(poolItem)
}
}

let i = 0
let currentItem = newlyAppendedItems[i]
while (currentItem) {
Expand Down

0 comments on commit c177f36

Please sign in to comment.