Skip to content

Commit e4cd8fd

Browse files
committed
fix: Duplicated channels after two notification events and not up to date channel state
1 parent bbd0464 commit e4cd8fd

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,33 @@ describe('ChannelService', () => {
766766
expect(channel.on).toHaveBeenCalledWith(jasmine.any(Function));
767767
}));
768768

769+
it(`shouldn't add channels duplicated if two notification event is received for the same channel`, fakeAsync(async () => {
770+
await init();
771+
const channel = generateMockChannels()[0];
772+
channel.cid = 'channel';
773+
channel.id = 'channel';
774+
channel.type = 'messaging';
775+
mockChatClient.channel.and.returnValue(channel);
776+
spyOn(channel, 'watch').and.callThrough();
777+
spyOn(channel, 'on').and.callThrough();
778+
const spy = jasmine.createSpy();
779+
service.channels$.subscribe(spy);
780+
events$.next({
781+
eventType: 'notification.added_to_channel',
782+
event: { channel: channel } as any as Event<DefaultStreamChatGenerics>,
783+
});
784+
events$.next({
785+
eventType: 'notification.message_new',
786+
event: { channel: channel } as any as Event<DefaultStreamChatGenerics>,
787+
});
788+
tick();
789+
790+
const channels = spy.calls.mostRecent().args[0] as Channel[];
791+
792+
expect(channels.filter((c) => c.cid === channel.cid).length).toBe(1);
793+
expect(channel.on).toHaveBeenCalledOnceWith(jasmine.any(Function));
794+
}));
795+
769796
it('should remove channel form the list if user is removed from channel', async () => {
770797
await init();
771798
let channel!: Channel<DefaultStreamChatGenerics>;

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -876,21 +876,28 @@ export class ChannelService<
876876
}
877877
}
878878

879-
private addChannelsFromNotification(channelResponses: ChannelResponse<T>[]) {
880-
const newChannels: Channel<T>[] = [];
879+
private async addChannelsFromNotification(
880+
channelResponses: ChannelResponse<T>[]
881+
) {
882+
let newChannels: Channel<T>[] = [];
883+
const watchRequests: Promise<any>[] = [];
881884
channelResponses.forEach((channelResponse) => {
882885
const channel = this.chatClientService.chatClient.channel(
883886
channelResponse.type,
884887
channelResponse.id
885888
);
886-
void channel.watch();
887-
this.watchForChannelEvents(channel);
889+
watchRequests.push(channel.watch());
888890
newChannels.push(channel);
889891
});
890-
this.channelsSubject.next([
891-
...newChannels,
892-
...(this.channelsSubject.getValue() || []),
893-
]);
892+
await Promise.all(watchRequests);
893+
const currentChannels = this.channelsSubject.getValue() || [];
894+
newChannels = newChannels.filter(
895+
(newChannel) => !currentChannels.find((c) => c.cid === newChannel.cid)
896+
);
897+
if (newChannels.length > 0) {
898+
newChannels.forEach((c) => this.watchForChannelEvents(c));
899+
this.channelsSubject.next([...newChannels, ...currentChannels]);
900+
}
894901
}
895902

896903
private removeChannelsFromChannelList(cids: string[]) {

0 commit comments

Comments
 (0)