refactor(socket): remove unused connection state management code

This commit is contained in:
cogwheel0
2025-11-01 22:51:32 +05:30
parent 8cc7a23477
commit e15392ea59
2 changed files with 2 additions and 146 deletions

View File

@@ -390,119 +390,6 @@ final socketServiceProvider = Provider<SocketService?>((ref) {
return asyncService.maybeWhen(data: (service) => service, orElse: () => null);
});
enum SocketConnectionState { disconnected, connecting, connected }
@Riverpod(keepAlive: true)
class SocketConnectionStream extends _$SocketConnectionStream {
StreamController<SocketConnectionState>? _controller;
ProviderSubscription<AsyncValue<SocketService?>>? _serviceSubscription;
VoidCallback? _cancelConnectListener;
VoidCallback? _cancelDisconnectListener;
SocketConnectionState _latestState = SocketConnectionState.connecting;
@override
Stream<SocketConnectionState> build() {
final controller = StreamController<SocketConnectionState>.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<AsyncValue<SocketService?>>(
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<ConversationDelta>? _controller;