Skip to content

Commit 68bbd3d

Browse files
committed
fix: Missing unsubscriptions on ChannelService reset
1 parent 4d043c7 commit 68bbd3d

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { fakeAsync, TestBed, tick } from '@angular/core/testing';
22
import { Subject } from 'rxjs';
3-
import { first } from 'rxjs/operators';
3+
import { first, take } from 'rxjs/operators';
44
import {
55
Channel,
66
ChannelMemberResponse,
77
ChannelOptions,
8+
ChannelResponse,
89
ChannelSort,
910
Event,
1011
SendMessageAPIResponse,
@@ -184,10 +185,24 @@ describe('ChannelService', () => {
184185
expect(activeChannelSpy).toHaveBeenCalledWith(undefined);
185186
expect(messageToQuoteSpy).toHaveBeenCalledWith(undefined);
186187
expect(latestMessagesSpy).toHaveBeenCalledWith({});
188+
189+
channelsSpy.calls.reset();
190+
events$.next({
191+
eventType: 'message.new',
192+
event: {
193+
channel: {
194+
id: 'channel',
195+
} as ChannelResponse<DefaultStreamChatGenerics>,
196+
} as Event<DefaultStreamChatGenerics>,
197+
});
198+
199+
expect(channelsSpy).not.toHaveBeenCalled();
187200
});
188201

189202
it('should deselect active channel', async () => {
190203
await init();
204+
let activeChannel!: Channel<DefaultStreamChatGenerics>;
205+
service.activeChannel$.pipe(take(1)).subscribe((c) => (activeChannel = c!));
191206
const messagesSpy = jasmine.createSpy();
192207
service.activeChannelMessages$.subscribe(messagesSpy);
193208
const activeChannelSpy = jasmine.createSpy();
@@ -206,6 +221,11 @@ describe('ChannelService', () => {
206221
expect(activeChannelSpy).toHaveBeenCalledWith(undefined);
207222
expect(messageToQuoteSpy).toHaveBeenCalledWith(undefined);
208223
expect(latestMessagesSpy).toHaveBeenCalledWith({});
224+
225+
messagesSpy.calls.reset();
226+
(activeChannel as MockChannel).handleEvent('message.new', mockMessage());
227+
228+
expect(messagesSpy).not.toHaveBeenCalled();
209229
});
210230

211231
it('should tell if user #hasMoreChannels$', async () => {
@@ -1521,6 +1541,7 @@ describe('ChannelService', () => {
15211541
service['options'],
15221542
service['shouldSetActiveChannel']
15231543
);
1544+
15241545
expect(service.reset).toHaveBeenCalledWith();
15251546
});
15261547
});

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
combineLatest,
55
Observable,
66
ReplaySubject,
7+
Subscription,
78
} from 'rxjs';
89
import { filter, first, map, shareReplay } from 'rxjs/operators';
910
import {
@@ -259,6 +260,7 @@ export class ChannelService<
259260
);
260261
private _shouldMarkActiveChannelAsRead = true;
261262
private shouldSetActiveChannel: boolean | undefined;
263+
private clientEventsSubscription: Subscription | undefined;
262264

263265
private channelListSetter = (
264266
channels: (Channel<T> | ChannelResponse<T>)[]
@@ -393,6 +395,7 @@ export class ChannelService<
393395
if (!activeChannel) {
394396
return;
395397
}
398+
this.stopWatchForActiveChannelEvents(activeChannel);
396399
this.activeChannelMessagesSubject.next([]);
397400
this.activeChannelSubject.next(undefined);
398401
this.activeParentMessageIdSubject.next(undefined);
@@ -489,7 +492,7 @@ export class ChannelService<
489492
this.sort = sort || { last_message_at: -1, updated_at: -1 };
490493
this.shouldSetActiveChannel = shouldSetActiveChannel;
491494
const result = await this.queryChannels(this.shouldSetActiveChannel);
492-
this.chatClientService.events$.subscribe(
495+
this.clientEventsSubscription = this.chatClientService.events$.subscribe(
493496
(notification) => void this.handleNotification(notification)
494497
);
495498
return result;
@@ -501,6 +504,7 @@ export class ChannelService<
501504
reset() {
502505
this.deselectActiveChannel();
503506
this.channelsSubject.next(undefined);
507+
this.clientEventsSubscription?.unsubscribe();
504508
}
505509

506510
/**
@@ -795,7 +799,7 @@ export class ChannelService<
795799
case 'connection.recovered': {
796800
this.ngZone.run(() => {
797801
this.reset();
798-
this.init(
802+
void this.init(
799803
this.filters!,
800804
this.sort,
801805
this.options,

0 commit comments

Comments
 (0)