Merge "Ensure that gr-diff-view only accesses dependencies when connected"
diff --git a/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view.ts b/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view.ts
index b1cfe55..92657dc 100644
--- a/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view.ts
+++ b/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view.ts
@@ -121,6 +121,7 @@
 import {browserModelToken} from '../../../services/browser/browser-model';
 import {commentsModelToken} from '../../../services/comments/comments-model';
 import {resolve, DIPolymerElement} from '../../../services/dependency';
+import {BehaviorSubject} from 'rxjs';
 
 const ERR_REVIEW_STATUS = 'Couldn’t change file review status.';
 const LOADING_BLAME = 'Loading blame...';
@@ -382,8 +383,11 @@
 
   private subscriptions: Subscription[] = [];
 
+  private connected$ = new BehaviorSubject(false);
+
   override connectedCallback() {
     super.connectedCallback();
+    this.connected$.next(true);
     this._throttledToggleFileReviewed = throttleWrap(_ =>
       this._handleToggleFileReviewed()
     );
@@ -471,6 +475,7 @@
       s.unsubscribe();
     }
     this.subscriptions = [];
+    this.connected$.next(false);
     super.disconnectedCallback();
   }
 
@@ -1164,7 +1169,7 @@
         )
       );
     }
-    promises.push(until(this.getCommentsModel().commentsLoading$, isFalse));
+    promises.push(this.waitUntilCommentsLoaded());
 
     this.$.diffHost.cancel();
     this.$.diffHost.clearDiffContent();
@@ -1219,6 +1224,11 @@
       });
   }
 
+  private async waitUntilCommentsLoaded() {
+    await until(this.connected$, c => c);
+    await until(this.getCommentsModel().commentsLoading$, isFalse);
+  }
+
   /**
    * If the params specify a diff address then configure the diff cursor.
    */