Skip to content

preserve data viewer scroll position and show new columns on refresh - #17883

Merged
kevinushey merged 3 commits into
mainfrom
feature/preserve-data-viewer-scroll
Jun 5, 2026
Merged

kevinushey merged 3 commits into
mainfrom
feature/preserve-data-viewer-scroll

Conversation

@kevinushey

Copy link
Copy Markdown
Collaborator

Summary

When a viewed data frame is changed in place -- a column added or removed, or values edited in an existing column -- the data viewer refreshes by tearing the grid down and rebuilding it. Two things went wrong on that refresh:

  1. Scroll position was lost. destroyGrid zeroes the viewport scroll and the grid rebuilt from row 1, so scrolling to row ~600 and then adding a variable snapped you back to the top.
  2. A newly added column did not appear. The column fetch (buildRequestedColumns) clamps the requested window to totalCols, but totalCols was never reset on refresh and still held the previous frame's count -- so a column added since fell outside the clamp and was never fetched.

This PR fixes both.

Changes

  • Preserve scroll across an in-place refresh (DataViewer.js): capture the scroll position before bootstrap() tears the grid down, and restore it once the first row batch re-establishes the scrollable height -- but only when the unfiltered row count (recordsTotal) is unchanged. A changed row count signals genuinely new data (subset, reassignment, rows added/removed), so the grid still resets to the top in that case. "Reset View" continues to return to the top.
  • Show newly added columns (DataViewer.js): reset the stale totalCols in resetGridState, consistent with how the sibling frame state (cols, totalRows) is already cleared. 0 means "unknown," so the window is requested in full and the server bounds it to the real frame. resetGridState only runs from bootstrap, which always re-fetches columns and repopulates totalCols. Wide, column-paginated frames are unaffected: columnOffset/maxDisplayColumns are still preserved, so the column window stays put.

Testing

Two new Playwright tests in data_viewer.test.ts, plus the existing data-viewer and pagination/sorting suites (19 tests total) all pass on the dev desktop build:

  • scroll position is preserved across an in-place refresh, and the newly added column appears;
  • scroll position resets to the top when the underlying row count changes.

Fixes #17861

When a viewed object is changed in place (a column added or removed, or
values edited in an existing column), the data viewer refreshes by tearing
the grid down and rebuilding it, which snapped the user back to the first
row. Capture the scroll position before the rebuild and restore it once the
first row batch re-establishes the scrollable height -- but only when the
unfiltered row count is unchanged. A changed row count signals genuinely new
data (subset, reassignment, rows added/removed), so the grid still resets to
the top in that case.

Addresses #17861
On an in-place refresh the column fetch (buildRequestedColumns) clamped the
requested column window to totalCols, but totalCols was never reset and still
held the previous frame's count. A column added since (e.g. `df$new <- ...`)
therefore fell outside the clamp and was never fetched, so the new column did
not appear until the viewer was reopened. resetGridState already clears the
sibling frame state (cols, and totalRows via invalidateCache); reset totalCols
there too so the window is requested in full and the server bounds it to the
real frame. resetGridState only runs from bootstrap, which always re-fetches
columns and repopulates totalCols.

Addresses #17861
@kevinushey kevinushey added this to the Blue Plumbago milestone Jun 5, 2026
@kevinushey
kevinushey requested a review from gtritchie June 5, 2026 18:25
@gtritchie

Copy link
Copy Markdown
Member

Review summary

Ran four specialized passes (general code, tests, comments, silent failures) and independently verified the contested findings against the source. One real robustness bug worth fixing, plus a few coverage gaps.

Critical: pendingScrollRestore can be stranded by a failed refresh, then fire on a later unrelated bootstrap

captureScrollForRefresh() sets pendingScrollRestore then calls bootstrap(). It is only ever consumed (and nulled) inside the fetchRows success callback (DataViewer.js:3897). Every path that aborts the rebuild before that callback leaves the value set:

  • fetchColumns error returns without calling the callback (DataViewer.js:475,478)
  • fetchRows .catch calls showError and never the callback (DataViewer.js:544)
  • stale-token discard (DataViewer.js:532) and initGrid early returns (:3799, :3867)

A stranded value then survives to the next bootstrap that does reach the success callback — e.g. column pagination (columnNav -> bootstrap, DataViewer.js:4411), which carries no scroll intent. The only guard is restore.rows !== totalRows (DataViewer.js:2139); column paging preserves the row count, so the guard passes and the stale offset is applied to an unrelated render. This also violates the variable's documented invariant ("Null except during the brief window between refreshData() and the restore", DataViewer.js:134).

Practical impact is low (requires a refresh to fail server-side first, then a column-page action), but the fix is trivial and defensive: clear pendingScrollRestore unconditionally at the top of bootstrap() (or in destroyGrid), reading it into a local for the current cycle. That binds capture/consume to a single rebuild and closes the leak.

Test coverage gaps

  • "Reset View" path is untested. The fix added pendingScrollRestore = null in refreshAndReset (DataViewer.js:4472) so Reset View returns to top — no test guards it. A regression here would silently restore the old position, the opposite of the contract.
  • Phantom coverage in the test name. The test is titled "(or editing values in an existing column)" (data_viewer.test.ts:309) but only exercises column add. Column removal (df$V5 <- NULL) and value edits are different paths to the same refresh and aren't tested.
  • The inactive-tab lastScrollTop fallback (DataViewer.js:2119-2124) is entirely untested — the more subtle half of the capture logic, and easy to break.

Suggestions

  • Global-search interaction (minor, pre-existing). cachedSearch is cleared on refresh and not persisted, while the restore gates on unfiltered totalRows. With an active global search, a refresh drops the search, filteredRows jumps back to totalRows, the guard still passes, and the restored offset points at a different row (clamped, never crashing). Per-column filters are persisted and unaffected.
  • RAF not cancelled on teardown (low). The restore requestAnimationFrame (DataViewer.js:2146) isn't tracked/cancelled in destroyGrid (unlike pendingScrollbarRaf). A rapid second refresh in the tiny pre-paint window could apply a stale offset. Add a drawCounter check inside the RAF, or cancel it on teardown.
  • Magic number. target - 23 (data_viewer.test.ts:355) encodes ~1 ROW_HEIGHT of slack with no comment tying it to the constant.

Recommended action

  1. Fix the stranding bug — clear pendingScrollRestore at the start of bootstrap(). One line, removes a latent cross-refresh leak.
  2. Add the Reset View test and a column-removal test (makes the advertised behavior real).
  3. Consider the inactive-tab fallback test and a one-line note on the global-search edge.

Only item 1 is a code change worth considering before merge, and even that is low-severity.

@gtritchie gtritchie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

See feedback

@github-actions

github-actions Bot commented Jun 5, 2026 •

Copy link
Copy Markdown

🍎 macOS Desktop E2E · ARM64

❌ 1 test(s) failed

███████████████████░ 99% pass rate

✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
309 1 13 1
📥 Download Playwright report

⬇️ playwright-report.zip — extract and open index.html

Includes screenshots, traces, and step-by-step details for every test. Expires in 15 days.


2 shards · macos-26 ARM64 · commit 6fc306438852dd81ccf2acde5e6a4181632bd2c2 · Run #96

Address review feedback on #17883:
- bind the captured scroll position to a single rebuild via activeScrollRestore, so a refresh that aborts before its restore callback can't leak the offset onto an unrelated later bootstrap (e.g. column pagination)
- guard the restore requestAnimationFrame with a drawCounter token so a rapid second refresh can't apply a stale offset
- note the unfiltered-total gate / global-search edge in the restore comment
- add Playwright tests for column removal and Reset View; rename the column-add test and annotate the ROW_HEIGHT slack
@kevinushey

Copy link
Copy Markdown
Collaborator Author

Thanks for the thorough review! Addressed in 6fc3064:

Code

  • Stranding bug (critical): the captured scroll position is now bound to a single rebuild via a separate activeScrollRestore. bootstrap() unconditionally moves pendingScrollRestore into it (clearing the handoff slot) at the top of every rebuild, so a refresh that aborts before its restore callback can no longer leak the offset onto an unrelated later bootstrap (e.g. column pagination).
  • RAF teardown race: the restore requestAnimationFrame now captures a drawCounter token and bails if a newer fetch has superseded it.
  • Global-search edge: added a note in the restoreScrollAfterRefresh comment explaining the unfiltered-total gate, that per-column filters are preserved, and that an active global search lands on a clamped (never out-of-range) row.

Tests

  • Added a column-removal test and a Reset View test (guards the explicit position drop in refreshAndReset).
  • Renamed the column-add test so its name/comment no longer over-claim, and annotated the ROW_HEIGHT (23px) slack.

Deliberately skipped

  • The inactive-tab lastScrollTop fallback test -- exercising it reliably needs an activate/deactivate cycle that tends to be flaky in e2e, so I left it out rather than add a fragile test. Happy to add it if you'd like the coverage.

All 16 data-viewer tests pass on the dev desktop build.

@kevinushey
kevinushey requested a review from gtritchie June 5, 2026 20:44

@gtritchie gtritchie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@kevinushey
kevinushey merged commit 96e4d28 into main Jun 5, 2026
9 of 10 checks passed
@kevinushey
kevinushey deleted the feature/preserve-data-viewer-scroll branch June 5, 2026 22:56
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.

Preserve scroll position when data view is refreshed

2 participants