]> juplo.de Git - demos/kafka/chat/commitdiff
feat: `ChatroomService` remembers the current `Chatroom`
authorKai Moritz <kai@juplo.de>
Sun, 12 Oct 2025 11:18:19 +0000 (13:18 +0200)
committerKai Moritz <kai@juplo.de>
Sun, 12 Oct 2025 17:28:09 +0000 (19:28 +0200)
pacts
src/app/chatroom/chatroom.component.ts
src/app/chatroom/chatroom.service.pact.spec.ts
src/app/chatroom/chatroom.service.ts

diff --git a/pacts b/pacts
index 8c4e57bb0973a60deb4d16b4e3ff13ba8a45989e..d7ba11ea3b6c4eb24bf8d89a910ffbc90d26b441 160000 (submodule)
--- a/pacts
+++ b/pacts
@@ -1 +1 @@
-Subproject commit 8c4e57bb0973a60deb4d16b4e3ff13ba8a45989e
+Subproject commit d7ba11ea3b6c4eb24bf8d89a910ffbc90d26b441
index 40839b0601f450598f66d0e8e5c634c84c705a0a..32dd6a5fead7f4d55ff10ae2419799ab25c4f5da 100644 (file)
@@ -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({
index 2d0b156623f0c58644569093f09117b04698bd7b..b932b498ee46037188fd3f3f253708ff6b1a7c85 100644 (file)
@@ -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<void>((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();
       });
   });
 });
index a526e86cce8fe4284417bf1bce77051b28d22134..f18f8e72999e7e36fdb397eaf67eadc9f228efa0 100644 (file)
@@ -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<Message> = new Subscriber<Message>();
   private uri: string = "CLOSED";
   private canceled: boolean = false;
@@ -30,10 +32,24 @@ export class ChatroomService {
     return this.http.get<Chatroom[]>(this.backendUri + 'list');
   }
 
-  getChatroom(shard: string, id: string): Observable<Chatroom> {
-    return this.http.get<Chatroom>(
-      this.backendUri + id,
-      { headers: { 'X-Shard': shard }});
+  setChatroom(shard: string, id: string): Promise<Chatroom> {
+    return firstValueFrom<Chatroom>(this
+      .http
+      .get<Chatroom>(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<Message> {