TF-3157 Update web socket with background service worker

TF-3157 Stub BroadcastChannel for mobile build
This commit is contained in:
DatDang
2024-09-27 10:42:10 +07:00
committed by Dat H. Pham
parent c4d0c27603
commit 7d59982cc9
16 changed files with 414 additions and 67 deletions
@@ -2,4 +2,6 @@ class WebSocketPushNotSupportedException implements Exception {}
class WebSocketUriUnavailableException implements Exception {}
class WebSocketTicketUnavailableException implements Exception {}
class WebSocketTicketUnavailableException implements Exception {}
class WebSocketClosedException implements Exception {}
@@ -0,0 +1 @@
enum WebSocketAction {connect, disconnect}
@@ -5,12 +5,12 @@ import 'package:jmap_dart_client/jmap/push/state_change.dart';
class InitializingWebSocketPushChannel extends LoadingState {}
class WebSocketPushStateReceived extends UIState {
final StateChange stateChange;
final StateChange? stateChange;
WebSocketPushStateReceived(this.stateChange);
@override
List<Object> get props => [stateChange];
List<Object?> get props => [stateChange];
}
class WebSocketConnectionFailed extends FeatureFailure {
@@ -7,6 +7,7 @@ import 'package:dartz/dartz.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/session/session.dart';
import 'package:jmap_dart_client/jmap/push/state_change.dart';
import 'package:tmail_ui_user/features/push_notification/data/model/web_socket_echo.dart';
import 'package:tmail_ui_user/features/push_notification/domain/repository/web_socket_repository.dart';
import 'package:tmail_ui_user/features/push_notification/domain/state/web_socket_push_state.dart';
@@ -31,9 +32,21 @@ class ConnectWebSocketInteractor {
}
Either<Failure, Success> _toStateChange(dynamic data) {
if (data is String) {
data = jsonDecode(data);
StateChange? possibleStateChange;
try {
if (data is String) {
data = jsonDecode(data);
}
possibleStateChange = StateChange.fromJson(data);
return Right(WebSocketPushStateReceived(possibleStateChange));
} catch (e) {
logError('ConnectWebSocketInteractor::_toStateChange: '
'websocket message is not StateChange: $data');
final dataIsWebSocketEcho = WebSocketEcho.isValid(data);
if (dataIsWebSocketEcho) {
return Right(WebSocketPushStateReceived(null));
}
return Left(WebSocketConnectionFailed(exception: e));
}
return Right(WebSocketPushStateReceived(StateChange.fromJson(data)));
}
}