From: Kai Moritz Date: Sun, 12 Oct 2025 11:18:19 +0000 (+0200) Subject: feat: `ChatroomService` remembers the current `Chatroom` X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=ea7f2fa6b4d0095978a078ad78b58c7266bf6be5;p=demos%2Fkafka%2Fchat feat: `ChatroomService` remembers the current `Chatroom` --- diff --git a/pacts b/pacts index 8c4e57bb..d7ba11ea 160000 --- a/pacts +++ b/pacts @@ -1 +1 @@ -Subproject commit 8c4e57bb0973a60deb4d16b4e3ff13ba8a45989e +Subproject commit d7ba11ea3b6c4eb24bf8d89a910ffbc90d26b441 diff --git a/src/app/chatroom/chatroom.component.ts b/src/app/chatroom/chatroom.component.ts index 40839b06..32dd6a5f 100644 --- a/src/app/chatroom/chatroom.component.ts +++ b/src/app/chatroom/chatroom.component.ts @@ -41,8 +41,8 @@ export class ChatroomComponent implements OnInit, OnDestroy { } else { this.chatroomsService - .getChatroom(shard, id) - .subscribe(chatroom => this.chatroom = chatroom); + .setChatroom(shard, id) + .then(chatroom => this.chatroom = chatroom); this.chatroomsService .listen(shard, id) .subscribe({ diff --git a/src/app/chatroom/chatroom.service.pact.spec.ts b/src/app/chatroom/chatroom.service.pact.spec.ts index 2d0b1566..b932b498 100644 --- a/src/app/chatroom/chatroom.service.pact.spec.ts +++ b/src/app/chatroom/chatroom.service.pact.spec.ts @@ -1,7 +1,6 @@ import { Matchers, Pact, SpecificationVersion } from '@pact-foundation/pact'; import { TestBed } from '@angular/core/testing'; import { provideHttpClient } from '@angular/common/http'; -import { lastValueFrom } from 'rxjs'; import { ChatroomService } from './chatroom.service'; import { Chatroom } from './chatroom.model'; import { APP_PROPS } from '../app.tokens'; @@ -14,7 +13,7 @@ describe('Pact between the ChatroomService and the backend', () => { provider: 'ChatBackendController', spec: SpecificationVersion.SPECIFICATION_VERSION_V4, dir: 'pacts', - logLevel: 'debug', + logLevel: 'info', }); @@ -53,9 +52,10 @@ describe('Pact between the ChatroomService and the backend', () => { }); const service = TestBed.inject(ChatroomService); - const chatroom = await lastValueFrom(service.getChatroom("2", "5c73531c-6fc4-426c-adcb-afc5c140a0f7")); + const chatroom = await service.setChatroom("2", "5c73531c-6fc4-426c-adcb-afc5c140a0f7"); expect(chatroom).toEqual(EXAMPLE_CHATROOM); + expect(service.getChatRoom()).toEqual(EXAMPLE_CHATROOM); }); }); @@ -73,7 +73,9 @@ describe('Pact between the ChatroomService and the backend', () => { 'X-Shard': 2, }); }) - .willRespondWith(404) + .willRespondWith(404, (builder) => { + builder.jsonBody({ error: Matchers.like("404 not found") }) + }) .executeTest(async (mockserver) => { await TestBed.configureTestingModule({ providers: [ @@ -85,19 +87,9 @@ describe('Pact between the ChatroomService and the backend', () => { const service = TestBed.inject(ChatroomService); - return new Promise((resolve, reject) => { - - service - .getChatroom("2", "7f59ec77-832e-4a17-8d22-55ef46242c17") - .subscribe({ - next: (chatrooms) => { - reject('a status other than 200 should have rejected the observable') - }, - error: (error) => { - resolve(); - }, - }); - }); + const chatroom$ = service.setChatroom("2", "7f59ec77-832e-4a17-8d22-55ef46242c17"); + await expect(chatroom$).rejects.toHaveProperty('status', 404); + expect(service.getChatRoom()).toBeUndefined(); }); }); }); diff --git a/src/app/chatroom/chatroom.service.ts b/src/app/chatroom/chatroom.service.ts index a526e86c..f18f8e72 100644 --- a/src/app/chatroom/chatroom.service.ts +++ b/src/app/chatroom/chatroom.service.ts @@ -1,7 +1,7 @@ import { inject, Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { EventSourceMessage, fetchEventSource } from '@microsoft/fetch-event-source'; -import { Observable, Subscriber } from 'rxjs'; +import { catchError, firstValueFrom, Observable, Subscriber, tap, throwError } from 'rxjs'; import { Chatroom, Message } from './index'; import { APP_PROPS } from '../app.tokens'; @@ -17,6 +17,8 @@ export class ChatroomService { private http = inject(HttpClient); private backendUri: string; + private chatroom?: Chatroom; + private channel: Subscriber = new Subscriber(); private uri: string = "CLOSED"; private canceled: boolean = false; @@ -30,10 +32,24 @@ export class ChatroomService { return this.http.get(this.backendUri + 'list'); } - getChatroom(shard: string, id: string): Observable { - return this.http.get( - this.backendUri + id, - { headers: { 'X-Shard': shard }}); + setChatroom(shard: string, id: string): Promise { + return firstValueFrom(this + .http + .get(this.backendUri + id, { headers: { 'X-Shard': shard }}) + .pipe( + tap((chatroom: Chatroom) => { + console.log('loaded new current chatroom: ' + chatroom.name); + this.chatroom = chatroom; + }), + catchError((error) => { + console.error('error while loading the new chatroom:', error); + this.chatroom = undefined; + return throwError(() => error); + }))); + } + + getChatRoom(): Chatroom | undefined { + return this.chatroom } listen(shard: string, id: string): Observable {