TF-3157 Remove unnecessary background service worker
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/session/session.dart';
|
||||
import 'package:web_socket_channel/web_socket_channel.dart';
|
||||
|
||||
abstract class WebSocketDatasource {
|
||||
Stream<dynamic> getWebSocketChannel(Session session, AccountId accountId);
|
||||
Future<WebSocketChannel> getWebSocketChannel(
|
||||
Session session,
|
||||
AccountId accountId);
|
||||
}
|
||||
+14
-42
@@ -1,21 +1,17 @@
|
||||
|
||||
import 'package:core/data/constants/constant.dart';
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:core/utils/broadcast_channel/broadcast_channel.dart';
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/capability/capability_identifier.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/capability/websocket_capability.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/session/session.dart';
|
||||
import 'package:model/extensions/session_extension.dart';
|
||||
import 'package:rxdart/transformers.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/data/datasource/web_socket_datasource.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/data/model/connect_web_socket_message.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/data/network/web_socket_api.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/domain/exceptions/web_socket_exceptions.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/domain/model/web_socket_action.dart';
|
||||
import 'package:tmail_ui_user/main/error/capability_validator.dart';
|
||||
import 'package:tmail_ui_user/main/exceptions/exception_thrower.dart';
|
||||
import 'package:universal_html/html.dart';
|
||||
import 'package:web_socket_channel/web_socket_channel.dart';
|
||||
|
||||
class WebSocketDatasourceImpl implements WebSocketDatasource {
|
||||
final WebSocketApi _webSocketApi;
|
||||
@@ -23,35 +19,21 @@ class WebSocketDatasourceImpl implements WebSocketDatasource {
|
||||
|
||||
const WebSocketDatasourceImpl(this._webSocketApi, this._exceptionThrower);
|
||||
|
||||
static const String _webSocketClosed = 'webSocketClosed';
|
||||
|
||||
@override
|
||||
Stream getWebSocketChannel(Session session, AccountId accountId) {
|
||||
return Stream
|
||||
.castFrom(_getWebSocketChannel(session, accountId))
|
||||
.doOnError(_exceptionThrower.throwException);
|
||||
}
|
||||
|
||||
Stream _getWebSocketChannel(
|
||||
Session session,
|
||||
AccountId accountId,
|
||||
) async* {
|
||||
final broadcastChannel = BroadcastChannel(Constant.wsServiceWorkerBroadcastChannel);
|
||||
try {
|
||||
Future<WebSocketChannel> getWebSocketChannel(Session session, AccountId accountId) {
|
||||
return Future.sync(() async {
|
||||
_verifyWebSocketCapabilities(session, accountId);
|
||||
final webSocketTicket = await _webSocketApi.getWebSocketTicket(session, accountId);
|
||||
final webSocketUri = _getWebSocketUri(session, accountId);
|
||||
window.navigator.serviceWorker?.controller?.postMessage(ConnectWebSocketMessage(
|
||||
webSocketAction: WebSocketAction.connect,
|
||||
webSocketUrl: webSocketUri.toString(),
|
||||
webSocketTicket: webSocketTicket
|
||||
).toJson());
|
||||
|
||||
yield* _webSocketListener(broadcastChannel);
|
||||
} catch (e) {
|
||||
logError('RemoteWebSocketDatasourceImpl::getWebSocketChannel():error: $e');
|
||||
rethrow;
|
||||
}
|
||||
final webSocketChannel = WebSocketChannel.connect(
|
||||
Uri.parse('$webSocketUri?ticket=$webSocketTicket'),
|
||||
protocols: ["jmap"],
|
||||
);
|
||||
|
||||
await webSocketChannel.ready;
|
||||
|
||||
return webSocketChannel;
|
||||
}).catchError(_exceptionThrower.throwException);
|
||||
}
|
||||
|
||||
void _verifyWebSocketCapabilities(Session session, AccountId accountId) {
|
||||
@@ -77,14 +59,4 @@ class WebSocketDatasourceImpl implements WebSocketDatasource {
|
||||
|
||||
return webSocketUri;
|
||||
}
|
||||
|
||||
Stream _webSocketListener(BroadcastChannel broadcastChannel) {
|
||||
return broadcastChannel.onMessage.map((event) {
|
||||
if (event.data == _webSocketClosed) {
|
||||
throw WebSocketClosedException();
|
||||
}
|
||||
|
||||
return event.data;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
|
||||
part 'web_socket_echo.g.dart';
|
||||
|
||||
@JsonSerializable(includeIfNull: false)
|
||||
class WebSocketEcho {
|
||||
@JsonKey(name: '@type')
|
||||
final String? type;
|
||||
final String? requestId;
|
||||
final List<List<dynamic>>? methodResponses;
|
||||
|
||||
WebSocketEcho({
|
||||
this.type,
|
||||
this.requestId,
|
||||
this.methodResponses,
|
||||
});
|
||||
|
||||
factory WebSocketEcho.fromJson(Map<String, dynamic> json) => _$WebSocketEchoFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$WebSocketEchoToJson(this);
|
||||
|
||||
static bool isValid(Map<String, dynamic> json) {
|
||||
try {
|
||||
final webSocketEcho = WebSocketEcho.fromJson(json);
|
||||
final listResponses = webSocketEcho.methodResponses?.firstOrNull;
|
||||
return listResponses?.contains('Core/echo') ?? false;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import 'package:jmap_dart_client/jmap/core/capability/capability_identifier.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/data/model/web_socket_request.dart';
|
||||
|
||||
class WebSocketEchoRequest extends WebSocketRequest {
|
||||
static const String type = 'Request';
|
||||
static const String id = 'R1';
|
||||
static final CapabilityIdentifier usingCapability = CapabilityIdentifier.jmapCore;
|
||||
static const String method = 'Core/echo';
|
||||
|
||||
@override
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'@type': type,
|
||||
'id': id,
|
||||
'using': [usingCapability.value.toString()],
|
||||
'methodCalls': [[method, {}, 'c0']],
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import 'package:fcm/model/type_name.dart';
|
||||
|
||||
class WebSocketPushEnableRequest {
|
||||
static const String type = 'WebSocketPushEnable';
|
||||
|
||||
static Map<String, dynamic> toJson({
|
||||
required List<TypeName> dataTypes,
|
||||
}) {
|
||||
return {
|
||||
'@type': type,
|
||||
'dataTypes': dataTypes.map((typeName) => typeName.value).toList(),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
abstract class WebSocketRequest {
|
||||
const WebSocketRequest();
|
||||
|
||||
Map<String, dynamic> toJson();
|
||||
}
|
||||
@@ -2,6 +2,7 @@ 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/data/datasource/web_socket_datasource.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/domain/repository/web_socket_repository.dart';
|
||||
import 'package:web_socket_channel/web_socket_channel.dart';
|
||||
|
||||
class WebSocketRepositoryImpl implements WebSocketRepository {
|
||||
final WebSocketDatasource _webSocketDatasource;
|
||||
@@ -9,6 +10,8 @@ class WebSocketRepositoryImpl implements WebSocketRepository {
|
||||
WebSocketRepositoryImpl(this._webSocketDatasource);
|
||||
|
||||
@override
|
||||
Stream getWebSocketChannel(Session session, AccountId accountId)
|
||||
=> _webSocketDatasource.getWebSocketChannel(session, accountId);
|
||||
Future<WebSocketChannel> getWebSocketChannel(
|
||||
Session session,
|
||||
AccountId accountId
|
||||
) => _webSocketDatasource.getWebSocketChannel(session, accountId);
|
||||
}
|
||||
Reference in New Issue
Block a user