From f2e1b472987c3d2a40451d9915a0b350c14325c7 Mon Sep 17 00:00:00 2001
From: paf <paf@titelfrei.de>
Date: Wed, 31 Mar 2021 11:59:42 +0200
Subject: [PATCH] unsubscribes all subscriptions in group-monitor.component
 correctly when destroyed

---
 .../group-monitor.component.html              |  2 +-
 .../group-monitor/group-monitor.component.ts  | 46 +++++++++----------
 .../group-monitor/group-monitor.service.ts    |  5 ++
 3 files changed, 27 insertions(+), 26 deletions(-)

diff --git a/src/app/group-monitor/group-monitor.component.html b/src/app/group-monitor/group-monitor.component.html
index 891f02d9..549f28ea 100644
--- a/src/app/group-monitor/group-monitor.component.html
+++ b/src/app/group-monitor/group-monitor.component.html
@@ -5,7 +5,7 @@
   </p>
   <span class="fill-remaining-space"></span>
   <p>
-    <mat-chip-list *ngIf="connectionStatus$ | async as connectionStatus">
+    <mat-chip-list *ngIf="gms.connectionStatus$ | async as connectionStatus">
 
       <mat-chip [class]="connectionStatus + ' connection-status'">
         <mat-icon>
diff --git a/src/app/group-monitor/group-monitor.component.ts b/src/app/group-monitor/group-monitor.component.ts
index 31176cc6..5266bb9e 100644
--- a/src/app/group-monitor/group-monitor.component.ts
+++ b/src/app/group-monitor/group-monitor.component.ts
@@ -16,7 +16,6 @@ import {
   TestViewDisplayOptions,
   TestViewDisplayOptionKey, Selection, TestSession, TestSessionSetStats, CommandResponse, UIMessage
 } from './group-monitor.interfaces';
-import { ConnectionStatus } from '../shared/websocket-backend.service';
 import { GroupMonitorService } from './group-monitor.service';
 
 @Component({
@@ -37,8 +36,6 @@ export class GroupMonitorComponent implements OnInit, OnDestroy {
   ownGroup$: Observable<GroupData>;
   private ownGroupName = '';
 
-  connectionStatus$: Observable<ConnectionStatus>;
-
   selectedElement: Selection;
   markedElement: Selection;
 
@@ -57,30 +54,31 @@ export class GroupMonitorComponent implements OnInit, OnDestroy {
 
   messages: UIMessage[] = [];
 
-  private routingSubscription: Subscription = null;
+  private subscriptions: Subscription[] = [];
 
   @ViewChild('adminbackground') mainElem:ElementRef;
   @ViewChild('sidenav', { static: true }) sidenav: MatSidenav;
 
   ngOnInit(): void {
-    this.routingSubscription = this.route.params.subscribe(params => {
-      this.ownGroup$ = this.bs.getGroupData(params['group-name']);
-      this.ownGroupName = params['group-name'];
-      this.gms.connect(params['group-name']);
-    });
-    this.gms.sessionsStats$.subscribe(stats => {
-      this.onSessionsUpdate(stats);
-    });
-    this.gms.checkedStats$.subscribe(stats => {
-      this.onCheckedChange(stats);
-    });
-    this.connectionStatus$ = this.bs.connectionStatus$;
-    this.gms.commandResponses$.subscribe(commandResponse => {
-      this.messages.push(this.commandResponseToMessage(commandResponse));
-    });
-    this.gms.commandResponses$
-      .pipe(switchMap(() => interval(7000)))
-      .subscribe(() => this.messages.shift());
+    this.subscriptions = [
+      this.route.params.subscribe(params => {
+        this.ownGroup$ = this.bs.getGroupData(params['group-name']);
+        this.ownGroupName = params['group-name'];
+        this.gms.connect(params['group-name']);
+      }),
+      this.gms.sessionsStats$.subscribe(stats => {
+        this.onSessionsUpdate(stats);
+      }),
+      this.gms.checkedStats$.subscribe(stats => {
+        this.onCheckedChange(stats);
+      }),
+      this.gms.commandResponses$.subscribe(commandResponse => {
+        this.messages.push(this.commandResponseToMessage(commandResponse));
+      }),
+      this.gms.commandResponses$
+        .pipe(switchMap(() => interval(7000)))
+        .subscribe(() => this.messages.shift())
+    ];
   }
 
   private commandResponseToMessage(commandResponse: CommandResponse): UIMessage {
@@ -103,10 +101,8 @@ export class GroupMonitorComponent implements OnInit, OnDestroy {
   }
 
   ngOnDestroy(): void {
-    if (this.routingSubscription !== null) {
-      this.routingSubscription.unsubscribe();
-    }
     this.gms.disconnect();
+    this.subscriptions.forEach(subscription => subscription.unsubscribe());
   }
 
   ngAfterViewChecked(): void {
diff --git a/src/app/group-monitor/group-monitor.service.ts b/src/app/group-monitor/group-monitor.service.ts
index 8629c0ef..e6522901 100644
--- a/src/app/group-monitor/group-monitor.service.ts
+++ b/src/app/group-monitor/group-monitor.service.ts
@@ -16,6 +16,7 @@ import {
   TestSessionFilter, TestSessionSetStats,
   TestSessionsSuperStates, CommandResponse
 } from './group-monitor.interfaces';
+import { ConnectionStatus } from '../shared/websocket-backend.service';
 
 /**
  * func:
@@ -43,6 +44,8 @@ export class GroupMonitorService {
   filters$: Subject<TestSessionFilter[]>;
   checkingOptions: CheckingOptions;
 
+  connectionStatus$: Observable<ConnectionStatus>;
+
   private groupName: string;
 
   get sessions$(): Observable<TestSession[]> {
@@ -132,6 +135,8 @@ export class GroupMonitorService {
         tap(sessions => this.synchronizeChecked(sessions))
       )
       .subscribe(this._sessions$);
+
+    this.connectionStatus$ = this.bs.connectionStatus$;
   }
 
   disconnect(): void {
-- 
GitLab