44 Channel ,
55 ChannelFilters ,
66 ChannelResponse ,
7+ ConnectAPIResponse ,
78 OwnUserResponse ,
89 UserFilters ,
910 UserResponse ,
@@ -60,6 +61,7 @@ export class ChatClientService<
6061 private pendingInvitesSubject = new BehaviorSubject <
6162 ( ChannelResponse < T > | Channel < T > ) [ ]
6263 > ( [ ] ) ;
64+ private subscriptions : { unsubscribe : ( ) => void } [ ] = [ ] ;
6365
6466 constructor (
6567 private ngZone : NgZone ,
@@ -81,12 +83,13 @@ export class ChatClientService<
8183 apiKey : string ,
8284 userOrId : string | OwnUserResponse < T > | UserResponse < T > ,
8385 userTokenOrProvider : TokenOrProvider
84- ) {
86+ ) : ConnectAPIResponse < T > {
8587 this . chatClient = StreamChat . getInstance < T > ( apiKey ) ;
8688 this . chatClient . devToken ;
89+ let result ;
8790 await this . ngZone . runOutsideAngular ( async ( ) => {
8891 const user = typeof userOrId === 'string' ? { id : userOrId } : userOrId ;
89- await this . chatClient . connectUser ( user , userTokenOrProvider ) ;
92+ result = await this . chatClient . connectUser ( user , userTokenOrProvider ) ;
9093 this . chatClient . setUserAgent (
9194 `stream-chat-angular-${ version } -${ this . chatClient . getUserAgent ( ) } `
9295 ) ;
@@ -98,30 +101,35 @@ export class ChatClientService<
98101 ) ;
99102 this . pendingInvitesSubject . next ( channels ) ;
100103 this . appSettingsSubject . next ( undefined ) ;
101- this . chatClient . on ( ( e ) => {
102- this . updatePendingInvites ( e ) ;
103- this . notificationSubject . next ( {
104- eventType : e . type ,
105- event : e ,
106- } ) ;
107- } ) ;
104+ this . subscriptions . push (
105+ this . chatClient . on ( ( e ) => {
106+ this . updatePendingInvites ( e ) ;
107+ this . notificationSubject . next ( {
108+ eventType : e . type ,
109+ event : e ,
110+ } ) ;
111+ } )
112+ ) ;
108113 let removeNotification : undefined | Function ;
109- this . chatClient . on ( 'connection.changed' , ( e ) => {
110- this . ngZone . run ( ( ) => {
111- const isOnline = e . online ;
112- if ( isOnline ) {
113- if ( removeNotification ) {
114- removeNotification ( ) ;
114+ this . subscriptions . push (
115+ this . chatClient . on ( 'connection.changed' , ( e ) => {
116+ this . ngZone . run ( ( ) => {
117+ const isOnline = e . online ;
118+ if ( isOnline ) {
119+ if ( removeNotification ) {
120+ removeNotification ( ) ;
121+ }
122+ } else {
123+ removeNotification =
124+ this . notificationService . addPermanentNotification (
125+ 'streamChat.Connection failure, reconnecting now...'
126+ ) ;
115127 }
116- } else {
117- removeNotification =
118- this . notificationService . addPermanentNotification (
119- 'streamChat.Connection failure, reconnecting now...'
120- ) ;
121- }
122- this . connectionStateSubject . next ( isOnline ? 'online' : 'offline' ) ;
123- } ) ;
124- } ) ;
128+ this . connectionStateSubject . next ( isOnline ? 'online' : 'offline' ) ;
129+ } ) ;
130+ } )
131+ ) ;
132+ return result ;
125133 }
126134
127135 /**
@@ -130,6 +138,7 @@ export class ChatClientService<
130138 async disconnectUser ( ) {
131139 this . pendingInvitesSubject . next ( [ ] ) ;
132140 await this . chatClient . disconnectUser ( ) ;
141+ this . subscriptions . forEach ( ( s ) => s . unsubscribe ( ) ) ;
133142 }
134143
135144 /**
0 commit comments