From e9f8ae1d5c844db6172ae6f7c547c96d506a3c81 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 7 Jan 2023 19:45:48 +0100 Subject: [PATCH] fix: chatroom stops listening, when destroyed --- src/app/chatroom.service.ts | 6 ++++++ src/app/chatroom/chatroom.component.ts | 9 +++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/app/chatroom.service.ts b/src/app/chatroom.service.ts index 11ad225a..74bda941 100644 --- a/src/app/chatroom.service.ts +++ b/src/app/chatroom.service.ts @@ -40,6 +40,12 @@ export class ChatroomService { return observable; } + unlisten(): void { + this.closeChannel(); + this.channelUri = 'UNKNOWN'; + this.channel = new Subscriber(); + } + // Creates SSE event source, handles SSE events private openChannel(): void { diff --git a/src/app/chatroom/chatroom.component.ts b/src/app/chatroom/chatroom.component.ts index 7178a5a8..c94a55bf 100644 --- a/src/app/chatroom/chatroom.component.ts +++ b/src/app/chatroom/chatroom.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit, NgZone } from '@angular/core'; +import { Component, OnInit, NgZone, OnDestroy } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { ChatroomService } from "../chatroom.service"; import { UserService } from "../user.service"; @@ -10,7 +10,7 @@ import { Message } from "../message"; templateUrl: './chatroom.component.html', styleUrls: ['./chatroom.component.less'] }) -export class ChatroomComponent implements OnInit { +export class ChatroomComponent implements OnInit, OnDestroy { chatroom: Chatroom = { id: 'FOO', name: 'BAR'}; messages: Message[] = []; @@ -27,6 +27,11 @@ export class ChatroomComponent implements OnInit { this.userService.assertUserisKnown(() => this.getChatroom()); } + ngOnDestroy() { + this.chatroomsService.unlisten(); + this.chatroom = { id: 'FOO', name: 'BAR' }; + } + getChatroom(): void { const id: string | null = this.route.snapshot.paramMap.get('id'); if (id === null) { -- 2.20.1