TF-3157 Remove unnecessary background service worker
This commit is contained in:
@@ -19,10 +19,8 @@ abstract class PushBaseController {
|
||||
Session? session;
|
||||
AccountId? accountId;
|
||||
|
||||
StreamSubscription<Either<Failure, Success>>? _stateStreamSubscription;
|
||||
|
||||
void consumeState(Stream<Either<Failure, Success>> newStateStream) {
|
||||
_stateStreamSubscription = newStateStream.listen(
|
||||
newStateStream.listen(
|
||||
_handleStateStream,
|
||||
onError: handleErrorViewState,
|
||||
);
|
||||
@@ -40,11 +38,6 @@ abstract class PushBaseController {
|
||||
logError('PushBaseController::handleErrorViewState():error: $error | stackTrace: $stackTrace');
|
||||
}
|
||||
|
||||
void cancelStateStreamSubscription() {
|
||||
_stateStreamSubscription?.cancel();
|
||||
_stateStreamSubscription = null;
|
||||
}
|
||||
|
||||
void initialize({AccountId? accountId, Session? session}) {
|
||||
this.accountId = accountId;
|
||||
this.session = session;
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/presentation/state/success.dart';
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:fcm/model/type_name.dart';
|
||||
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/session/session.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/domain/exceptions/web_socket_exceptions.dart';
|
||||
import 'package:jmap_dart_client/jmap/push/state_change.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/data/model/web_socket_echo_request.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/data/model/web_socket_push_enable_request.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/domain/state/web_socket_push_state.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/domain/usecases/connect_web_socket_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/controller/push_base_controller.dart';
|
||||
@@ -11,6 +17,7 @@ import 'package:tmail_ui_user/features/push_notification/presentation/extensions
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/listener/email_change_listener.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/listener/mailbox_change_listener.dart';
|
||||
import 'package:tmail_ui_user/main/routes/route_navigation.dart';
|
||||
import 'package:web_socket_channel/web_socket_channel.dart';
|
||||
|
||||
class WebSocketController extends PushBaseController {
|
||||
WebSocketController._internal();
|
||||
@@ -22,11 +29,16 @@ class WebSocketController extends PushBaseController {
|
||||
ConnectWebSocketInteractor? _connectWebSocketInteractor;
|
||||
|
||||
int _retryRemained = 3;
|
||||
WebSocketChannel? _webSocketChannel;
|
||||
Timer? _webSocketPingTimer;
|
||||
StreamSubscription? _webSocketSubscription;
|
||||
|
||||
@override
|
||||
void handleFailureViewState(Failure failure) {
|
||||
logError('WebSocketController::handleFailureViewState():Failure $failure');
|
||||
cancelStateStreamSubscription();
|
||||
_webSocketSubscription?.cancel();
|
||||
_webSocketChannel = null;
|
||||
_webSocketPingTimer?.cancel();
|
||||
if (failure is WebSocketConnectionFailed) {
|
||||
_handleWebSocketConnectionRetry();
|
||||
}
|
||||
@@ -35,18 +47,15 @@ class WebSocketController extends PushBaseController {
|
||||
@override
|
||||
void handleSuccessViewState(Success success) {
|
||||
log('WebSocketController::handleSuccessViewState():Success $success');
|
||||
if (success is WebSocketPushStateReceived) {
|
||||
_handleWebSocketPushStateReceived(success);
|
||||
if (success is WebSocketConnectionSuccess) {
|
||||
_handleWebSocketConnectionSuccess(success);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void handleErrorViewState(Object error, StackTrace stackTrace) {
|
||||
super.handleErrorViewState(error, stackTrace);
|
||||
cancelStateStreamSubscription();
|
||||
if (error is WebSocketClosedException) {
|
||||
_handleWebSocketConnectionRetry();
|
||||
}
|
||||
handleFailureViewState(WebSocketConnectionFailed());
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -65,26 +74,69 @@ class WebSocketController extends PushBaseController {
|
||||
consumeState(_connectWebSocketInteractor!.execute(session, accountId));
|
||||
}
|
||||
|
||||
void _handleWebSocketPushStateReceived(WebSocketPushStateReceived success) {
|
||||
log('WebSocketController::_handleWebSocketPushStateReceived(): $success');
|
||||
void _handleWebSocketConnectionSuccess(WebSocketConnectionSuccess success) {
|
||||
log('WebSocketController::_handleWebSocketConnectionSuccess(): $success');
|
||||
_retryRemained = 3;
|
||||
if (accountId == null || session == null) return;
|
||||
final stateChange = success.stateChange;
|
||||
if (stateChange == null) return;
|
||||
final mapTypeState = stateChange.getMapTypeState(accountId!);
|
||||
mappingTypeStateToAction(
|
||||
mapTypeState,
|
||||
accountId!,
|
||||
emailChangeListener: EmailChangeListener.instance,
|
||||
mailboxChangeListener: MailboxChangeListener.instance,
|
||||
session!.username,
|
||||
session: session);
|
||||
_webSocketChannel = success.webSocketChannel;
|
||||
_enableWebSocketPush();
|
||||
_pingWebSocket();
|
||||
_listenToWebSocket();
|
||||
}
|
||||
|
||||
void _handleWebSocketConnectionRetry() {
|
||||
_webSocketSubscription?.cancel();
|
||||
_webSocketChannel = null;
|
||||
_webSocketPingTimer?.cancel();
|
||||
if (_retryRemained > 0) {
|
||||
_retryRemained--;
|
||||
_connectWebSocket(accountId, session);
|
||||
}
|
||||
}
|
||||
|
||||
void _enableWebSocketPush() {
|
||||
_webSocketChannel?.sink.add(jsonEncode(WebSocketPushEnableRequest.toJson(
|
||||
dataTypes: [TypeName.emailType, TypeName.mailboxType]
|
||||
)));
|
||||
}
|
||||
|
||||
void _pingWebSocket() {
|
||||
_webSocketPingTimer = Timer.periodic(const Duration(seconds: 10), (_) {
|
||||
_webSocketChannel?.sink.add(jsonEncode(WebSocketEchoRequest().toJson()));
|
||||
});
|
||||
}
|
||||
|
||||
void _listenToWebSocket() {
|
||||
_webSocketSubscription = _webSocketChannel?.stream.listen(
|
||||
(data) {
|
||||
log('WebSocketController::_listenToWebSocket(): $data');
|
||||
if (session == null || accountId == null) return;
|
||||
if (data is String) {
|
||||
data = jsonDecode(data);
|
||||
}
|
||||
|
||||
try {
|
||||
final stateChange = StateChange.fromJson(data);
|
||||
final mapTypeState = stateChange.getMapTypeState(accountId!);
|
||||
mappingTypeStateToAction(
|
||||
mapTypeState,
|
||||
accountId!,
|
||||
emailChangeListener: EmailChangeListener.instance,
|
||||
mailboxChangeListener: MailboxChangeListener.instance,
|
||||
session!.username,
|
||||
session: session);
|
||||
} catch (e) {
|
||||
logError('WebSocketController::_listenToWebSocket(): Data is not StateChange');
|
||||
}
|
||||
},
|
||||
cancelOnError: true,
|
||||
onError: (error) {
|
||||
logError('WebSocketController::_listenToWebSocket():Error: $error');
|
||||
handleFailureViewState(WebSocketConnectionFailed(exception: error));
|
||||
},
|
||||
onDone: () {
|
||||
log('WebSocketController::_listenToWebSocket():onDone');
|
||||
_handleWebSocketConnectionRetry();
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user