Repository navigation
Surface scrollTo scroll errors and let a newer scrollTo replace an older one #407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ef60ab1
02bebbc
c35f357
1fdc44d
e69895d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. Scrolls that focusing or selecting a node start never replace a pending call. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +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. It also always returns a promise now, including when the id is empty. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -364,10 +364,84 @@ 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 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); | ||
| const scrollToItem = jest.fn(() => { | ||
| throw new Error("scroll failed"); | ||
| }); | ||
| const list = { current: { scrollToItem } as any }; | ||
| 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"); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added in c35f357: |
||
| }); | ||
|
|
||
| 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", () => { | ||
| 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"); | ||
| }); | ||
|
|
||
| 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)", () => { | ||
| const data = [{ id: "a" }, { id: "b" }, { id: "c" }]; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,6 +43,7 @@ export class TreeApi<T> { | |
| * 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<RootState, Actions>, | ||
|
|
@@ -348,7 +349,7 @@ export class TreeApi<T> { | |
| edit(node: string | IdObj | T): Promise<EditResult> { | ||
| 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) => { | ||
|
|
@@ -413,7 +414,7 @@ export class TreeApi<T> { | |
| 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); | ||
| } | ||
| } | ||
|
|
@@ -456,7 +457,7 @@ export class TreeApi<T> { | |
| mostRecent: id, | ||
| }); | ||
| } | ||
| this.scrollTo(id, opts.align); | ||
| this.scrollQuietly(id, opts.align); | ||
| if (this.focusedNode && changeFocus) { | ||
| safeRun(this.props.onFocus, this.focusedNode); | ||
| } | ||
|
|
@@ -479,7 +480,7 @@ export class TreeApi<T> { | |
| 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); | ||
| } | ||
|
|
@@ -499,7 +500,7 @@ export class TreeApi<T> { | |
| 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); | ||
| } | ||
|
|
@@ -732,7 +733,7 @@ export class TreeApi<T> { | |
| } | ||
| } | ||
| this.redrawList(); | ||
| this.scrollTo(this.focusedNode); | ||
| this.scrollQuietly(this.focusedNode); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -753,23 +754,38 @@ export class TreeApi<T> { | |
| /* Scrolling */ | ||
|
|
||
| scrollTo(identity: Identity | T, align: Align = "smart") { | ||
| if (!identity) return Promise.resolve(); | ||
| const token = ++this.scrollToken; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for adding A user clicking another row winning seems fine. The case that worries me is a controlled Could internal scrolls skip the token bump (or use their own), so only public
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 1fdc44d. Added a test for your case: |
||
| 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; | ||
| const id = this.identify(identity); | ||
| this.scrollWhenReady(this.identify(identity), align, () => false).catch(() => {}); | ||
| } | ||
|
|
||
| private scrollWhenReady(id: string, align: Align, superseded: () => boolean) { | ||
| this.openParents(id); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two small questions, not blockers:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both intended:
|
||
| 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. | ||
| () => {}, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that errors thrown inside the Maybe give those call sites a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| ); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
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
scrollToreturns a promise, butscrollTo(null)(or any falsy id) hits the earlyif (!identity) return;and returnsundefined. JS callers who writetree.scrollTo(selectedId).then(...)with a null id would get a TypeError. ReturningPromise.resolve()from that early exit would make the docs hold in every case.There was a problem hiding this comment.
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(), soscrollToreturns a promise for any id. It still leaves a pending call alone. Added a test fornulland""; both fail without the change. The changeset mentions it too.