From ef60ab19076a260aabd2b68b00c56217ba648b5e Mon Sep 17 00:00:00 2001 From: Melih Ozcan <59609421+melihozcan1@users.noreply.github.com> Date: Sat, 28 Nov 2026 22:02:03 +0300 Subject: [PATCH 1/4] Surface scrollTo scroll errors and let a newer scrollTo replace an older pending one --- .changes/scrollto-latest-call-wins.md | 4 +++ .changes/scrollto-surface-scroll-errors.md | 4 +++ .../src/interfaces/tree-api.test.ts | 32 +++++++++++++++++++ .../react-arborist/src/interfaces/tree-api.ts | 31 ++++++++++-------- modules/react-arborist/src/utils.test.ts | 2 ++ 5 files changed, 60 insertions(+), 13 deletions(-) create mode 100644 .changes/scrollto-latest-call-wins.md create mode 100644 .changes/scrollto-surface-scroll-errors.md diff --git a/.changes/scrollto-latest-call-wins.md b/.changes/scrollto-latest-call-wins.md new file mode 100644 index 00000000..9b211677 --- /dev/null +++ b/.changes/scrollto-latest-call-wins.md @@ -0,0 +1,4 @@ +--- +type: fix +--- +A newer `tree.scrollTo()` call now replaces an older one that is still waiting for its row, so the list no longer jumps back to the older target when that row appears later. diff --git a/.changes/scrollto-surface-scroll-errors.md b/.changes/scrollto-surface-scroll-errors.md new file mode 100644 index 00000000..b4404433 --- /dev/null +++ b/.changes/scrollto-surface-scroll-errors.md @@ -0,0 +1,4 @@ +--- +type: fix +--- +`tree.scrollTo()` no longer swallows errors thrown while scrolling to a node. It still ignores the case where the id never appears, but any other error now rejects the returned promise instead of failing silently. diff --git a/modules/react-arborist/src/interfaces/tree-api.test.ts b/modules/react-arborist/src/interfaces/tree-api.test.ts index 50df68b6..989e8e96 100644 --- a/modules/react-arborist/src/interfaces/tree-api.test.ts +++ b/modules/react-arborist/src/interfaces/tree-api.test.ts @@ -364,10 +364,42 @@ describe("scrollTo stops waiting for an id that never appears (#403)", () => { const done = api.scrollTo("missing"); jest.runAllTimers(); await done; + expect(jest.getTimerCount()).toBe(0); expect(list.current.scrollToItem).not.toHaveBeenCalled(); }); }); +describe("scrollTo surfaces errors thrown while scrolling", () => { + test("rejects with the error from scrollToItem", async () => { + const store = createStore(rootReducer); + const scrollToItem = jest.fn(() => { + throw new Error("scroll failed"); + }); + 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"); + }); +}); + +describe("a newer scrollTo replaces an older one that is still waiting", () => { + beforeEach(() => jest.useFakeTimers()); + afterEach(() => jest.useRealTimers()); + + test("the older call does not scroll when its row appears later", async () => { + const store = createStore(rootReducer); + const list = { current: { scrollToItem: jest.fn() } as any }; + const api = new TreeApi(store, { data: rowData }, list, { current: null }); + const older = api.scrollTo("late"); + await api.scrollTo("b"); + api.update({ data: [...rowData, { id: "late" }] }); + jest.runAllTimers(); + await older; + expect(jest.getTimerCount()).toBe(0); + expect(list.current.scrollToItem).toHaveBeenCalledTimes(1); + expect(list.current.scrollToItem).toHaveBeenCalledWith(1, "smart"); + }); +}); + describe("scrollToOffset / scrollOffset set and read the vertical position (#194)", () => { const data = [{ id: "a" }, { id: "b" }, { id: "c" }]; diff --git a/modules/react-arborist/src/interfaces/tree-api.ts b/modules/react-arborist/src/interfaces/tree-api.ts index b4805c11..3761048f 100644 --- a/modules/react-arborist/src/interfaces/tree-api.ts +++ b/modules/react-arborist/src/interfaces/tree-api.ts @@ -43,6 +43,7 @@ export class TreeApi { * the last hover of a frame wins, whichever row produced it. */ private pendingHover: { drop: DropResult | null; cursor: Cursor | null } | null = null; private hoverThrottle = createAnimationFrameThrottle(); + private scrollToken = 0; constructor( public store: Store, @@ -755,21 +756,25 @@ export class TreeApi { scrollTo(identity: Identity | T, align: Align = "smart") { if (!identity) return; const id = this.identify(identity); + const token = ++this.scrollToken; + const superseded = () => token !== this.scrollToken; this.openParents(id); return utils - .waitFor(() => id in this.idToIndex) - .then(() => { - const index = this.idToIndex[id]; - if (index === undefined) return; - this.list.current?.scrollToItem(index, align); - /* react-window only scrolls vertically. A deeply nested node is - indented by level * indent and can sit past the right edge when rows - overflow horizontally, so bring it into view ourselves (#220). */ - this.scrollToNodeHorizontally(this.get(id)); - }) - .catch(() => { - // Id: ${id} never appeared in the list. - }); + .waitFor(() => superseded() || id in this.idToIndex) + .then( + () => { + if (superseded()) return; + const index = this.idToIndex[id]; + if (index === undefined) return; + this.list.current?.scrollToItem(index, align); + /* react-window only scrolls vertically. A deeply nested node is + indented by level * indent and can sit past the right edge when rows + overflow horizontally, so bring it into view ourselves (#220). */ + this.scrollToNodeHorizontally(this.get(id)); + }, + // Only waitFor giving up is ignored; errors thrown while scrolling reject. + () => {}, + ); } /** diff --git a/modules/react-arborist/src/utils.test.ts b/modules/react-arborist/src/utils.test.ts index 2cb1a277..4de2a2f8 100644 --- a/modules/react-arborist/src/utils.test.ts +++ b/modules/react-arborist/src/utils.test.ts @@ -8,6 +8,7 @@ test("waitFor rejects after checking fn WAIT_FOR_TRIES times", async () => { const done = expect(waitFor(fn)).rejects.toBeUndefined(); jest.runAllTimers(); await done; + expect(jest.getTimerCount()).toBe(0); expect(fn).toHaveBeenCalledTimes(WAIT_FOR_TRIES); }); @@ -16,4 +17,5 @@ test("waitFor resolves when fn first returns true on the last try", async () => const done = expect(waitFor(() => ++calls === WAIT_FOR_TRIES)).resolves.toBeUndefined(); jest.runAllTimers(); await done; + expect(jest.getTimerCount()).toBe(0); }); From c35f3570f08b62780973505f9dd90618c00abe9f Mon Sep 17 00:00:00 2001 From: Melih Ozcan <59609421+melihozcan1@users.noreply.github.com> Date: Sun, 29 Nov 2026 01:18:54 +0300 Subject: [PATCH 2/4] Keep scroll errors from internal scrollTo callers out of unhandled rejections --- .changes/scrollto-surface-scroll-errors.md | 2 +- .../src/interfaces/tree-api.test.ts | 19 +++++++++++++++++-- .../react-arborist/src/interfaces/tree-api.ts | 18 ++++++++++++------ 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/.changes/scrollto-surface-scroll-errors.md b/.changes/scrollto-surface-scroll-errors.md index b4404433..93fef957 100644 --- a/.changes/scrollto-surface-scroll-errors.md +++ b/.changes/scrollto-surface-scroll-errors.md @@ -1,4 +1,4 @@ --- type: fix --- -`tree.scrollTo()` no longer swallows errors thrown while scrolling to a node. It still ignores the case where the id never appears, but any other error now rejects the returned promise instead of failing silently. +`tree.scrollTo()` no longer swallows errors thrown while scrolling to a node. It still ignores the case where the id never appears, but any other error now rejects the returned promise instead of failing silently. Scrolls that focusing, selecting or editing a node start on their own still never reject. diff --git a/modules/react-arborist/src/interfaces/tree-api.test.ts b/modules/react-arborist/src/interfaces/tree-api.test.ts index 989e8e96..79992ea0 100644 --- a/modules/react-arborist/src/interfaces/tree-api.test.ts +++ b/modules/react-arborist/src/interfaces/tree-api.test.ts @@ -370,15 +370,30 @@ describe("scrollTo stops waiting for an id that never appears (#403)", () => { }); describe("scrollTo surfaces errors thrown while scrolling", () => { - test("rejects with the error from scrollToItem", async () => { + function setup() { const store = createStore(rootReducer); const scrollToItem = jest.fn(() => { throw new Error("scroll failed"); }); const list = { current: { scrollToItem } as any }; - const api = new TreeApi(store, { data: rowData }, list, { current: null }); + return { api: new TreeApi(store, { data: rowData }, list, { current: null }), scrollToItem }; + } + + test("rejects with the error from scrollToItem", async () => { + const { api } = setup(); await expect(api.scrollTo("b")).rejects.toThrow("scroll failed"); }); + + test.each(["focus", "select", "selectMulti", "selectContiguous", "edit"] as const)( + "%s does not turn it into an unhandled rejection", + async (method) => { + const { api, scrollToItem } = setup(); + api[method]("b"); + // Node rethrows an unhandled rejection once the task ends, which fails this test. + await new Promise((resolve) => setTimeout(resolve, 0)); + expect(scrollToItem).toHaveBeenCalled(); + }, + ); }); describe("a newer scrollTo replaces an older one that is still waiting", () => { diff --git a/modules/react-arborist/src/interfaces/tree-api.ts b/modules/react-arborist/src/interfaces/tree-api.ts index 3761048f..170a008e 100644 --- a/modules/react-arborist/src/interfaces/tree-api.ts +++ b/modules/react-arborist/src/interfaces/tree-api.ts @@ -349,7 +349,7 @@ export class TreeApi { edit(node: string | IdObj | T): Promise { const id = this.identify(node); this.resolveEdit({ cancelled: true }); - this.scrollTo(id); + this.scrollQuietly(id); this.dispatch(edit(id)); this.redrawList(this.get(id)?.rowIndex ?? 0); return new Promise((resolve) => { @@ -414,7 +414,7 @@ export class TreeApi { this.select(node); } else { this.dispatch(focus(this.identify(node))); - if (opts.scroll !== false) this.scrollTo(node); + if (opts.scroll !== false) this.scrollQuietly(node); if (this.focusedNode) safeRun(this.props.onFocus, this.focusedNode); } } @@ -457,7 +457,7 @@ export class TreeApi { mostRecent: id, }); } - this.scrollTo(id, opts.align); + this.scrollQuietly(id, opts.align); if (this.focusedNode && changeFocus) { safeRun(this.props.onFocus, this.focusedNode); } @@ -480,7 +480,7 @@ export class TreeApi { this.dispatch(selection.anchor(node.id)); this.dispatch(selection.mostRecent(node.id)); } - this.scrollTo(node, opts.align); + this.scrollQuietly(node, opts.align); if (this.focusedNode && changeFocus) { safeRun(this.props.onFocus, this.focusedNode); } @@ -500,7 +500,7 @@ export class TreeApi { this.dispatch(selection.add(selectableNodes.map((n) => n.id))); this.dispatch(selection.mostRecent(id)); } - this.scrollTo(id); + this.scrollQuietly(id); if (this.focusedNode) safeRun(this.props.onFocus, this.focusedNode); safeRun(this.props.onSelect, this.selectedNodes); } @@ -733,7 +733,7 @@ export class TreeApi { } } this.redrawList(); - this.scrollTo(this.focusedNode); + this.scrollQuietly(this.focusedNode); } } @@ -777,6 +777,12 @@ export class TreeApi { ); } + /* For scrolls that are a side effect of focus, selection or editing: nobody + awaits them, so a rejection would surface as an unhandled one. */ + private scrollQuietly(identity: Identity | T, align?: Align) { + this.scrollTo(identity, align)?.catch(() => {}); + } + /** * Horizontally scroll the list so the node's indented content is in view. * A no-op when the list doesn't overflow horizontally (the common case), so From 1fdc44dd8279d93ab6740c6aac377b145e8f73fc Mon Sep 17 00:00:00 2001 From: Melih Ozcan <59609421+melihozcan1@users.noreply.github.com> Date: Sun, 29 Nov 2026 11:36:21 +0300 Subject: [PATCH 3/4] Keep internal scrolls from cancelling a pending scrollTo and document that scrollTo can reject --- .changes/scrollto-latest-call-wins.md | 2 +- .changes/scrollto-surface-scroll-errors.md | 2 +- README.md | 2 +- .../src/interfaces/tree-api.test.ts | 17 +++++++++++++++ .../react-arborist/src/interfaces/tree-api.ts | 21 ++++++++++++------- 5 files changed, 33 insertions(+), 11 deletions(-) diff --git a/.changes/scrollto-latest-call-wins.md b/.changes/scrollto-latest-call-wins.md index 9b211677..310041e2 100644 --- a/.changes/scrollto-latest-call-wins.md +++ b/.changes/scrollto-latest-call-wins.md @@ -1,4 +1,4 @@ --- type: fix --- -A newer `tree.scrollTo()` call now replaces an older one that is still waiting for its row, so the list no longer jumps back to the older target when that row appears later. +A newer `tree.scrollTo()` call now replaces an older one that is still waiting for its row, so the list no longer jumps back to the older target when that row appears later. Scrolls that focusing or selecting a node start never replace a pending call. diff --git a/.changes/scrollto-surface-scroll-errors.md b/.changes/scrollto-surface-scroll-errors.md index 93fef957..7db3ef45 100644 --- a/.changes/scrollto-surface-scroll-errors.md +++ b/.changes/scrollto-surface-scroll-errors.md @@ -1,4 +1,4 @@ --- -type: fix +type: feature --- `tree.scrollTo()` no longer swallows errors thrown while scrolling to a node. It still ignores the case where the id never appears, but any other error now rejects the returned promise instead of failing silently. Scrolls that focusing, selecting or editing a node start on their own still never reject. diff --git a/README.md b/README.md index 43aeceb6..f378a414 100644 --- a/README.md +++ b/README.md @@ -967,7 +967,7 @@ Returns true if the node with _id_ is internal and is under the dragged node. _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. _tree_.**scrollToOffset**(_offset_) diff --git a/modules/react-arborist/src/interfaces/tree-api.test.ts b/modules/react-arborist/src/interfaces/tree-api.test.ts index 79992ea0..c424a78c 100644 --- a/modules/react-arborist/src/interfaces/tree-api.test.ts +++ b/modules/react-arborist/src/interfaces/tree-api.test.ts @@ -413,6 +413,23 @@ describe("a newer scrollTo replaces an older one that is still waiting", () => { expect(list.current.scrollToItem).toHaveBeenCalledTimes(1); expect(list.current.scrollToItem).toHaveBeenCalledWith(1, "smart"); }); + + test("a scroll started by select() does not replace it", async () => { + const store = createStore(rootReducer); + const list = { current: { scrollToItem: jest.fn() } as any }; + const api = new TreeApi(store, { data: rowData }, list, { current: null }); + const pending = api.scrollTo("late"); + // What the controlled `selection` effect in provider.tsx does. + api.select("b", { focus: false }); + api.update({ data: [...rowData, { id: "late" }] }); + jest.runAllTimers(); + await pending; + expect(jest.getTimerCount()).toBe(0); + expect(list.current.scrollToItem.mock.calls).toEqual([ + [1, "smart"], + [3, "smart"], + ]); + }); }); describe("scrollToOffset / scrollOffset set and read the vertical position (#194)", () => { diff --git a/modules/react-arborist/src/interfaces/tree-api.ts b/modules/react-arborist/src/interfaces/tree-api.ts index 170a008e..499d6dda 100644 --- a/modules/react-arborist/src/interfaces/tree-api.ts +++ b/modules/react-arborist/src/interfaces/tree-api.ts @@ -755,9 +755,20 @@ export class TreeApi { scrollTo(identity: Identity | T, align: Align = "smart") { if (!identity) return; - const id = this.identify(identity); const token = ++this.scrollToken; - const superseded = () => token !== this.scrollToken; + return this.scrollWhenReady(this.identify(identity), align, () => token !== this.scrollToken); + } + + /* For scrolls that are a side effect of focus, selection or editing. Nobody + awaits them, so a rejection would surface as an unhandled one. They also + leave the token alone, so a controlled selection update can't cancel a + pending scrollTo(). */ + private scrollQuietly(identity: Identity | T, align: Align = "smart") { + if (!identity) return; + this.scrollWhenReady(this.identify(identity), align, () => false).catch(() => {}); + } + + private scrollWhenReady(id: string, align: Align, superseded: () => boolean) { this.openParents(id); return utils .waitFor(() => superseded() || id in this.idToIndex) @@ -777,12 +788,6 @@ export class TreeApi { ); } - /* For scrolls that are a side effect of focus, selection or editing: nobody - awaits them, so a rejection would surface as an unhandled one. */ - private scrollQuietly(identity: Identity | T, align?: Align) { - this.scrollTo(identity, align)?.catch(() => {}); - } - /** * Horizontally scroll the list so the node's indented content is in view. * A no-op when the list doesn't overflow horizontally (the common case), so From e69895daf3f49874cd97b7f5442b25d1f95dc7be Mon Sep 17 00:00:00 2001 From: Melih Ozcan <59609421+melihozcan1@users.noreply.github.com> Date: Sun, 29 Nov 2026 20:05:45 +0300 Subject: [PATCH 4/4] Return a resolved promise from scrollTo when the id is empty --- .changes/scrollto-surface-scroll-errors.md | 2 +- modules/react-arborist/src/interfaces/tree-api.test.ts | 10 ++++++++++ modules/react-arborist/src/interfaces/tree-api.ts | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/.changes/scrollto-surface-scroll-errors.md b/.changes/scrollto-surface-scroll-errors.md index 7db3ef45..99c00c27 100644 --- a/.changes/scrollto-surface-scroll-errors.md +++ b/.changes/scrollto-surface-scroll-errors.md @@ -1,4 +1,4 @@ --- type: feature --- -`tree.scrollTo()` no longer swallows errors thrown while scrolling to a node. It still ignores the case where the id never appears, but any other error now rejects the returned promise instead of failing silently. Scrolls that focusing, selecting or editing a node start on their own still never reject. +`tree.scrollTo()` no longer swallows errors thrown while scrolling to a node. It still ignores the case where the id never appears, but any other error now rejects the returned promise instead of failing silently. Scrolls that focusing, selecting or editing a node start on their own still never reject. It also always returns a promise now, including when the id is empty. diff --git a/modules/react-arborist/src/interfaces/tree-api.test.ts b/modules/react-arborist/src/interfaces/tree-api.test.ts index c424a78c..89da28c1 100644 --- a/modules/react-arborist/src/interfaces/tree-api.test.ts +++ b/modules/react-arborist/src/interfaces/tree-api.test.ts @@ -369,6 +369,16 @@ describe("scrollTo stops waiting for an id that never appears (#403)", () => { }); }); +describe("scrollTo returns a promise even without an id", () => { + test.each([null, ""])("%p", async (id) => { + const store = createStore(rootReducer); + const list = { current: { scrollToItem: jest.fn() } as any }; + const api = new TreeApi(store, { data: rowData }, list, { current: null }); + await expect(api.scrollTo(id)).resolves.toBeUndefined(); + expect(list.current.scrollToItem).not.toHaveBeenCalled(); + }); +}); + describe("scrollTo surfaces errors thrown while scrolling", () => { function setup() { const store = createStore(rootReducer); diff --git a/modules/react-arborist/src/interfaces/tree-api.ts b/modules/react-arborist/src/interfaces/tree-api.ts index 499d6dda..6b03eff9 100644 --- a/modules/react-arborist/src/interfaces/tree-api.ts +++ b/modules/react-arborist/src/interfaces/tree-api.ts @@ -754,7 +754,7 @@ export class TreeApi { /* Scrolling */ scrollTo(identity: Identity | T, align: Align = "smart") { - if (!identity) return; + if (!identity) return Promise.resolve(); const token = ++this.scrollToken; return this.scrollWhenReady(this.identify(identity), align, () => token !== this.scrollToken); }