Repository navigation
Stop scrollTo polling forever when the id never appears (#403) - #404
melihozcan1 wants to merge 3 commits into
Conversation
👷 Deploy request for react-arborist pending review.Visit the deploys page to approve it
|
TrevorBurnham
left a comment
There was a problem hiding this comment.
Thanks, this fixes #403: the missing return after reject() is what kept the poll running forever, and I don't see any regressions. A couple of small suggestions are inline.
Some things in code this PR doesn't touch, which could be follow-ups rather than blockers here:
id in this.idToIndex(tree-api.ts, inscrollTo):inalso matches inheritedObject.prototypekeys, soscrollTo("toString")orscrollTo("constructor")resolves immediately and passes a function toscrollToItem.Object.hasOwn(this.idToIndex, id)would avoid that.- Overlapping
scrollTocalls aren't cancelled. An older call that's still polling can resolve after a newer one and jump the view back to a stale target. - Polling outlives unmount: for up to ~1 s, keeping the
TreeApireachable until it ends. - Silent give-up:
scrollToswallows the rejection, so a caller that awaits it can't tell "scrolled" from "gave up." - Longer term:
TreeApialready knows whenidToIndexis rebuilt (constructor /update()), soscrollTocould save a pending target and handle it there instead of polling on a timer. That would remove the ~1 s limit (data that takes longer to load is currently never scrolled to) and this whole class of timer leak.
| function check() { | ||
| tries += 1; | ||
| if (tries === 100) reject(); | ||
| if (tries === 100) { |
There was a problem hiding this comment.
Small off-by-one that's still here: on the 100th try this rejects without calling fn(), so the condition is only checked 99 times. If the row shows up in that last ~10 ms window, the scroll is silently skipped.
Checking first would fix that and also shorten the block:
function check() {
if (fn()) return resolve();
if (++tries >= 100) return reject();
setTimeout(check, 10);
}Naming the 100 / 10 ms constants (and exporting them) would also let the test use them instead of a hard-coded 2000.
There was a problem hiding this comment.
Thanks, good catch. Switched to your version: fn is checked first, so the 100th try counts, and check returns as soon as it resolves or rejects. The limits are now WAIT_FOR_TRIES and WAIT_FOR_INTERVAL_MS, exported from utils.ts (they stay internal; the package index only re-exports getTreeLinePrefix from there).
The new utils.test.ts pins this down: waitFor calls fn exactly WAIT_FOR_TRIES times before rejecting, and a row that appears on the last try still resolves. Both tests fail on the previous commit.
| test("leaves no timer running after it gives up", async () => { | ||
| const api = setupApi({ data: rowData }); | ||
| const done = api.scrollTo("missing"); | ||
| jest.advanceTimersByTime(2000); |
There was a problem hiding this comment.
Two suggestions for this test:
advanceTimersByTime(2000)depends on waitFor's hidden limits (100 × 10 ms). If those change, the test hangs atawait doneinstead of failing clearly.jest.runAllTimers()would test the leak directly: onmainit throws on the infinite timer loop.getTimerCount() === 0would also pass if a regression resolved immediately for a missing id. MockingscrollToItemon the list and asserting it was never called would pin down the give-up behavior.
There was a problem hiding this comment.
Done, the test now uses jest.runAllTimers() (on main it fails with Jest's infinite-loop abort) and asserts scrollToItem is never called.
One note on the second point: scrollTo's .then already returns early when idToIndex[id] is undefined, so the scrollToItem spy alone wouldn't catch a waitFor that resolved immediately for a missing id. The new waitFor tests cover that directly: it has to check fn WAIT_FOR_TRIES times and then reject.
|
Thanks for the review! I've applied both inline suggestions (details in the threads) and kept this PR to the |
8497888 to
7e8b986
Compare
|
Opened #406 for the Separately, the e2e test |
Summary
Fixes #403.
waitFor, whichtree.scrollTo()uses to wait for a row to appear, rejects after 100 tries but then falls through tosetTimeout(check, 10), so it keeps polling every 10 ms for as long as the page is open. EveryscrollTofor an id that never appears in that tree leaves one of these running, and they add up (details and measurements in #403).waitFornow checksfnfirst and returns as soon as it resolves or gives up, so it stops after at most 100 checks, and the 100th try checksfntoo (before, it rejected without checking). The limits are named constants,WAIT_FOR_TRIESandWAIT_FOR_INTERVAL_MS. When the id does appear,waitForresolves as before.Tests:
tree-api.test.ts:scrollToto an id that isn't in the tree runs all pending timers to completion (onmain, Jest aborts on the infinite timer loop) and never callsscrollToItem.utils.test.ts(new):waitForrejects after checkingfnexactlyWAIT_FOR_TRIEStimes, and resolves whenfnfirst returns true on the last try.The existing
scrollTotests cover the found path.Checklist
.changes/entry describing the change (see.changes/README.md), or applied theskip-changeloglabel if it has no user-facing effect.yarn lint,yarn fmt:check, andyarn workspace react-arborist testpass (warning-clean — seeAGENTS.md).