-Subproject commit 8c4e57bb0973a60deb4d16b4e3ff13ba8a45989e
+Subproject commit d7ba11ea3b6c4eb24bf8d89a910ffbc90d26b441
}
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({
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';
provider: 'ChatBackendController',
spec: SpecificationVersion.SPECIFICATION_VERSION_V4,
dir: 'pacts',
- logLevel: 'debug',
+ logLevel: 'info',
});
});
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);
});
});
'X-Shard': 2,
});
})
- .willRespondWith(404)
+ .willRespondWith(404, (builder) => {
+ builder.jsonBody({ error: Matchers.like("404 not found") })
+ })
.executeTest(async (mockserver) => {
await TestBed.configureTestingModule({
providers: [
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();
});
});
});
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';
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;
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> {