From e15392ea5984619b8ab1ba3342a32fdff47e8ea4 Mon Sep 17 00:00:00 2001 From: cogwheel0 <172976095+cogwheel0@users.noreply.github.com> Date: Sat, 1 Nov 2025 22:51:32 +0530 Subject: [PATCH] refactor(socket): remove unused connection state management code --- lib/core/providers/app_providers.dart | 113 -------------------------- lib/core/services/socket_service.dart | 35 +------- 2 files changed, 2 insertions(+), 146 deletions(-) diff --git a/lib/core/providers/app_providers.dart b/lib/core/providers/app_providers.dart index cd331a4..0e9a30d 100644 --- a/lib/core/providers/app_providers.dart +++ b/lib/core/providers/app_providers.dart @@ -390,119 +390,6 @@ final socketServiceProvider = Provider((ref) { return asyncService.maybeWhen(data: (service) => service, orElse: () => null); }); -enum SocketConnectionState { disconnected, connecting, connected } - -@Riverpod(keepAlive: true) -class SocketConnectionStream extends _$SocketConnectionStream { - StreamController? _controller; - ProviderSubscription>? _serviceSubscription; - VoidCallback? _cancelConnectListener; - VoidCallback? _cancelDisconnectListener; - SocketConnectionState _latestState = SocketConnectionState.connecting; - - @override - Stream build() { - final controller = StreamController.broadcast( - sync: true, - ); - controller - ..onListen = _primeState - ..onCancel = _maybeNotifyDisconnected; - _controller = controller; - - final initialService = ref - .watch(socketServiceManagerProvider) - .maybeWhen(data: (service) => service, orElse: () => null); - _handleServiceChange(initialService); - - _serviceSubscription = ref.listen>( - socketServiceManagerProvider, - (_, next) => _handleServiceChange( - next.maybeWhen(data: (service) => service, orElse: () => null), - ), - ); - - ref.onDispose(() { - _serviceSubscription?.close(); - _serviceSubscription = null; - _unbindSocket(); - _controller?.close(); - _controller = null; - }); - - return controller.stream; - } - - /// Publishes a disconnected state when the final listener cancels. - void _maybeNotifyDisconnected() { - try { - _controller?.add(SocketConnectionState.disconnected); - _latestState = SocketConnectionState.disconnected; - } catch (_) {} - } - - /// Replays the cached state to new listeners. - void _primeState() { - try { - _controller?.add(_latestState); - } catch (_) {} - } - - void _handleServiceChange(SocketService? service) { - if (service == null) { - _unbindSocket(); - _emit(SocketConnectionState.connecting); - return; - } - - _emit( - service.isConnected - ? SocketConnectionState.connected - : SocketConnectionState.connecting, - ); - _bindSocket(service); - } - - void _bindSocket(SocketService service) { - _unbindSocket(); - - void handleConnect(dynamic _) { - _emit(SocketConnectionState.connected); - } - - void handleDisconnect(dynamic _) { - _emit(SocketConnectionState.disconnected); - } - - service.socket?.on('connect', handleConnect); - service.socket?.on('disconnect', handleDisconnect); - - _cancelConnectListener = () { - service.socket?.off('connect', handleConnect); - }; - _cancelDisconnectListener = () { - service.socket?.off('disconnect', handleDisconnect); - }; - } - - void _emit(SocketConnectionState next) { - if (_latestState == next) { - return; - } - _latestState = next; - try { - _controller?.add(next); - } catch (_) {} - } - - void _unbindSocket() { - _cancelConnectListener?.call(); - _cancelDisconnectListener?.call(); - _cancelConnectListener = null; - _cancelDisconnectListener = null; - } -} - @Riverpod(keepAlive: true) class ConversationDeltaStream extends _$ConversationDeltaStream { StreamController? _controller; diff --git a/lib/core/services/socket_service.dart b/lib/core/services/socket_service.dart index fe88c19..fe7d773 100644 --- a/lib/core/services/socket_service.dart +++ b/lib/core/services/socket_service.dart @@ -2,10 +2,7 @@ import 'package:flutter/widgets.dart'; import 'package:socket_io_client/socket_io_client.dart' as io; import '../models/server_config.dart'; -import '../../l10n/app_localizations.dart'; -import 'navigation_service.dart'; import 'socket_tls_override.dart'; -import '../../shared/utils/ui_utils.dart'; typedef SocketChatEventHandler = void Function( @@ -282,37 +279,9 @@ class SocketService with WidgetsBindingObserver { } } - void _handleConnectError(dynamic err) { - // Show user-facing error notification - final context = NavigationService.context; - if (context != null) { - final l10n = AppLocalizations.of(context); - if (l10n != null) { - UiUtils.showMessage( - context, - l10n.websocketConnectionError, - isError: true, - duration: const Duration(seconds: 5), - ); - } - } - } + void _handleConnectError(dynamic err) {} - void _handleReconnectFailed(dynamic _) { - // Show user-facing error notification - final context = NavigationService.context; - if (context != null) { - final l10n = AppLocalizations.of(context); - if (l10n != null) { - UiUtils.showMessage( - context, - l10n.websocketReconnectFailed, - isError: true, - duration: const Duration(seconds: 5), - ); - } - } - } + void _handleReconnectFailed(dynamic _) {} void _handleDisconnect(dynamic reason) { // Silent disconnect