Skip to content

Commit 19dc318

Browse files
committed
fix: avoid selecting the same channel twice
1 parent 73bb425 commit 19dc318

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,7 @@ describe('ChannelService', () => {
15241524
it('should query members, less than 100 members', async () => {
15251525
await init();
15261526
const channel = generateMockChannels(1)[0];
1527+
channel.cid = 'new-channel';
15271528
channel.state.members = {
15281529
jack: { user: { id: 'jack', name: 'Jack' } },
15291530
john: { user: { id: 'john' } },
@@ -1545,6 +1546,7 @@ describe('ChannelService', () => {
15451546
it('should query members, more than 100 members', async () => {
15461547
await init();
15471548
const channel = generateMockChannels(1)[0];
1549+
channel.cid = 'new-channel';
15481550
spyOn(channel, 'queryMembers').and.callThrough();
15491551
const users = Array.from({ length: 101 }, (_, i) => ({ id: `${i}` }));
15501552
channel.state.members = {} as any as Record<
@@ -1964,4 +1966,17 @@ describe('ChannelService', () => {
19641966
newChannel
19651967
);
19661968
});
1969+
1970+
it('should do nothing if same channel is selected twice', () => {
1971+
const activeChannel = generateMockChannels()[0];
1972+
1973+
service.setAsActiveChannel(activeChannel);
1974+
1975+
const spy = jasmine.createSpy();
1976+
service.activeChannel$.subscribe(spy);
1977+
spy.calls.reset();
1978+
service.setAsActiveChannel(activeChannel);
1979+
1980+
expect(spy).not.toHaveBeenCalled();
1981+
});
19671982
});

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,9 @@ export class ChannelService<
405405
*/
406406
setAsActiveChannel(channel: Channel<T>) {
407407
const prevActiveChannel = this.activeChannelSubject.getValue();
408+
if (prevActiveChannel?.cid === channel.cid) {
409+
return;
410+
}
408411
this.stopWatchForActiveChannelEvents(prevActiveChannel);
409412
this.watchForActiveChannelEvents(channel);
410413
this.addChannel(channel);

0 commit comments

Comments
 (0)