fix(virtual-core): stop scroll corrections from ping-ponging on a stale layout box #1374
Openitisrmk(GitHub profile, opens in a new tab)wants to merge 1 commit into
main fromrefs/pull/1374/headopened - @@ -2044,3 +2079,21 @@20442079 }20452080 }204620812082+ // A macrotask after the ResizeObserver broadcast, then a frame for the2083+ // live read. Microtasks run between broadcast callbacks, so they must not2084+ // be what releases the guard; setTimeout does not.2085+ private _scheduleEchoSettle = () => {2086+ if (this._echoSettleScheduled) return2087+ const win = this.targetWindow2088+ if (win?.setTimeout == null) return2089+ this._echoSettleScheduled = true2090+ const token = this._echoToken2091+ win.setTimeout(() => {2092+ if (token !== this._echoToken) return2093+ this._echoSettleScheduled = false2094+ const keys = [...this._echoConfirmKeys]2095+ this._echoConfirmKeys.clear()2096+ // Nothing echoed: release the guard so a later, real return to the2097+ // previous size still applies. An echo waits for a settled box.2098+ if (keys.length === 0) {2099+ this._echoFrom.clear()@@ -2046,0 +2105,21 @@2105+ for (const key of keys) this._confirmEchoKey(key)2106+ }2107+ if (win.requestAnimationFrame == null) {2108+ confirm()2109+ return2110+ }2111+ win.requestAnimationFrame(() => {2112+ if (token !== this._echoToken) return2113+ win.requestAnimationFrame(confirm)2114+ })2115+ }, 0)2116+ }2117+2118+ private _confirmEchoKey = (key: Key) => {2119+ const node = this.elementsCache.get(key)2120+ if (!node?.isConnected) return2121+ const index = this.indexFromElement(node)2122+ if (!this.isIndexInRange(index)) return2123+ const live = this._readLiveItemSize(node)2124+ if (live == null || !Number.isFinite(live)) return2125+ this.resizeItem(index, live)@@ -2046,0 +2116,21 @@2116+ }2117+2118+ private _confirmEchoKey = (key: Key) => {2119+ const node = this.elementsCache.get(key)2120+ if (!node?.isConnected) return2121+ const index = this.indexFromElement(node)2122+ if (!this.isIndexInRange(index)) return2123+ const live = this._readLiveItemSize(node)2124+ if (live == null || !Number.isFinite(live)) return2125+ this.resizeItem(index, live)2126+ }2127+2128+ // A fresh border box. The sync measure path returns the cache, which is2129+ // the size the echo just refused to commit, so the confirm cannot use it.2130+ private _readLiveItemSize = (node: TItemElement): number | null => {2131+ const el = node as unknown as HTMLElement2132+ const horizontal = this.options.horizontal2133+ const block = el?.[horizontal ? 'offsetWidth' : 'offsetHeight']2134+ const inline = el?.[horizontal ? 'offsetHeight' : 'offsetWidth']2135+ if (typeof block !== 'number' || !(block > 0)) {2136+ return this.options.measureElement(node, undefined, this)@@ -2046,0 +2127,21 @@2127+2128+ // A fresh border box. The sync measure path returns the cache, which is2129+ // the size the echo just refused to commit, so the confirm cannot use it.2130+ private _readLiveItemSize = (node: TItemElement): number | null => {2131+ const el = node as unknown as HTMLElement2132+ const horizontal = this.options.horizontal2133+ const block = el?.[horizontal ? 'offsetWidth' : 'offsetHeight']2134+ const inline = el?.[horizontal ? 'offsetHeight' : 'offsetWidth']2135+ if (typeof block !== 'number' || !(block > 0)) {2136+ return this.options.measureElement(node, undefined, this)2137+ }2138+ const entry = {2139+ target: node,2140+ borderBoxSize: [2141+ {2142+ blockSize: horizontal ? inline || 0 : block,2143+ inlineSize: horizontal ? block : inline || 0,2144+ },2145+ ],2146+ } as unknown as ResizeObserverEntry2147+ return this.options.measureElement(node, entry, this)@@ -2078,3 +2190,14 @@20782190 const delta = size - itemSize2079219120802192 if (delta !== 0) {2193+ // Immediate reversal of the size this correction just left, seen from2194+ // the notify it flushed or from the next observation in the same2195+ // broadcast. Keep the size we already committed. The confirm below2196+ // reads the box again once layout has had a frame (#1252).2197+ if (2198+ this._echoFrom.get(key) === size &&2199+ (this._observingResize || this._correctionDepth > 0)2200+ ) {2201+ this._echoConfirmKeys.add(key)2202+ this._scheduleEchoSettle()2203+ return
itisrmk(GitHub profile, opens in a new tab) linked this to fix #1252
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: 8ae09d1134ceaab095ee1cdba3caa732d9ad53ac
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 9 packages
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR