Skip to content

Commit c806e2c

Browse files
authored
Merge pull request #525 from GetStream/zd-44581
feat: always reselect active channel after reconnect
2 parents 619cd55 + 4b95517 commit c806e2c

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,23 +2128,22 @@ describe('ChannelService', () => {
21282128
);
21292129
});
21302130

2131-
it('should deselect active channel if active channel is not present after state reconnect', fakeAsync(async () => {
2131+
it('should relaod active channel if active channel is not present after state reconnect', fakeAsync(async () => {
21322132
await init();
21332133
let activeChannel!: Channel<DefaultStreamChatGenerics>;
21342134
service.activeChannel$.subscribe((c) => (activeChannel = c!));
21352135
let channels!: Channel<DefaultStreamChatGenerics>[];
21362136
service.channels$.subscribe((c) => (channels = c!));
21372137
channels = channels.filter((c) => c.id !== activeChannel.id);
2138-
const spy = jasmine.createSpy();
2139-
service.activeChannel$.subscribe(spy);
2140-
spy.calls.reset();
2138+
spyOn(activeChannel, 'watch');
21412139
mockChatClient.queryChannels.and.resolveTo(channels);
2142-
spyOn(service, 'deselectActiveChannel').and.callThrough();
21432140
events$.next({ eventType: 'connection.recovered' } as ClientEvent);
21442141
tick();
2142+
const spy = jasmine.createSpy();
2143+
service.activeChannel$.subscribe(spy);
21452144

2146-
expect(spy).toHaveBeenCalledWith(undefined);
2147-
expect(service.deselectActiveChannel).toHaveBeenCalledWith();
2145+
expect(spy).toHaveBeenCalledWith(activeChannel);
2146+
expect(activeChannel.watch).toHaveBeenCalledWith();
21482147
}));
21492148

21502149
it(`shouldn't deselect active channel if active channel is present after state reconnect`, fakeAsync(async () => {

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,8 +1562,25 @@ export class ChannelService<
15621562
(existingChannel) => existingChannel.cid === channel.cid
15631563
)
15641564
);
1565-
this.channelsSubject.next([...prevChannels, ...filteredChannels]);
15661565
let currentActiveChannel = this.activeChannelSubject.getValue();
1566+
let isCurrentActiveChannelDeselected = false;
1567+
const nextChannels = [...prevChannels, ...filteredChannels];
1568+
if (
1569+
recoverState &&
1570+
currentActiveChannel &&
1571+
!filteredChannels.find((c) => c.cid === currentActiveChannel?.cid)
1572+
) {
1573+
try {
1574+
await currentActiveChannel.watch();
1575+
nextChannels.unshift(currentActiveChannel);
1576+
} catch (e) {
1577+
isCurrentActiveChannelDeselected = true;
1578+
}
1579+
}
1580+
this.channelsSubject.next(nextChannels);
1581+
if (isCurrentActiveChannelDeselected) {
1582+
this.deselectActiveChannel();
1583+
}
15671584
if (
15681585
filteredChannels.length > 0 &&
15691586
!currentActiveChannel &&
@@ -1572,12 +1589,6 @@ export class ChannelService<
15721589
this.setAsActiveChannel(filteredChannels[0]);
15731590
currentActiveChannel = this.activeChannelSubject.getValue();
15741591
}
1575-
if (
1576-
recoverState &&
1577-
!filteredChannels.find((c) => c.cid === currentActiveChannel?.cid)
1578-
) {
1579-
this.deselectActiveChannel();
1580-
}
15811592
this.hasMoreChannelsSubject.next(channels.length >= this.options!.limit!);
15821593
this.channelQueryStateSubject.next({ state: 'success' });
15831594
if (

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export class ChatClientService<
102102
clientOptions?: StreamChatOptions
103103
): ConnectAPIResponse<T> {
104104
this.chatClient = StreamChat.getInstance<T>(apiKey, clientOptions);
105+
this.chatClient.recoverStateOnReconnect = false;
105106
this.chatClient.devToken;
106107
let result;
107108
await this.ngZone.runOutsideAngular(async () => {

0 commit comments

Comments
 (0)