Skip to content

Stop scrollTo polling forever when the id never appears (#403) - #404

Open
melihozcan1 wants to merge 3 commits into
jameskerr:mainfrom
melihozcan1:fix/waitfor-stop-polling
Open

melihozcan1 wants to merge 3 commits into
jameskerr:mainfrom
melihozcan1:fix/waitfor-stop-polling

Conversation

@melihozcan1

@melihozcan1 melihozcan1 commented Nov 3, 2026 •

Copy link
Copy Markdown

Summary

Fixes #403.

waitFor, which tree.scrollTo() uses to wait for a row to appear, rejects after 100 tries but then falls through to setTimeout(check, 10), so it keeps polling every 10 ms for as long as the page is open. Every scrollTo for an id that never appears in that tree leaves one of these running, and they add up (details and measurements in #403).

waitFor now checks fn first and returns as soon as it resolves or gives up, so it stops after at most 100 checks, and the 100th try checks fn too (before, it rejected without checking). The limits are named constants, WAIT_FOR_TRIES and WAIT_FOR_INTERVAL_MS. When the id does appear, waitFor resolves as before.

Tests:

  • tree-api.test.ts: scrollTo to an id that isn't in the tree runs all pending timers to completion (on main, Jest aborts on the infinite timer loop) and never calls scrollToItem.
  • utils.test.ts (new): waitFor rejects after checking fn exactly WAIT_FOR_TRIES times, and resolves when fn first returns true on the last try.

The existing scrollTo tests cover the found path.

Checklist

  • Added a .changes/ entry describing the change (see .changes/README.md), or applied the skip-changelog label if it has no user-facing effect.
  • yarn lint, yarn fmt:check, and yarn workspace react-arborist test pass (warning-clean — see AGENTS.md).

Copilot AI balanced review requested due to automatic review settings November 3, 2026 13:42
@netlify

netlify Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

👷 Deploy request for react-arborist pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 7e8b986

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@TrevorBurnham TrevorBurnham left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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, in scrollTo): in also matches inherited Object.prototype keys, so scrollTo("toString") or scrollTo("constructor") resolves immediately and passes a function to scrollToItem. Object.hasOwn(this.idToIndex, id) would avoid that.
  • Overlapping scrollTo calls 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 TreeApi reachable until it ends.
  • Silent give-up: scrollTo swallows the rejection, so a caller that awaits it can't tell "scrolled" from "gave up."
  • Longer term: TreeApi already knows when idToIndex is rebuilt (constructor / update()), so scrollTo could 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.

Comment thread modules/react-arborist/src/utils.ts Outdated
function check() {
tries += 1;
if (tries === 100) reject();
if (tries === 100) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two suggestions for this test:

  • advanceTimersByTime(2000) depends on waitFor's hidden limits (100 × 10 ms). If those change, the test hangs at await done instead of failing clearly. jest.runAllTimers() would test the leak directly: on main it throws on the infinite timer loop.
  • getTimerCount() === 0 would also pass if a regression resolved immediately for a missing id. Mocking scrollToItem on the list and asserting it was never called would pin down the give-up behavior.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot AI balanced review requested due to automatic review settings November 16, 2026 07:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@melihozcan1

Copy link
Copy Markdown
Author

Thanks for the review! I've applied both inline suggestions (details in the threads) and kept this PR to the waitFor fix. I'll take the other points as follow-ups.

Copilot AI balanced review requested due to automatic review settings November 16, 2026 08:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@melihozcan1

Copy link
Copy Markdown
Author

Opened #406 for the id in this.idToIndex point from the review: ids like toString or constructor are no longer found in idToIndex, so scrollTo and indexOf don't treat them as rows.

Separately, the e2e test budget-spec.cy.js ("updates folder totals when an expense moves between folders") fails intermittently in CI. It failed once on this PR and passed on the next run, and it failed the same way on #399 and #400: 5 of the 18 runs that reached Cypress since #401 landed. I couldn't reproduce it locally, so it likely needs a follow-up PR once the cause is clear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

scrollTo() keeps polling every 10 ms forever when the id never appears

3 participants