Skip to content

Commit e2e29c9

Browse files
authored
Merge pull request #385 from GetStream/fix-unsubsruce-after-channel-remove
fix: Stop watching channels removed from the channel list #383
2 parents 816062e + 506a9a3 commit e2e29c9

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ describe('ChannelService', () => {
3737
userID: string;
3838
pinMessage: jasmine.Spy;
3939
unpinMessage: jasmine.Spy;
40+
activeChannels: { [key: string]: Channel<DefaultStreamChatGenerics> };
4041
};
4142
let events$: Subject<ClientEvent>;
4243
let connectionState$: Subject<'online' | 'offline'>;
@@ -63,6 +64,7 @@ describe('ChannelService', () => {
6364
userID: user.id,
6465
pinMessage: jasmine.createSpy(),
6566
unpinMessage: jasmine.createSpy(),
67+
activeChannels: {},
6668
};
6769
events$ = new Subject();
6870
TestBed.configureTestingModule({
@@ -586,6 +588,8 @@ describe('ChannelService', () => {
586588
service.activeChannel$.pipe(first()).subscribe((c) => (channel = c!));
587589
const spy = jasmine.createSpy();
588590
service.channels$.subscribe(spy);
591+
mockChatClient.activeChannels[channel.cid] = channel;
592+
spyOn(channel, 'stopWatching');
589593
(channel as MockChannel).handleEvent('channel.hidden', {
590594
type: 'channel.hidden',
591595
channel,
@@ -594,6 +598,7 @@ describe('ChannelService', () => {
594598
let channels = spy.calls.mostRecent().args[0] as Channel[];
595599

596600
expect(channels.find((c) => c.cid === channel.cid)).toBeUndefined();
601+
expect(channel.stopWatching).toHaveBeenCalledWith();
597602

598603
(channel as MockChannel).handleEvent('channel.hidden', {
599604
type: 'channel.visible',
@@ -650,6 +655,8 @@ describe('ChannelService', () => {
650655
service.activeChannel$.pipe(first()).subscribe((c) => (channel = c!));
651656
const spy = jasmine.createSpy();
652657
service.channels$.subscribe(spy);
658+
mockChatClient.activeChannels[channel.cid] = channel;
659+
spyOn(channel, 'stopWatching');
653660
(channel as MockChannel).handleEvent('channel.deleted', {
654661
type: 'channel.deleted',
655662
channel,
@@ -658,6 +665,7 @@ describe('ChannelService', () => {
658665
const channels = spy.calls.mostRecent().args[0] as Channel[];
659666

660667
expect(channels.find((c) => c.cid === channel.cid)).toBeUndefined();
668+
expect(channel.stopWatching).toHaveBeenCalledWith();
661669
});
662670

663671
it('should call #customChannelDeletedHandler, if channel is deleted and handler is provided', async () => {
@@ -894,6 +902,8 @@ describe('ChannelService', () => {
894902
.subscribe((channels) => (channel = channels![1]));
895903
const spy = jasmine.createSpy();
896904
service.channels$.subscribe(spy);
905+
mockChatClient.activeChannels[channel.cid] = channel;
906+
spyOn(channel, 'stopWatching');
897907
spyOn(service, 'setAsActiveChannel');
898908
events$.next({
899909
eventType: 'notification.removed_from_channel',
@@ -904,6 +914,7 @@ describe('ChannelService', () => {
904914

905915
expect(channels.find((c) => c.cid === channel.cid)).toBeUndefined();
906916
expect(service.setAsActiveChannel).not.toHaveBeenCalled();
917+
expect(channel.stopWatching).toHaveBeenCalledWith();
907918
});
908919

909920
it('should remove channel form the list if user is removed from channel, and emit new active channel', async () => {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,12 @@ export class ChannelService<
10551055

10561056
private removeChannelsFromChannelList(cids: string[]) {
10571057
const channels = this.channels.filter((c) => !cids.includes(c.cid || ''));
1058+
cids.forEach(
1059+
(cid) =>
1060+
void this.chatClientService.chatClient.activeChannels[
1061+
cid
1062+
]?.stopWatching()
1063+
);
10581064
if (channels.length < this.channels.length) {
10591065
this.channelsSubject.next(channels);
10601066
if (cids.includes(this.activeChannelSubject.getValue()?.cid || '')) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export const generateMockChannels = (length = 25) => {
7979
};
8080
},
8181
watch: () => {},
82+
stopWatching: () => {},
8283
sendMessage: () => {},
8384
sendImage: () => {},
8485
sendFile: () => {},

0 commit comments

Comments
 (0)