From: Kai Moritz Date: Sat, 7 Jan 2023 18:45:48 +0000 (+0100) Subject: fix: chatroom stops listening, when destroyed X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=e9f8ae1d5c844db6172ae6f7c547c96d506a3c81;p=demos%2Fkafka%2Fchat fix: chatroom stops listening, when destroyed --- 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) {