Skip to content

Commit b719205

Browse files
authored
Merge pull request #340 from GetStream/textarea-disable-example
feat: Define user$ Observable #338
2 parents b1ac574 + 730ea6e commit b719205

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

.github/workflows/workflow.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
runs-on: ubuntu-latest
1414
strategy:
1515
matrix:
16-
node-version: [16.x]
16+
node-version: [14.x]
1717

1818
steps:
1919
- uses: actions/checkout@v2

projects/stream-chat-angular/src/lib/chat-client.service.spec.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { TestBed } from '@angular/core/testing';
22
import { Event, OwnUserResponse, StreamChat } from 'stream-chat';
33
import { version } from '../assets/version';
44
import { ChatClientService } from './chat-client.service';
5-
import { mockStreamChatClient, MockStreamChatClient } from './mocks';
5+
import {
6+
mockCurrentUser,
7+
mockStreamChatClient,
8+
MockStreamChatClient,
9+
} from './mocks';
610
import { NotificationService } from './notification.service';
711
import { DefaultStreamChatGenerics } from './types';
812

@@ -29,8 +33,11 @@ describe('ChatClientService', () => {
2933
expect(StreamChat.getInstance).toHaveBeenCalledWith(apiKey);
3034
const spy = jasmine.createSpy();
3135
service.appSettings$.subscribe(spy);
36+
const userSpy = jasmine.createSpy();
37+
service.user$.subscribe(userSpy);
3238

3339
expect(spy).toHaveBeenCalledWith(undefined);
40+
expect(userSpy).toHaveBeenCalledWith(mockCurrentUser());
3441
});
3542

3643
it('should disconnect user', async () => {
@@ -40,6 +47,9 @@ describe('ChatClientService', () => {
4047
service.pendingInvites$.subscribe(pendingInvitesSpy);
4148
pendingInvitesSpy.calls.reset();
4249
eventsSpy.calls.reset();
50+
const userSpy = jasmine.createSpy();
51+
service.user$.subscribe(userSpy);
52+
userSpy.calls.reset();
4353
await service.disconnectUser();
4454
const event = {
4555
id: 'mockevent',
@@ -50,6 +60,7 @@ describe('ChatClientService', () => {
5060
expect(mockChatClient.disconnectUser).toHaveBeenCalledWith();
5161
expect(pendingInvitesSpy).toHaveBeenCalledWith([]);
5262
expect(eventsSpy).not.toHaveBeenCalled();
63+
expect(userSpy).toHaveBeenCalledWith(undefined);
5364
});
5465

5566
it('should init with user meta data', async () => {

projects/stream-chat-angular/src/lib/chat-client.service.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ export class ChatClientService<
5353
* Emits the list of pending invites of the user. It emits every pending invitation during initialization and then extends the list when a new invite is received. More information can be found in the [channel invitations](../code-examples/channel-invites.mdx) guide.
5454
*/
5555
pendingInvites$: Observable<(ChannelResponse<T> | Channel<T>)[]>;
56+
/**
57+
* Emits the current chat user
58+
*/
59+
user$: Observable<UserResponse<T> | undefined>;
5660
private notificationSubject = new ReplaySubject<ClientEvent<T>>(1);
5761
private connectionStateSubject = new ReplaySubject<'offline' | 'online'>(1);
5862
private appSettingsSubject = new BehaviorSubject<AppSettings | undefined>(
@@ -61,6 +65,7 @@ export class ChatClientService<
6165
private pendingInvitesSubject = new BehaviorSubject<
6266
(ChannelResponse<T> | Channel<T>)[]
6367
>([]);
68+
private userSubject = new ReplaySubject<UserResponse<T> | undefined>(1);
6469
private subscriptions: { unsubscribe: () => void }[] = [];
6570

6671
constructor(
@@ -71,6 +76,7 @@ export class ChatClientService<
7176
this.connectionState$ = this.connectionStateSubject.asObservable();
7277
this.appSettings$ = this.appSettingsSubject.asObservable();
7378
this.pendingInvites$ = this.pendingInvitesSubject.asObservable();
79+
this.user$ = this.userSubject.asObservable();
7480
}
7581

7682
/**
@@ -90,6 +96,7 @@ export class ChatClientService<
9096
await this.ngZone.runOutsideAngular(async () => {
9197
const user = typeof userOrId === 'string' ? { id: userOrId } : userOrId;
9298
result = await this.chatClient.connectUser(user, userTokenOrProvider);
99+
this.userSubject.next(this.chatClient.user);
93100
this.chatClient.setUserAgent(
94101
`stream-chat-angular-${version}-${this.chatClient.getUserAgent()}`
95102
);
@@ -138,6 +145,7 @@ export class ChatClientService<
138145
async disconnectUser() {
139146
this.pendingInvitesSubject.next([]);
140147
await this.chatClient.disconnectUser();
148+
this.userSubject.next(undefined);
141149
this.subscriptions.forEach((s) => s.unsubscribe());
142150
}
143151

0 commit comments

Comments
 (0)