Skip to content

Surface scrollTo scroll errors and let a newer scrollTo replace an older one - #407

Merged
TrevorBurnham merged 5 commits into
jameskerr:mainfrom
melihozcan1:followup-scroll-to-bugfix
Nov 29, 2026
Merged

TrevorBurnham merged 5 commits into
jameskerr:mainfrom
melihozcan1:followup-scroll-to-bugfix

Conversation

@melihozcan1

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #404 (issue #403). It covers the three points from this review comment.

  1. scrollTo no longer hides errors from scrolling. The give-up handler is now the second argument to .then rather than a trailing .catch. Only waitFor giving up is ignored. If scrollToItem or scrollToNodeHorizontally throws, the promise scrollTo returns now rejects with that error. Callers that don't await it get an unhandled rejection in the console, where before nothing was logged.
  2. A newer scrollTo replaces an older one that is still waiting. Each call takes a per-tree token. A call that has been superseded never scrolls, and its waitFor check also counts "superseded" as done, so its poller stops on the next tick instead of running for up to ~1 s. In the example from the review, scrollTo("x") followed by scrollTo("y") now ends on y even if x's row appears later.
  3. Tests: expect(jest.getTimerCount()).toBe(0) after await done in the scrollTo() keeps polling every 10 ms forever when the id never appears #403 scrollTo test and in both waitFor tests.

Items 1 and 2 each have their own fix changeset. Item 3 is test-only.

New tests in tree-api.test.ts, both of which fail on main:

  • scrollTo rejects with the error when scrollToItem throws. On main it resolves.
  • An older scrollTo("late") that is still waiting, followed by scrollTo("b"): when late is added to the data, only b is scrolled to and no timers remain. On main, scrollToItem is called a second time with late's index.

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 28, 2026 19:02
@netlify

netlify Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

👷 Deploy request for react-arborist pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit e69895d

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
Contributor Author

The red test check is the flaky budget-spec.cy.js e2e test (it fails about 1 in 5 runs on unrelated branches), not this change. #408 fixes it. The unit tests pass.

this.scrollToNodeHorizontally(this.get(id));
},
// Only waitFor giving up is ignored; errors thrown while scrolling reject.
() => {},

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.

Now that errors thrown inside the .then reject instead of being swallowed, the internal callers turn them into unhandled rejections. scrollTo is called without a catch from lines 352, 417, 460, 483, 503 and 736 (focus, select, selectMulti, edit, the focused-node re-scroll and others), so a throwing scrollToItem during a click now surfaces as "Uncaught (in promise)" where it used to be silent.

Maybe give those call sites a .catch(() => {}), or route them through a private helper that does, so only the promise returned to a user's own tree.scrollTo() call can reject?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in c35f357. The six internal call sites now go through a private scrollQuietly() that adds .catch(() => {}), so only the promise from a user's own tree.scrollTo() can reject. The changeset says that now too.

});
const list = { current: { scrollToItem } as any };
const api = new TreeApi(store, { data: rowData }, list, { current: null });
await expect(api.scrollTo("b")).rejects.toThrow("scroll failed");

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.

This covers the public promise, but nothing exercises the internal callers. A test that makes scrollToItem throw, calls api.select("b") or api.focus("b"), and checks that no unhandled rejection escapes would have caught the issue on tree-api.ts.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added in c35f357: focus, select, selectMulti, selectContiguous and edit each run against a scrollToItem that throws. All five fail without the fix. A process.on("unhandledRejection") spy doesn't work here, because Jest gives each test file its own process (it saw 0 calls), so the tests rely on Node rethrowing the unhandled rejection, which fails the running test.

const id = this.identify(identity);
const token = ++this.scrollToken;
const superseded = () => token !== this.scrollToken;
this.openParents(id);

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 small questions, not blockers:

  • openParents(id) runs before the cancellation check, so a call that's later replaced still leaves its target's ancestors expanded. That seems reasonable (the call did happen), just confirming it's intended.
  • The if (!identity) return; early return comes before ++this.scrollToken, so scrollTo(null) can't cancel a pending wait. Intended?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Both intended:

  • openParents runs synchronously when the call is made, so by the time a newer call replaces it, the ancestors are already open; only the scroll is cancelled. Collapsing them again would mean tracking which folders that call opened, and could close one the user opened in the meantime.
  • scrollTo(null) has no target, so it leaves a pending call alone. openSiblings calls scrollTo(this.focusedNode), which is null when nothing is focused. If null cancelled, that call would silently drop an unrelated pending scroll.

Copilot AI balanced review requested due to automatic review settings November 28, 2026 21:29

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.

Copilot AI balanced review requested due to automatic review settings November 28, 2026 22:18

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
Contributor Author

Correction to my note above: #408 fixed a different race. The one failing here is fixed by #409 (CI green). Once #409 is merged, I'll merge main in here.

scrollTo(identity: Identity | T, align: Align = "smart") {
if (!identity) return;
const id = this.identify(identity);
const token = ++this.scrollToken;

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 for adding scrollQuietly, that takes care of the unhandled rejections. One follow-on: since scrollQuietly goes through scrollTo, internal scrolls also bump scrollToken, so any focus or selection change cancels an explicit tree.scrollTo(id) that's still waiting for its row.

A user clicking another row winning seems fine. The case that worries me is a controlled selection prop. The effect in components/provider.tsx calls api.select(api.props.selection, { focus: false }), which scrolls. So an app that adds a node, calls tree.scrollTo(newId) and updates selection in the same handler gets its scrollTo silently cancelled when that effect runs after the next render. Before this PR both scrolls would have run.

Could internal scrolls skip the token bump (or use their own), so only public scrollTo calls replace each other?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 1fdc44d. scrollTo and scrollQuietly now share a private scrollWhenReady(id, align, superseded). Only scrollTo bumps the token, and internal scrolls are never superseded, so they don't cancel each other or a pending scrollTo, same as before this PR.

Added a test for your case: scrollTo("late") is pending, select("b", { focus: false }) runs (what the controlled selection effect does), then late appears and still gets scrolled to. It fails on c35f357.

@@ -0,0 +1,4 @@
---
type: fix

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.

scrollTo used to never reject, and now it can. App code that calls tree.scrollTo(id) without awaiting it or adding a .catch would start seeing unhandled rejections after a patch upgrade. Worth mentioning the rejection in the README's scrollTo entry (under Scrolling), and maybe filing this as feature rather than fix?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 1fdc44d. The README's scrollTo entry now says it returns a promise that resolves once the node is in view, when a newer call replaces it, or after about a second if the id never appears, and that it rejects if scrolling throws. The changeset is now feature; yarn release --preview infers 4.1.0.

Copilot AI balanced review requested due to automatic review settings November 29, 2026 08:36

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.

Comment thread README.md
_tree_.**scrollTo**(_id_, _[align]_)

Scroll to the node with _id_. If this node is not visible, this method will open all its parents. The align argument can be _"auto" | "smart" | "center" | "end" | "start"_.
Scroll to the node with _id_. If this node is not visible, this method will open all its parents. The align argument can be _"auto" | "smart" | "center" | "end" | "start"_. Returns a promise. It resolves once the node is scrolled into view, when a newer _scrollTo_ call replaces this one, or after about a second if no node with _id_ appears. It rejects if scrolling itself throws.

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 for the updates, both look good. One small mismatch: this now says scrollTo returns a promise, but scrollTo(null) (or any falsy id) hits the early if (!identity) return; and returns undefined. JS callers who write tree.scrollTo(selectedId).then(...) with a null id would get a TypeError. Returning Promise.resolve() from that early exit would make the docs hold in every case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, fixed in e69895d. The early exit now returns Promise.resolve(), so scrollTo returns a promise for any id. It still leaves a pending call alone. Added a test for null and ""; both fail without the change. The changeset mentions it too.

Copilot AI balanced review requested due to automatic review settings November 29, 2026 17:05

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
Contributor Author

The red test check on e69895d is the budget e2e flake again ('📂Entertainment$0', run), not this change. The unit tests and the other e2e specs pass. #409 fixes it, and it now also has the shared cy.dragAndDrop you suggested. Once #409 is merged I'll merge main in here.

@TrevorBurnham
TrevorBurnham merged commit 1178400 into jameskerr:main Nov 29, 2026
1 of 2 checks passed
@TrevorBurnham

Copy link
Copy Markdown
Collaborator

Thanks! Released as v4.1.0.

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.

3 participants