Repository navigation
Surface scrollTo scroll errors and let a newer scrollTo replace an older one - #407
Conversation
👷 Deploy request for react-arborist pending review.Visit the deploys page to approve it
|
|
The red |
| this.scrollToNodeHorizontally(this.get(id)); | ||
| }, | ||
| // Only waitFor giving up is ignored; errors thrown while scrolling reject. | ||
| () => {}, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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, soscrollTo(null)can't cancel a pending wait. Intended?
There was a problem hiding this comment.
Both intended:
openParentsruns 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.openSiblingscallsscrollTo(this.focusedNode), which is null when nothing is focused. If null cancelled, that call would silently drop an unrelated pending scroll.
| scrollTo(identity: Identity | T, align: Align = "smart") { | ||
| if (!identity) return; | ||
| const id = this.identify(identity); | ||
| const token = ++this.scrollToken; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 | |||
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
… that scrollTo can reject
| _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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
Thanks! Released as v4.1.0. |
Summary
Follow-up to #404 (issue #403). It covers the three points from this review comment.
scrollTono longer hides errors from scrolling. The give-up handler is now the second argument to.thenrather than a trailing.catch. OnlywaitForgiving up is ignored. IfscrollToItemorscrollToNodeHorizontallythrows, the promisescrollToreturns now rejects with that error. Callers that don't await it get an unhandled rejection in the console, where before nothing was logged.scrollToreplaces an older one that is still waiting. Each call takes a per-tree token. A call that has been superseded never scrolls, and itswaitForcheck 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 byscrollTo("y")now ends on y even if x's row appears later.expect(jest.getTimerCount()).toBe(0)afterawait donein the scrollTo() keeps polling every 10 ms forever when the id never appears #403scrollTotest and in bothwaitFortests.Items 1 and 2 each have their own
fixchangeset. Item 3 is test-only.New tests in
tree-api.test.ts, both of which fail onmain:scrollTorejects with the error whenscrollToItemthrows. Onmainit resolves.scrollTo("late")that is still waiting, followed byscrollTo("b"): whenlateis added to the data, onlybis scrolled to and no timers remain. Onmain,scrollToItemis called a second time withlate's index.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).