TF-3157 Update web socket with background service worker
TF-3157 Stub BroadcastChannel for mobile build
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/presentation/state/success.dart';
|
||||
@@ -17,12 +19,12 @@ abstract class PushBaseController {
|
||||
Session? session;
|
||||
AccountId? accountId;
|
||||
|
||||
StreamSubscription<Either<Failure, Success>>? _stateStreamSubscription;
|
||||
|
||||
void consumeState(Stream<Either<Failure, Success>> newStateStream) {
|
||||
newStateStream.listen(
|
||||
_stateStreamSubscription = newStateStream.listen(
|
||||
_handleStateStream,
|
||||
onError: (error, stackTrace) {
|
||||
logError('PushBaseController::consumeState():onError:error: $error | stackTrace: $stackTrace');
|
||||
}
|
||||
onError: handleErrorViewState,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -34,6 +36,15 @@ abstract class PushBaseController {
|
||||
|
||||
void handleSuccessViewState(Success success);
|
||||
|
||||
void handleErrorViewState(Object error, StackTrace stackTrace) {
|
||||
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;
|
||||
@@ -87,22 +98,20 @@ abstract class PushBaseController {
|
||||
{Session? session}
|
||||
) {
|
||||
final newState = jmap.State(mapTypeState[typeName.value]);
|
||||
if (typeName == TypeName.emailType) {
|
||||
if (isForeground) {
|
||||
return SynchronizeEmailOnForegroundAction(typeName, newState, accountId, session);
|
||||
} else {
|
||||
return StoreEmailStateToRefreshAction(typeName, newState, accountId, userName, session);
|
||||
}
|
||||
} else if (typeName == TypeName.emailDelivery) {
|
||||
if (!isForeground) {
|
||||
return PushNotificationAction(typeName, newState, session, accountId, userName);
|
||||
}
|
||||
} else if (typeName == TypeName.mailboxType) {
|
||||
if (isForeground) {
|
||||
return SynchronizeMailboxOnForegroundAction(typeName, newState, accountId);
|
||||
} else {
|
||||
return StoreMailboxStateToRefreshAction(typeName, newState, accountId, userName);
|
||||
}
|
||||
switch (typeName) {
|
||||
case TypeName.emailType:
|
||||
return isForeground
|
||||
? SynchronizeEmailOnForegroundAction(typeName, newState, accountId, session)
|
||||
: StoreEmailStateToRefreshAction(typeName, newState, accountId, userName, session);
|
||||
case TypeName.emailDelivery:
|
||||
if (!isForeground) {
|
||||
return PushNotificationAction(typeName, newState, session, accountId, userName);
|
||||
}
|
||||
break;
|
||||
case TypeName.mailboxType:
|
||||
return isForeground
|
||||
? SynchronizeMailboxOnForegroundAction(typeName, newState, accountId)
|
||||
: StoreMailboxStateToRefreshAction(typeName, newState, accountId, userName);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import 'package:core/presentation/state/success.dart';
|
||||
import 'package:core/utils/app_logger.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: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';
|
||||
@@ -20,9 +21,15 @@ class WebSocketController extends PushBaseController {
|
||||
|
||||
ConnectWebSocketInteractor? _connectWebSocketInteractor;
|
||||
|
||||
int _retryRemained = 3;
|
||||
|
||||
@override
|
||||
void handleFailureViewState(Failure failure) {
|
||||
logError('WebSocketController::handleFailureViewState():Failure $failure');
|
||||
cancelStateStreamSubscription();
|
||||
if (failure is WebSocketConnectionFailed) {
|
||||
_handleWebSocketConnectionRetry();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -33,6 +40,15 @@ class WebSocketController extends PushBaseController {
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void handleErrorViewState(Object error, StackTrace stackTrace) {
|
||||
super.handleErrorViewState(error, stackTrace);
|
||||
cancelStateStreamSubscription();
|
||||
if (error is WebSocketClosedException) {
|
||||
_handleWebSocketConnectionRetry();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void initialize({AccountId? accountId, Session? session}) {
|
||||
super.initialize(accountId: accountId, session: session);
|
||||
@@ -51,8 +67,10 @@ class WebSocketController extends PushBaseController {
|
||||
|
||||
void _handleWebSocketPushStateReceived(WebSocketPushStateReceived success) {
|
||||
log('WebSocketController::_handleWebSocketPushStateReceived(): $success');
|
||||
_retryRemained = 3;
|
||||
if (accountId == null || session == null) return;
|
||||
final stateChange = success.stateChange;
|
||||
if (stateChange == null) return;
|
||||
final mapTypeState = stateChange.getMapTypeState(accountId!);
|
||||
mappingTypeStateToAction(
|
||||
mapTypeState,
|
||||
@@ -62,4 +80,11 @@ class WebSocketController extends PushBaseController {
|
||||
session!.username,
|
||||
session: session);
|
||||
}
|
||||
|
||||
void _handleWebSocketConnectionRetry() {
|
||||
if (_retryRemained > 0) {
|
||||
_retryRemained--;
|
||||
_connectWebSocket(accountId, session);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user