Skip to content

Commit

Permalink
fix: measureElement handle elements when keys change
Browse files Browse the repository at this point in the history
  • Loading branch information
piecyk committed Aug 19, 2024
1 parent aff750d commit 27e1ae8
Showing 1 changed file with 14 additions and 48 deletions.
62 changes: 14 additions & 48 deletions packages/virtual-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,38 +615,6 @@ export class Virtualizer<
const measurements = this.measurementsCache.slice(0, min)

for (let i = min; i < count; i++) {
let measureElement = this.measurementsCache[i]?.measureElement

if (!measureElement) {
measureElement = (node: TItemElement | null | undefined) => {
const key = getItemKey(i)
const prevNode = this.elementsCache.get(key)

if (!node) {
if (prevNode) {
this.observer.unobserve(prevNode)
this.elementsCache.delete(key)
}
return
}

if (prevNode !== node) {
if (prevNode) {
this.observer.unobserve(prevNode)
}
this.observer.observe(node)
this.elementsCache.set(key, node)
}

if (node.isConnected) {
this.resizeItem(
i,
this.options.measureElement(node, undefined, this),
)
}
}
}

const key = getItemKey(i)

const furthestMeasurement =
Expand Down Expand Up @@ -677,7 +645,7 @@ export class Virtualizer<
end,
key,
lane,
measureElement,
measureElement: this.measureElement,
}
}

Expand Down Expand Up @@ -750,30 +718,22 @@ export class Virtualizer<
node: TItemElement,
entry: ResizeObserverEntry | undefined,
) => {
const i = this.indexFromElement(node)
const item = this.getMeasurements()[i]
const index = this.indexFromElement(node)
const key = this.options.getItemKey(index)

if (!item || !node.isConnected) {
this.elementsCache.forEach((cached, key) => {
if (cached === node) {
this.observer.unobserve(node)
this.elementsCache.delete(key)
}
})
return
}

const prevNode = this.elementsCache.get(item.key)
const prevNode = this.elementsCache.get(key)

if (prevNode !== node) {
if (prevNode) {
this.observer.unobserve(prevNode)
}
this.observer.observe(node)
this.elementsCache.set(item.key, node)
this.elementsCache.set(key, node)
}

this.resizeItem(i, this.options.measureElement(node, entry, this))
if (node.isConnected) {
this.resizeItem(index, this.options.measureElement(node, entry, this))
}
}

resizeItem = (index: number, size: number) => {
Expand Down Expand Up @@ -809,6 +769,12 @@ export class Virtualizer<

measureElement = (node: TItemElement | null | undefined) => {
if (!node) {
this.elementsCache.forEach((cached, key) => {
if (!cached.isConnected) {
this.observer.unobserve(cached)
this.elementsCache.delete(key)
}
})
return
}

Expand Down

0 comments on commit 27e1ae8

Please sign in to comment.