@@ -273,11 +273,8 @@ export class ChannelService<
273273 const deletedChannels = currentChannels . filter (
274274 ( c ) => ! channels ?. find ( ( channel ) => channel . cid === c . cid )
275275 ) ;
276- this . addChannelsFromNotification ( newChannels as ChannelResponse < T > [ ] ) ;
276+ void this . addChannelsFromNotification ( newChannels as ChannelResponse < T > [ ] ) ;
277277 this . removeChannelsFromChannelList ( deletedChannels . map ( ( c ) => c . cid ) ) ;
278- if ( ! newChannels . length && ! deletedChannels . length ) {
279- this . channelsSubject . next ( channels as Channel < T > [ ] ) ;
280- }
281278 } ;
282279
283280 private messageListSetter = ( messages : StreamMessage < T > [ ] ) => {
@@ -866,31 +863,38 @@ export class ChannelService<
866863
867864 private handleNewMessageNotification ( clientEvent : ClientEvent < T > ) {
868865 if ( clientEvent . event . channel ) {
869- this . addChannelsFromNotification ( [ clientEvent . event . channel ] ) ;
866+ void this . addChannelsFromNotification ( [ clientEvent . event . channel ] ) ;
870867 }
871868 }
872869
873870 private handleAddedToChannelNotification ( clientEvent : ClientEvent < T > ) {
874871 if ( clientEvent . event . channel ) {
875- this . addChannelsFromNotification ( [ clientEvent . event . channel ] ) ;
872+ void this . addChannelsFromNotification ( [ clientEvent . event . channel ] ) ;
876873 }
877874 }
878875
879- private addChannelsFromNotification ( channelResponses : ChannelResponse < T > [ ] ) {
880- const newChannels : Channel < T > [ ] = [ ] ;
876+ private async addChannelsFromNotification (
877+ channelResponses : ChannelResponse < T > [ ]
878+ ) {
879+ let newChannels : Channel < T > [ ] = [ ] ;
880+ const watchRequests : Promise < any > [ ] = [ ] ;
881881 channelResponses . forEach ( ( channelResponse ) => {
882882 const channel = this . chatClientService . chatClient . channel (
883883 channelResponse . type ,
884884 channelResponse . id
885885 ) ;
886- void channel . watch ( ) ;
887- this . watchForChannelEvents ( channel ) ;
886+ watchRequests . push ( channel . watch ( ) ) ;
888887 newChannels . push ( channel ) ;
889888 } ) ;
890- this . channelsSubject . next ( [
891- ...newChannels ,
892- ...( this . channelsSubject . getValue ( ) || [ ] ) ,
893- ] ) ;
889+ await Promise . all ( watchRequests ) ;
890+ const currentChannels = this . channelsSubject . getValue ( ) || [ ] ;
891+ newChannels = newChannels . filter (
892+ ( newChannel ) => ! currentChannels . find ( ( c ) => c . cid === newChannel . cid )
893+ ) ;
894+ if ( newChannels . length > 0 ) {
895+ newChannels . forEach ( ( c ) => this . watchForChannelEvents ( c ) ) ;
896+ this . channelsSubject . next ( [ ...newChannels , ...currentChannels ] ) ;
897+ }
894898 }
895899
896900 private removeChannelsFromChannelList ( cids : string [ ] ) {
0 commit comments