Introduce the concept of Finalizable to tear down services.
This will allow services and AppContext to be properly cleared
when necessary.
This is in the interest of getting rid of globals. In the parent change,
it was discovered that some services don't clean up nicely hence this
change.
Additionally, as we will allow creating and removing of certain services
(e.g. ChangeService and CommentsService) they will also need the ability
to be cleaned up.
In addition, clean up how we register services for tests and clean up
after tests.
Because we now subscribe more often as we are recreating Services for
each test, subscriptions were leaking and there would be
stack-overflow-errors in the subscribe logic. To address this, move away
from using disconnected$ and instead rely on .unsubscribe() for
subscriptions;
Google-Bug-Id: b/206459178
Change-Id: I93ee6933ccc68728e1de174e1daac52cc4b83dc7
diff --git a/polygerrit-ui/app/elements/diff/gr-diff-mode-selector/gr-diff-mode-selector.ts b/polygerrit-ui/app/elements/diff/gr-diff-mode-selector/gr-diff-mode-selector.ts
index cf328f1..4f6ae14 100644
--- a/polygerrit-ui/app/elements/diff/gr-diff-mode-selector/gr-diff-mode-selector.ts
+++ b/polygerrit-ui/app/elements/diff/gr-diff-mode-selector/gr-diff-mode-selector.ts
@@ -14,6 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+import {Subscription} from 'rxjs';
import '@polymer/iron-icon/iron-icon';
import '@polymer/iron-a11y-announcer/iron-a11y-announcer';
import '../../../styles/shared-styles';
@@ -27,8 +28,6 @@
import {getAppContext} from '../../../services/app-context';
import {fireIronAnnounce} from '../../../utils/event-util';
import {diffViewMode$} from '../../../services/browser/browser-model';
-import {Subject} from 'rxjs';
-import {takeUntil} from 'rxjs/operators';
@customElement('gr-diff-mode-selector')
export class GrDiffModeSelector extends PolymerElement {
@@ -51,7 +50,7 @@
private readonly userService = getAppContext().userService;
- disconnected$ = new Subject();
+ private subscriptions: Subscription[] = [];
constructor() {
super();
@@ -62,13 +61,17 @@
(
IronA11yAnnouncer as unknown as FixIronA11yAnnouncer
).requestAvailability();
- diffViewMode$
- .pipe(takeUntil(this.disconnected$))
- .subscribe(diffView => (this.mode = diffView));
+ this.subscriptions.push(
+ diffViewMode$.subscribe(diffView => (this.mode = diffView))
+ );
}
override disconnectedCallback() {
- this.disconnected$.next();
+ for (const s of this.subscriptions) {
+ s.unsubscribe();
+ }
+ this.subscriptions = [];
+ super.disconnectedCallback();
}
/**