Skip to content

Commit d0d1314

Browse files
authored
Merge pull request #288 from GetStream/return-response-from-init
Return response from init
2 parents 3309c4e + f3ee816 commit d0d1314

File tree

5 files changed

+50
-28
lines changed

5 files changed

+50
-28
lines changed

projects/stream-chat-angular/src/lib/avatar/avatar.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export class AvatarComponent {
6060
const otherMembers = Object.values(
6161
this.channel?.state?.members || {}
6262
).filter(
63-
(m) => m.user_id !== this.chatClientService.chatClient.user!.id
63+
(m) => m.user_id !== this.chatClientService.chatClient.user?.id
6464
);
6565
if (otherMembers.length === 1) {
6666
result =

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,21 @@ describe('ChatClientService', () => {
3535

3636
it('should disconnect user', async () => {
3737
const pendingInvitesSpy = jasmine.createSpy();
38+
const eventsSpy = jasmine.createSpy();
39+
service.events$.subscribe(eventsSpy);
3840
service.pendingInvites$.subscribe(pendingInvitesSpy);
3941
pendingInvitesSpy.calls.reset();
42+
eventsSpy.calls.reset();
4043
await service.disconnectUser();
44+
const event = {
45+
id: 'mockevent',
46+
type: 'notification.added_to_channel',
47+
} as any as Event;
48+
mockChatClient.handleEvent(event.type, event);
4149

4250
expect(mockChatClient.disconnectUser).toHaveBeenCalledWith();
4351
expect(pendingInvitesSpy).toHaveBeenCalledWith([]);
52+
expect(eventsSpy).not.toHaveBeenCalled();
4453
});
4554

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

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

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
Channel,
55
ChannelFilters,
66
ChannelResponse,
7+
ConnectAPIResponse,
78
OwnUserResponse,
89
UserFilters,
910
UserResponse,
@@ -60,6 +61,7 @@ export class ChatClientService<
6061
private pendingInvitesSubject = new BehaviorSubject<
6162
(ChannelResponse<T> | Channel<T>)[]
6263
>([]);
64+
private subscriptions: { unsubscribe: () => void }[] = [];
6365

6466
constructor(
6567
private ngZone: NgZone,
@@ -81,12 +83,13 @@ export class ChatClientService<
8183
apiKey: string,
8284
userOrId: string | OwnUserResponse<T> | UserResponse<T>,
8385
userTokenOrProvider: TokenOrProvider
84-
) {
86+
): ConnectAPIResponse<T> {
8587
this.chatClient = StreamChat.getInstance<T>(apiKey);
8688
this.chatClient.devToken;
89+
let result;
8790
await this.ngZone.runOutsideAngular(async () => {
8891
const user = typeof userOrId === 'string' ? { id: userOrId } : userOrId;
89-
await this.chatClient.connectUser(user, userTokenOrProvider);
92+
result = await this.chatClient.connectUser(user, userTokenOrProvider);
9093
this.chatClient.setUserAgent(
9194
`stream-chat-angular-${version}-${this.chatClient.getUserAgent()}`
9295
);
@@ -98,30 +101,35 @@ export class ChatClientService<
98101
);
99102
this.pendingInvitesSubject.next(channels);
100103
this.appSettingsSubject.next(undefined);
101-
this.chatClient.on((e) => {
102-
this.updatePendingInvites(e);
103-
this.notificationSubject.next({
104-
eventType: e.type,
105-
event: e,
106-
});
107-
});
104+
this.subscriptions.push(
105+
this.chatClient.on((e) => {
106+
this.updatePendingInvites(e);
107+
this.notificationSubject.next({
108+
eventType: e.type,
109+
event: e,
110+
});
111+
})
112+
);
108113
let removeNotification: undefined | Function;
109-
this.chatClient.on('connection.changed', (e) => {
110-
this.ngZone.run(() => {
111-
const isOnline = e.online;
112-
if (isOnline) {
113-
if (removeNotification) {
114-
removeNotification();
114+
this.subscriptions.push(
115+
this.chatClient.on('connection.changed', (e) => {
116+
this.ngZone.run(() => {
117+
const isOnline = e.online;
118+
if (isOnline) {
119+
if (removeNotification) {
120+
removeNotification();
121+
}
122+
} else {
123+
removeNotification =
124+
this.notificationService.addPermanentNotification(
125+
'streamChat.Connection failure, reconnecting now...'
126+
);
115127
}
116-
} else {
117-
removeNotification =
118-
this.notificationService.addPermanentNotification(
119-
'streamChat.Connection failure, reconnecting now...'
120-
);
121-
}
122-
this.connectionStateSubject.next(isOnline ? 'online' : 'offline');
123-
});
124-
});
128+
this.connectionStateSubject.next(isOnline ? 'online' : 'offline');
129+
});
130+
})
131+
);
132+
return result;
125133
}
126134

127135
/**
@@ -130,6 +138,7 @@ export class ChatClientService<
130138
async disconnectUser() {
131139
this.pendingInvitesSubject.next([]);
132140
await this.chatClient.disconnectUser();
141+
this.subscriptions.forEach((s) => s.unsubscribe());
133142
}
134143

135144
/**

projects/stream-chat-angular/src/lib/get-channel-display-text.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const getChannelDisplayText = (
1212
if (channel.state.members && Object.keys(channel.state.members).length > 0) {
1313
const members = Object.values(channel.state.members)
1414
.map((m) => m.user || { id: m.user_id! })
15-
.filter((m) => m.id !== currentUser.id);
15+
.filter((m) => m.id !== currentUser?.id);
1616
return listUsers(members);
1717
}
1818
return channel.id;

projects/stream-chat-angular/src/lib/mocks/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ export type MockStreamChatClient = {
275275
appSettings$: Subject<AppSettings>;
276276
user: UserResponse;
277277
connectUser: jasmine.Spy;
278-
on: (name: EventTypes, handler: () => {}) => void;
278+
on: (name: EventTypes, handler: () => {}) => { unsubscribe: () => void };
279279
handleEvent: (name: EventTypes, event: Event) => void;
280280
flagMessage: jasmine.Spy;
281281
setUserAgent: jasmine.Spy;
@@ -319,11 +319,15 @@ export const mockStreamChatClient = (): MockStreamChatClient => {
319319
} else {
320320
eventHandlers['all'] = name;
321321
}
322+
return {
323+
unsubscribe: () =>
324+
delete eventHandlers[typeof name === 'string' ? name : 'all'],
325+
};
322326
};
323327
const handleEvent = (name: EventTypes, event: Event) => {
324328
if (eventHandlers[name as string]) {
325329
eventHandlers[name as string](event);
326-
} else {
330+
} else if (eventHandlers['all']) {
327331
eventHandlers['all']({ ...event, type: name });
328332
}
329333
};

0 commit comments

Comments
 (0)