Skip to content

Commit d674cbf

Browse files
agorischekCopilot
andcommitted
Give a change from outside a fresh replanning budget
Only switches between measured plans count towards the burst that bounds oscillation, not settles. A region that isn't as wide as when the bar settled, or content that finished rendering (`pending`), starts a new burst, so repeated notices at one width and content key can still replan; resize notifications from the plan the bar settled on leave the regions as wide as they were and keep the bound. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 3aa4f71 commit d674cbf

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

‎packages/host/core/src/fit-plan.ts‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ export function useFitPlan<T>(
9999
needs: new Map<string, number>(),
100100
ceilings: new Map<string, number>(),
101101
stale: false,
102+
fresh: false,
103+
regionWidths: new Map<Element, number>(),
102104
settled: plans[0]!,
103105
locked: false,
104106
steps: 0,
@@ -114,7 +116,7 @@ export function useFitPlan<T>(
114116
useLayoutEffect(() => {
115117
const element = bar.current
116118
if (!element) return
117-
const {sign, find, lock: lockOf, ceiling, measure, hysteresis, shift, carry, need, pending} = latest.current
119+
const {sign, find, lock: lockOf, ceiling, measure, hysteresis, shift, carry, need, pending, regions} = latest.current
118120
const m = memo.current
119121
if (m.key !== key) {
120122
m.key = key
@@ -148,6 +150,7 @@ export function useFitPlan<T>(
148150
void later.then(() => {
149151
if (memo.current.key !== key || memo.current.plans !== plans) return
150152
if (shift) memo.current.stale = true
153+
memo.current.fresh = true
151154
setState(previous => ({...previous, tick: previous.tick + 1}))
152155
})
153156
if (ceiling) m.ceilings.set(sign(plan), ceiling(element, plan))
@@ -164,21 +167,24 @@ export function useFitPlan<T>(
164167
allowed: lock,
165168
})
166169
// Trying an unmeasured plan always adds information. Switching between measured plans could only repeat if
167-
// measurements disagreed from one render to the next, so a burst of those settles; it never settles on a plan
168-
// known not to fit while one that fits is known.
170+
// measurements disagreed from one render to the next, so a burst of those switches settles; it never settles on a
171+
// plan known not to fit while one that fits is known. A change from outside (a region resized since the bar settled,
172+
// content that finished rendering) starts a new burst.
169173
const fits = (candidate: T) => (needOf(candidate) ?? Infinity) <= width + 0.5
170174
const burst = `${key}:${Math.round(width)}`
171-
if (m.burst !== burst) {
175+
if (m.burst !== burst || m.fresh) {
172176
m.burst = burst
173177
m.steps = 0
178+
m.fresh = false
174179
}
175180
const known = needOf(plans[target]!) !== undefined
176-
const switches = known ? ++m.steps : m.steps
181+
const switches = known && target !== index ? ++m.steps : m.steps
177182
if (target !== index && (!known || switches <= 4 || (!fits(plan) && switches <= 4 + plans.length))) {
178183
delete element.dataset.fitSettled
179184
setState(previous => ({...previous, plan: plans[target]!}))
180185
} else {
181186
m.settled = plan
187+
m.regionWidths = new Map([...element.querySelectorAll(regions)].map(region => [region, region.getBoundingClientRect().width]))
182188
element.dataset.fitSettled = String(Math.round(width))
183189
}
184190
}, [bar, plans, plan, index, key, state.width, state.tick])
@@ -192,8 +198,13 @@ export function useFitPlan<T>(
192198
// so it waits for the next frame, and renders synchronously there, before that frame's layout and paint.
193199
let frame = 0
194200
const resize = new ResizeObserver(entries => {
195-
// A region that resized changed what the shown plan needs.
196-
if (latest.current.shift && entries.some(entry => entry.target !== element)) memo.current.stale = true
201+
// A region that resized changed what the shown plan needs; one that isn't as wide as when the bar settled changed
202+
// from outside, not as part of the plan it settled on.
203+
const m = memo.current
204+
const regions = entries.filter(entry => entry.target !== element)
205+
if (latest.current.shift && regions.length) m.stale = true
206+
if (regions.some(({target}) => Math.abs(target.getBoundingClientRect().width - (m.regionWidths.get(target) ?? NaN)) >= 0.5))
207+
m.fresh = true
197208
if (frame) return
198209
frame = requestAnimationFrame(() => {
199210
frame = 0

0 commit comments

Comments
 (0)