@@ -123,6 +123,13 @@ class StreamChatClient {
123123 },
124124 );
125125
126+ _connectionStatusSubscription = wsConnectionStatusStream.pairwise ().listen (
127+ (statusPair) {
128+ final [prevStatus, currStatus] = statusPair;
129+ return _onConnectionStatusChanged (prevStatus, currStatus);
130+ },
131+ );
132+
126133 state = ClientState (this );
127134 }
128135
@@ -220,7 +227,7 @@ class StreamChatClient {
220227 ///```
221228 final LogHandlerFunction logHandlerFunction;
222229
223- StreamSubscription <ConnectionStatus >? _connectionStatusSubscription;
230+ StreamSubscription <List < ConnectionStatus > >? _connectionStatusSubscription;
224231
225232 final _eventController = PublishSubject <Event >();
226233
@@ -238,20 +245,14 @@ class StreamChatClient {
238245 },
239246 );
240247
241- final _wsConnectionStatusController =
242- BehaviorSubject .seeded (ConnectionStatus .disconnected);
243-
244- set _wsConnectionStatus (ConnectionStatus status) =>
245- _wsConnectionStatusController.add (status);
246-
247248 /// The current status value of the [_ws] connection
248- ConnectionStatus get wsConnectionStatus =>
249- _wsConnectionStatusController.value;
249+ ConnectionStatus get wsConnectionStatus => _ws.connectionStatus;
250250
251251 /// This notifies the connection status of the [_ws] connection.
252252 /// Listen to this to get notified when the [_ws] tries to reconnect.
253- Stream <ConnectionStatus > get wsConnectionStatusStream =>
254- _wsConnectionStatusController.stream.distinct ();
253+ Stream <ConnectionStatus > get wsConnectionStatusStream {
254+ return _ws.connectionStatusStream.distinct ();
255+ }
255256
256257 /// Default log handler function for the [StreamChatClient] logger.
257258 static void defaultLogHandler (LogRecord record) {
@@ -447,16 +448,6 @@ class StreamChatClient {
447448 throw StreamChatError ('Connection already available for ${user .id }' );
448449 }
449450
450- _wsConnectionStatus = ConnectionStatus .connecting;
451-
452- // skipping `ws` seed connection status -> ConnectionStatus.disconnected
453- // otherwise `client.wsConnectionStatusStream` will emit in order
454- // 1. ConnectionStatus.disconnected -> client seed status
455- // 2. ConnectionStatus.connecting -> client connecting status
456- // 3. ConnectionStatus.disconnected -> ws seed status
457- _connectionStatusSubscription =
458- _ws.connectionStatusStream.skip (1 ).listen (_connectionStatusHandler);
459-
460451 try {
461452 final event = await _ws.connect (
462453 user,
@@ -479,13 +470,7 @@ class StreamChatClient {
479470 /// This will not trigger default auto-retry mechanism for reconnection.
480471 /// You need to call [openConnection] to reconnect to [_ws] .
481472 void closeConnection () {
482- if (wsConnectionStatus == ConnectionStatus .disconnected) return ;
483-
484473 logger.info ('Closing web-socket connection for ${state .currentUser ?.id }' );
485- _wsConnectionStatus = ConnectionStatus .disconnected;
486-
487- _connectionStatusSubscription? .cancel ();
488- _connectionStatusSubscription = null ;
489474
490475 // Stop listening to events
491476 state.cancelEventSubscription ();
@@ -513,19 +498,25 @@ class StreamChatClient {
513498 return _eventController.add (event);
514499 }
515500
516- void _connectionStatusHandler (ConnectionStatus status) async {
517- final previousState = wsConnectionStatus;
518- final currentState = _wsConnectionStatus = status;
501+ void _onConnectionStatusChanged (
502+ ConnectionStatus prevStatus,
503+ ConnectionStatus currStatus,
504+ ) async {
505+ // If the status hasn't changed, we don't need to do anything.
506+ if (prevStatus == currStatus) return ;
519507
520- if (previousState != currentState) {
521- handleEvent (Event (
522- type: EventType .connectionChanged,
523- online: status == ConnectionStatus .connected,
524- ));
525- }
508+ final wasConnected = prevStatus == ConnectionStatus .connected;
509+ final isConnected = currStatus == ConnectionStatus .connected;
510+
511+ // Notify the connection status change event
512+ handleEvent (Event (
513+ type: EventType .connectionChanged,
514+ online: isConnected,
515+ ));
526516
527- if (currentState == ConnectionStatus .connected &&
528- previousState != ConnectionStatus .connected) {
517+ final connectionRecovered = ! wasConnected && isConnected;
518+
519+ if (connectionRecovered) {
529520 // connection recovered
530521 final cids = state.channels.keys.toList (growable: false );
531522 if (cids.isNotEmpty) {
@@ -2025,6 +2016,9 @@ class StreamChatClient {
20252016 Future <void > disconnectUser ({bool flushChatPersistence = false }) async {
20262017 logger.info ('Disconnecting user : ${state .currentUser ?.id }' );
20272018
2019+ // closing web-socket connection
2020+ closeConnection ();
2021+
20282022 // resetting state.
20292023 state.dispose ();
20302024 state = ClientState (this );
@@ -2035,27 +2029,17 @@ class StreamChatClient {
20352029 _connectionIdManager.reset ();
20362030
20372031 // closing persistence connection.
2038- await closePersistenceConnection (flush: flushChatPersistence);
2039-
2040- // closing web-socket connection
2041- return closeConnection ();
2032+ return closePersistenceConnection (flush: flushChatPersistence);
20422033 }
20432034
20442035 /// Call this function to dispose the client
20452036 Future <void > dispose () async {
2046- logger.info ('Disposing new StreamChatClient' );
2047-
2048- // disposing state.
2049- state.dispose ();
2050-
2051- // closing persistence connection.
2052- await closePersistenceConnection ();
2053-
2054- // closing web-socket connection.
2055- closeConnection ();
2037+ logger.info ('Disposing StreamChatClient' );
20562038
2039+ await disconnectUser ();
2040+ await _ws.dispose ();
20572041 await _eventController.close ();
2058- await _wsConnectionStatusController. close ();
2042+ await _connectionStatusSubscription ? . cancel ();
20592043 }
20602044}
20612045
0 commit comments