Fix the visibility listener of gr-change-list-view
It has also been firing when not connected to the DOM. A recent change
was made that would not destroy gr-change-list-view when navigating to
other pages. So even on the change page you would see a scroll position
reset when coming to it from another tab after 10 seconds.
The fix is to remove the listener when the component is disconnected.
This was tested manually using Dev Helper.
Google-Bug-Id: b/224768752
Release-Notes: skip
Change-Id: I52082a3bcf2d0e4430b17694dca479ed87dff8b3
diff --git a/polygerrit-ui/app/elements/change-list/gr-change-list-view/gr-change-list-view.ts b/polygerrit-ui/app/elements/change-list/gr-change-list-view/gr-change-list-view.ts
index 695df8f..6da4843 100644
--- a/polygerrit-ui/app/elements/change-list/gr-change-list-view/gr-change-list-view.ts
+++ b/polygerrit-ui/app/elements/change-list/gr-change-list-view/gr-change-list-view.ts
@@ -110,24 +110,36 @@
this.addEventListener('next-page', () => this.handleNextPage());
this.addEventListener('previous-page', () => this.handlePreviousPage());
this.addEventListener('reload', () => this.reload());
- // We are not currently verifying if the view is actually visible. We rely
- // on gr-app-element to restamp the component if view changes
- document.addEventListener('visibilitychange', () => {
- if (document.visibilityState === 'visible') {
- if (
- Date.now() - this.lastVisibleTimestampMs >
- RELOAD_DASHBOARD_INTERVAL_MS
- )
- this.reload();
- } else {
- this.lastVisibleTimestampMs = Date.now();
- }
- });
}
+ private readonly visibilityChangeListener = () => {
+ if (document.visibilityState === 'visible') {
+ if (
+ Date.now() - this.lastVisibleTimestampMs >
+ RELOAD_DASHBOARD_INTERVAL_MS
+ )
+ this.reload();
+ } else {
+ this.lastVisibleTimestampMs = Date.now();
+ }
+ };
+
override connectedCallback() {
super.connectedCallback();
this.loadPreferences();
+ document.addEventListener(
+ 'visibilitychange',
+ this.visibilityChangeListener
+ );
+ }
+
+ override disconnectedCallback() {
+ document.removeEventListener(
+ 'visibilitychange',
+ this.visibilityChangeListener
+ );
+ this.loadPreferences();
+ super.disconnectedCallback();
}
static override get styles() {