Skip to content

Commit

Permalink
fix: getItemKey is called with invalid index (#581)
Browse files Browse the repository at this point in the history
  • Loading branch information
piecyk authored Sep 10, 2023
1 parent 4425868 commit 3270b6c
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions packages/virtual-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,16 +619,23 @@ export class Virtualizer<
node: TItemElement,
entry: ResizeObserverEntry | undefined,
) => {
const index = this.indexFromElement(node)

const elementKey = this.options.getItemKey(index)
const item = this.measurementsCache[this.indexFromElement(node)]
if (!item) {
this.measureElementCache.forEach((cached, key) => {
if (cached === node) {
this.observer.unobserve(node)
this.measureElementCache.delete(key)
}
})
return
}

const prevNode = this.measureElementCache.get(elementKey)
const prevNode = this.measureElementCache.get(item.key)

if (!node.isConnected) {
this.observer.unobserve(node)
if (node === prevNode) {
this.measureElementCache.delete(elementKey)
if (prevNode) {
this.observer.unobserve(prevNode)
this.measureElementCache.delete(item.key)
}
return
}
Expand All @@ -638,23 +645,16 @@ export class Virtualizer<
this.observer.unobserve(prevNode)
}
this.observer.observe(node)
this.measureElementCache.set(elementKey, node)
this.measureElementCache.set(item.key, node)
}

const measuredItemSize = this.options.measureElement(node, entry, this)

this.resizeItem(index, measuredItemSize)
this.resizeItem(item, measuredItemSize)
}

resizeItem = (index: number, size: number) => {
const item = this.measurementsCache[index]
if (!item) {
console.warn(`Missing virtual item for index = ${index}`)
return
}

resizeItem = (item: VirtualItem, size: number) => {
const itemSize = this.itemSizeCache.get(item.key) ?? item.size

const delta = size - itemSize

if (delta !== 0) {
Expand All @@ -669,8 +669,7 @@ export class Virtualizer<
})
}

this.pendingMeasuredCacheIndexes.push(index)

this.pendingMeasuredCacheIndexes.push(item.index)
this.itemSizeCache = new Map(this.itemSizeCache.set(item.key, size))

this.notify()
Expand Down

0 comments on commit 3270b6c

Please sign in to comment.