Skip to content

Commit c72a90b

Browse files
committed
hamed/minor_refactor
1 parent 095954c commit c72a90b

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

lib/state/connection/connection_cubit.dart

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ class ConnectionCubit extends Cubit<ConnectionState> {
2929

3030
_api = Injector()<BaseAPI>();
3131

32-
_connectionInformation = connectionInformation;
33-
34-
_connect(_connectionInformation);
32+
connect(_connectionInformation = connectionInformation);
3533
}
3634

3735
final String _key = '${UniqueKey()}';
@@ -60,27 +58,25 @@ class ConnectionCubit extends Cubit<ConnectionState> {
6058
/// Gets app id of websocket.
6159
static String get appId => _connectionInformation.appId;
6260

63-
/// Reconnect to Websocket with new [connectionInformation].
64-
Future<void> reconnect({ConnectionInformation? connectionInformation}) async {
65-
if (connectionInformation != null) {
66-
_connectionInformation = connectionInformation;
67-
}
68-
69-
await _connect(_connectionInformation);
70-
}
61+
/// Connect to Websocket with new [connectionInformation].
62+
Future<void> connect([ConnectionInformation? connectionInformation]) async {
63+
try {
64+
if (connectionInformation != null) {
65+
_connectionInformation = connectionInformation;
66+
}
7167

72-
Future<void> _connect(ConnectionInformation connectionInformation) async {
73-
if (state is ConnectionConnectingState) {
74-
return;
75-
}
68+
await _api!.disconnect();
7669

77-
await _api!.connect(
78-
_connectionInformation,
79-
printResponse: enableDebug && printResponse,
80-
onError: (String error) => emit(ConnectionErrorState(error)),
81-
);
70+
await _api!.connect(
71+
_connectionInformation,
72+
printResponse: enableDebug && printResponse,
73+
onError: (String error) => emit(ConnectionErrorState(error)),
74+
);
8275

83-
_handleConnectionStatus();
76+
_handleConnectionStatus();
77+
} on Exception catch (e) {
78+
emit(ConnectionErrorState('$e'));
79+
}
8480
}
8581

8682
void _handleConnectionStatus() {

0 commit comments

Comments
 (0)