Skip to content

Commit f59c09a

Browse files
authored
Merge pull request #276 from GetStream/fix-reconnect-error
fix: Multiple state recovery requests #273
2 parents 954ea86 + c3f6c05 commit f59c09a

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,4 +1544,21 @@ describe('ChannelService', () => {
15441544

15451545
expect(service.reset).toHaveBeenCalledWith();
15461546
});
1547+
1548+
it(`shouldn't do duplicate state reset after connection recovered`, async () => {
1549+
await init();
1550+
spyOn(service, 'init');
1551+
spyOn(service, 'reset');
1552+
events$.next({ eventType: 'connection.recovered' } as ClientEvent);
1553+
events$.next({ eventType: 'connection.recovered' } as ClientEvent);
1554+
1555+
expect(service.init).toHaveBeenCalledOnceWith(
1556+
service['filters']!,
1557+
service['sort'],
1558+
service['options'],
1559+
service['shouldSetActiveChannel']
1560+
);
1561+
1562+
expect(service.reset).toHaveBeenCalledOnceWith();
1563+
});
15471564
});

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ export class ChannelService<
261261
private _shouldMarkActiveChannelAsRead = true;
262262
private shouldSetActiveChannel: boolean | undefined;
263263
private clientEventsSubscription: Subscription | undefined;
264+
private isStateRecoveryInProgress = false;
264265

265266
private channelListSetter = (
266267
channels: (Channel<T> | ChannelResponse<T>)[]
@@ -797,14 +798,23 @@ export class ChannelService<
797798
private handleNotification(clientEvent: ClientEvent<T>) {
798799
switch (clientEvent.eventType) {
799800
case 'connection.recovered': {
800-
this.ngZone.run(() => {
801+
void this.ngZone.run(async () => {
802+
if (this.isStateRecoveryInProgress) {
803+
return;
804+
}
805+
this.isStateRecoveryInProgress = true;
801806
this.reset();
802-
void this.init(
803-
this.filters!,
804-
this.sort,
805-
this.options,
806-
this.shouldSetActiveChannel
807-
);
807+
try {
808+
await this.init(
809+
this.filters!,
810+
this.sort,
811+
this.options,
812+
this.shouldSetActiveChannel
813+
);
814+
this.isStateRecoveryInProgress = false;
815+
} catch {
816+
this.isStateRecoveryInProgress = false;
817+
}
808818
});
809819
break;
810820
}

0 commit comments

Comments
 (0)