@tanstack/virtual-core 3.19.15 → 3.19.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +36 -2
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +2 -0
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +36 -2
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +63 -6
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -502,6 +502,7 @@ export class Virtualizer<
|
|
|
502
502
|
items: Array<Key | VirtualItem>
|
|
503
503
|
} | null = null
|
|
504
504
|
itemSizeCache = new Map<Key, number>()
|
|
505
|
+
private elementKeys = new WeakMap<TItemElement, Key>()
|
|
505
506
|
private itemSizeCacheVersion = 0
|
|
506
507
|
private builtMeasurementOptions: object | null = null
|
|
507
508
|
private builtItemSizeCacheVersion = -1
|
|
@@ -803,6 +804,16 @@ export class Virtualizer<
|
|
|
803
804
|
pendingObservations.delete(target)
|
|
804
805
|
get()?.unobserve(target)
|
|
805
806
|
},
|
|
807
|
+
isDelivering: () => delivering,
|
|
808
|
+
asDelivery: (fn: () => void) => {
|
|
809
|
+
const prev = delivering
|
|
810
|
+
delivering = true
|
|
811
|
+
try {
|
|
812
|
+
fn()
|
|
813
|
+
} finally {
|
|
814
|
+
delivering = prev
|
|
815
|
+
}
|
|
816
|
+
},
|
|
806
817
|
}
|
|
807
818
|
})()
|
|
808
819
|
range: { startIndex: number; endIndex: number } | null = null
|
|
@@ -1155,12 +1166,19 @@ export class Virtualizer<
|
|
|
1155
1166
|
this.observer.observe(cached)
|
|
1156
1167
|
})
|
|
1157
1168
|
|
|
1169
|
+
let observing = false
|
|
1158
1170
|
this.unsubs.push(
|
|
1159
1171
|
this.options.observeElementRect(this, (rect) => {
|
|
1160
1172
|
this.scrollRect = rect
|
|
1161
1173
|
this.maybeNotify()
|
|
1174
|
+
// A render inside a delivery can detach rows without a ref-null
|
|
1175
|
+
// callback (Solid never makes one). Drop them as the row observer
|
|
1176
|
+
// does. The first call is the initial read, not a delivery, and
|
|
1177
|
+
// rows that are not attached yet stay observed through it.
|
|
1178
|
+
if (observing) this.measureElement(null)
|
|
1162
1179
|
}),
|
|
1163
1180
|
)
|
|
1181
|
+
observing = true
|
|
1164
1182
|
|
|
1165
1183
|
this.unsubs.push(
|
|
1166
1184
|
this.options.observeElementOffset(this, (offset, isScrolling) => {
|
|
@@ -2165,6 +2183,23 @@ export class Virtualizer<
|
|
|
2165
2183
|
return
|
|
2166
2184
|
}
|
|
2167
2185
|
|
|
2186
|
+
// Solid calls the ref before its render effects set the index attribute,
|
|
2187
|
+
// and inserts the row after them. Read the index once they have run.
|
|
2188
|
+
// A row mounted during a resize delivery is still registered next frame.
|
|
2189
|
+
if (!node.isConnected && !node.getAttribute(this.options.indexAttribute)) {
|
|
2190
|
+
const delivering = this.observer.isDelivering()
|
|
2191
|
+
queueMicrotask(() => {
|
|
2192
|
+
if (!node.getAttribute(this.options.indexAttribute)) {
|
|
2193
|
+
this.indexFromElement(node)
|
|
2194
|
+
} else if (delivering) {
|
|
2195
|
+
this.observer.asDelivery(() => this.measureElement(node))
|
|
2196
|
+
} else {
|
|
2197
|
+
this.measureElement(node)
|
|
2198
|
+
}
|
|
2199
|
+
})
|
|
2200
|
+
return
|
|
2201
|
+
}
|
|
2202
|
+
|
|
2168
2203
|
const index = this.indexFromElement(node)
|
|
2169
2204
|
if (!this.isIndexInRange(index)) return
|
|
2170
2205
|
|
|
@@ -2175,8 +2210,20 @@ export class Virtualizer<
|
|
|
2175
2210
|
if (prevNode) {
|
|
2176
2211
|
this.observer.unobserve(prevNode)
|
|
2177
2212
|
}
|
|
2213
|
+
// The node may still be registered under another key: rows were
|
|
2214
|
+
// reordered and the framework kept the DOM node but gave it a new index
|
|
2215
|
+
// (or getItemKey now returns a different key for its index). Drop the
|
|
2216
|
+
// old entry so a later node for that key does not unobserve this one,
|
|
2217
|
+
// and unobserve it first so the observe() below delivers a fresh size
|
|
2218
|
+
// for the new key instead of being a no-op.
|
|
2219
|
+
const prevKey = this.elementKeys.get(node)
|
|
2220
|
+
if (prevKey !== undefined && this.elementsCache.get(prevKey) === node) {
|
|
2221
|
+
this.elementsCache.delete(prevKey)
|
|
2222
|
+
this.observer.unobserve(node)
|
|
2223
|
+
}
|
|
2178
2224
|
this.observer.observe(node)
|
|
2179
2225
|
this.elementsCache.set(key, node)
|
|
2226
|
+
this.elementKeys.set(node, key)
|
|
2180
2227
|
}
|
|
2181
2228
|
|
|
2182
2229
|
// Sync-measure when idle (initial render) or during programmatic scrolling
|
|
@@ -2195,6 +2242,13 @@ export class Virtualizer<
|
|
|
2195
2242
|
}
|
|
2196
2243
|
}
|
|
2197
2244
|
|
|
2245
|
+
// Every resizeItem() since the last getMeasurements() bumped the version
|
|
2246
|
+
// once and queued one pending resize. Any other bump (measure(), edge keys
|
|
2247
|
+
// change) means the built layout no longer matches itemSizeCache.
|
|
2248
|
+
private onlyPendingResizesSinceBuild = (): boolean =>
|
|
2249
|
+
this.itemSizeCacheVersion ===
|
|
2250
|
+
this.builtItemSizeCacheVersion + this.pendingResizes.length
|
|
2251
|
+
|
|
2198
2252
|
private canShiftFromPendingResizes = (): boolean =>
|
|
2199
2253
|
this.options.lanes === 1 &&
|
|
2200
2254
|
this._singleLaneMeasurements !== null &&
|
|
@@ -2202,16 +2256,19 @@ export class Virtualizer<
|
|
|
2202
2256
|
!this.lanesChangedFlag &&
|
|
2203
2257
|
!this.lanesSettling &&
|
|
2204
2258
|
this.getMeasurementOptions() === this.builtMeasurementOptions &&
|
|
2205
|
-
this.
|
|
2206
|
-
this.builtItemSizeCacheVersion + this.pendingResizes.length
|
|
2259
|
+
this.onlyPendingResizesSinceBuild()
|
|
2207
2260
|
|
|
2208
2261
|
resizeItem = (index: number, size: number) => {
|
|
2209
2262
|
if (!this.isIndexInRange(index)) return
|
|
2210
2263
|
|
|
2211
|
-
// Options can change before the next render
|
|
2212
|
-
//
|
|
2213
|
-
//
|
|
2214
|
-
|
|
2264
|
+
// Options or the size cache (measure()) can change before the next render
|
|
2265
|
+
// rebuilds the measurements. Rebuild so this resize is compared with the
|
|
2266
|
+
// current position and estimate, also when deciding whether it needs a
|
|
2267
|
+
// scroll correction.
|
|
2268
|
+
if (
|
|
2269
|
+
this.getMeasurementOptions() !== this.builtMeasurementOptions ||
|
|
2270
|
+
!this.onlyPendingResizesSinceBuild()
|
|
2271
|
+
) {
|
|
2215
2272
|
this.getMeasurements()
|
|
2216
2273
|
}
|
|
2217
2274
|
|