From 7ce307ece2c3e673dd495e3b692cbd9b131807ff Mon Sep 17 00:00:00 2001 From: Tommy Chow Date: Sun, 23 Mar 2025 22:11:09 -0700 Subject: [PATCH] add missing cleanup in chat store --- lib/screens/channel/chat/stores/chat_store.dart | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/screens/channel/chat/stores/chat_store.dart b/lib/screens/channel/chat/stores/chat_store.dart index 57fbf7d1..392f1587 100644 --- a/lib/screens/channel/chat/stores/chat_store.dart +++ b/lib/screens/channel/chat/stores/chat_store.dart @@ -61,6 +61,9 @@ abstract class ChatStoreBase with Store { WebSocketChannel? _sevenTVChannel; + /// The subscription that handles the 7TV WebSocket connection. + StreamSubscription? _sevenTVChannelListener; + static const _maxRetries = 5; // The retry counter for exponential backoff. @@ -517,7 +520,9 @@ abstract class ChatStoreBase with Store { } } - _sevenTVChannel?.stream.listen( + // Cancel previous subscription if exists + _sevenTVChannelListener?.cancel(); + _sevenTVChannelListener = _sevenTVChannel?.stream.listen( (data) { if (!settings.showVideo || settings.chatDelay == 0) { listener(data); @@ -777,7 +782,13 @@ abstract class ChatStoreBase with Store { _notificationTimer?.cancel(); sleepTimer?.cancel(); + // Cancel WebSocket subscriptions + _channelListener?.cancel(); + _sevenTVChannelListener?.cancel(); + + // Close WebSocket channels _channel?.sink.close(1000); + _sevenTVChannel?.sink.close(1000); for (final reactionDisposer in reactions) { reactionDisposer();