From 2cdc9dced3c5d01055d6f8dfbc8da52fddd396bb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=20K=C3=A4sler?= <tom.kaesler@mni.thm.de>
Date: Wed, 13 Nov 2019 16:12:07 +0100
Subject: [PATCH] Unsubscribe from comment stream

Refactor room pages and move attributes needed in all components to parent component.
---
 .../room-creator-page.component.ts            |  9 +-------
 .../room-moderator-page.component.ts          |  9 +-------
 .../room-participant-page.component.ts        |  9 +-------
 .../comment-list/comment-list.component.ts    | 10 +++++++--
 .../shared/room-page/room-page.component.ts   | 21 +++++++++++++++----
 5 files changed, 28 insertions(+), 30 deletions(-)

diff --git a/src/app/components/creator/room-creator-page/room-creator-page.component.ts b/src/app/components/creator/room-creator-page/room-creator-page.component.ts
index d6e7dc84c..c501669a0 100644
--- a/src/app/components/creator/room-creator-page/room-creator-page.component.ts
+++ b/src/app/components/creator/room-creator-page/room-creator-page.component.ts
@@ -37,8 +37,6 @@ export class RoomCreatorPageComponent extends RoomPageComponent implements OnIni
   moderatorCommentCounter: number;
   urlToCopy = 'https://frag.jetzt/participant/room/';
 
-  listenerFn: () => void;
-
   constructor(protected roomService: RoomService,
               protected notification: NotificationService,
               protected route: ActivatedRoute,
@@ -51,7 +49,7 @@ export class RoomCreatorPageComponent extends RoomPageComponent implements OnIni
               private liveAnnouncer: LiveAnnouncer,
               private _r: Renderer2,
               public eventService: EventService) {
-    super(roomService, route, location, wsCommentService, commentService);
+    super(roomService, route, location, wsCommentService, commentService, eventService);
     langService.langEmitter.subscribe(lang => translateService.use(lang));
   }
 
@@ -89,11 +87,6 @@ export class RoomCreatorPageComponent extends RoomPageComponent implements OnIni
     });
   }
 
-  ngOnDestroy() {
-    this.listenerFn();
-    this.eventService.makeFocusOnInputFalse();
-  }
-
   public announce() {
     this.liveAnnouncer.clear();
     this.liveAnnouncer.announce('Du befindest dich in der von dir erstellten Sitzung. ' +
diff --git a/src/app/components/moderator/room-moderator-page/room-moderator-page.component.ts b/src/app/components/moderator/room-moderator-page/room-moderator-page.component.ts
index 5449ff267..9825e7ec4 100644
--- a/src/app/components/moderator/room-moderator-page/room-moderator-page.component.ts
+++ b/src/app/components/moderator/room-moderator-page/room-moderator-page.component.ts
@@ -28,8 +28,6 @@ export class RoomModeratorPageComponent extends RoomPageComponent implements OnI
   moderatorCommentCounter: number;
   viewModuleCount = 1;
 
-  listenerFn: () => void;
-
   constructor(protected location: Location,
               protected roomService: RoomService,
               protected route: ActivatedRoute,
@@ -41,7 +39,7 @@ export class RoomModeratorPageComponent extends RoomPageComponent implements OnI
               public eventService: EventService,
               private liveAnnouncer: LiveAnnouncer,
               private _r: Renderer2) {
-    super(roomService, route, location, wsCommentService, commentService);
+    super(roomService, route, location, wsCommentService, commentService, eventService);
     langService.langEmitter.subscribe(lang => translateService.use(lang));
   }
 
@@ -123,11 +121,6 @@ export class RoomModeratorPageComponent extends RoomPageComponent implements OnI
     });
   }
 
-  ngOnDestroy() {
-    this.listenerFn();
-    this.eventService.makeFocusOnInputFalse();
-  }
-
   public announce() {
     this.liveAnnouncer.announce('Du befindest dich in der Sitzung in der du als Moderator gewählt wurdest. ' +
       'Drücke die Taste 1 um auf die Fragen-Übersicht zu gelangen, ' +
diff --git a/src/app/components/participant/room-participant-page/room-participant-page.component.ts b/src/app/components/participant/room-participant-page/room-participant-page.component.ts
index 78912bb65..366754f68 100644
--- a/src/app/components/participant/room-participant-page/room-participant-page.component.ts
+++ b/src/app/components/participant/room-participant-page/room-participant-page.component.ts
@@ -28,8 +28,6 @@ export class RoomParticipantPageComponent extends RoomPageComponent implements O
   deviceType = localStorage.getItem('deviceType');
   user: User;
 
-  listenerFn: () => void;
-
   constructor(protected location: Location,
               protected roomService: RoomService,
               protected route: ActivatedRoute,
@@ -41,7 +39,7 @@ export class RoomParticipantPageComponent extends RoomPageComponent implements O
               private liveAnnouncer: LiveAnnouncer,
               private _r: Renderer2,
               public eventService: EventService) {
-    super(roomService, route, location, wsCommentService, commentService);
+    super(roomService, route, location, wsCommentService, commentService, eventService);
     langService.langEmitter.subscribe(lang => translateService.use(lang));
   }
 
@@ -74,11 +72,6 @@ export class RoomParticipantPageComponent extends RoomPageComponent implements O
     });
   }
 
-  ngOnDestroy() {
-    this.listenerFn();
-    this.eventService.makeFocusOnInputFalse();
-  }
-
   public announce() {
     this.liveAnnouncer.clear();
     this.liveAnnouncer.announce('Du befindest dich in der Sitzung mit dem von dir eingegebenen Sitzungs-Code. ' +
diff --git a/src/app/components/shared/comment-list/comment-list.component.ts b/src/app/components/shared/comment-list/comment-list.component.ts
index 877fe3793..702c642f3 100644
--- a/src/app/components/shared/comment-list/comment-list.component.ts
+++ b/src/app/components/shared/comment-list/comment-list.component.ts
@@ -1,4 +1,4 @@
-import { Component, ElementRef, Input, OnInit, ViewChild } from '@angular/core';
+import { Component, ElementRef, Input, OnInit, OnDestroy, ViewChild } from '@angular/core';
 import { Comment } from '../../../models/comment';
 import { CommentService } from '../../../services/http/comment.service';
 import { TranslateService } from '@ngx-translate/core';
@@ -26,7 +26,7 @@ import { Router } from '@angular/router';
   templateUrl: './comment-list.component.html',
   styleUrls: ['./comment-list.component.scss']
 })
-export class CommentListComponent implements OnInit {
+export class CommentListComponent implements OnInit, OnDestroy {
   @ViewChild('searchBox') searchField: ElementRef;
   @Input() user: User;
   @Input() roomId: string;
@@ -114,6 +114,12 @@ export class CommentListComponent implements OnInit {
     });
   }
 
+  ngOnDestroy() {
+    if (!this.freeze) {
+      this.commentStream.unsubscribe();
+    }
+  }
+
   checkScroll(): void {
     const currentScroll = document.documentElement.scrollTop;
     this.scroll = currentScroll >= 65;
diff --git a/src/app/components/shared/room-page/room-page.component.ts b/src/app/components/shared/room-page/room-page.component.ts
index 94d5605f8..14f9944c4 100644
--- a/src/app/components/shared/room-page/room-page.component.ts
+++ b/src/app/components/shared/room-page/room-page.component.ts
@@ -1,28 +1,33 @@
-import { Component, OnInit } from '@angular/core';
+import { Component, OnInit, OnDestroy } from '@angular/core';
 import { Room } from '../../../models/room';
 import { RoomService } from '../../../services/http/room.service';
 import { ActivatedRoute } from '@angular/router';
 import { Location } from '@angular/common';
 import { WsCommentServiceService } from '../../../services/websockets/ws-comment-service.service';
 import { CommentService } from '../../../services/http/comment.service';
+import { EventService } from '../../../services/util/event.service';
 import { Message } from '@stomp/stompjs';
+import { Subscription } from 'rxjs';
 
 @Component({
   selector: 'app-room-page',
   templateUrl: './room-page.component.html',
   styleUrls: ['./room-page.component.scss']
 })
-export class RoomPageComponent implements OnInit {
+export class RoomPageComponent implements OnInit, OnDestroy {
   room: Room = null;
   isLoading = true;
   commentCounter: number;
   protected moderationEnabled = false;
+  protected sub: Subscription;
+  protected listenerFn: () => void;
 
   constructor(protected roomService: RoomService,
               protected route: ActivatedRoute,
               protected location: Location,
               protected wsCommentService: WsCommentServiceService,
-              protected commentService: CommentService
+              protected commentService: CommentService,
+              protected eventService: EventService
   ) {
   }
 
@@ -32,6 +37,14 @@ export class RoomPageComponent implements OnInit {
     });
   }
 
+  ngOnDestroy() {
+    this.listenerFn();
+    this.eventService.makeFocusOnInputFalse();
+    if (this.sub) {
+      this.sub.unsubscribe();
+    }
+  }
+
   protected afterRoomLoadHook() {
 
   }
@@ -51,7 +64,7 @@ export class RoomPageComponent implements OnInit {
         .subscribe(commentCounter => {
           this.commentCounter = commentCounter;
         });
-      this.wsCommentService.getCommentStream(this.room.id).subscribe((message: Message) => {
+      this.sub = this.wsCommentService.getCommentStream(this.room.id).subscribe((message: Message) => {
         const msg = JSON.parse(message.body);
         const payload = msg.payload;
         if (msg.type === 'CommentCreated') {
-- 
GitLab