Skip to content

Commit

Permalink
TF-3157 Remove unnecessary background service worker
Browse files Browse the repository at this point in the history
  • Loading branch information
tddang-linagora committed Nov 4, 2024
1 parent 66b39ee commit 00411b1
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@

import 'package:core/data/constants/constant.dart';
import 'dart:async';
import 'dart:convert';

import 'package:core/utils/app_logger.dart';
import 'package:core/utils/broadcast_channel/broadcast_channel.dart';
import 'package:fcm/model/type_name.dart';
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/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/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;
final ExceptionThrower _exceptionThrower;

const WebSocketDatasourceImpl(this._webSocketApi, this._exceptionThrower);

static const String _webSocketClosed = 'webSocketClosed';

@override
Stream getWebSocketChannel(Session session, AccountId accountId) {
return Stream
Expand All @@ -36,20 +36,30 @@ class WebSocketDatasourceImpl implements WebSocketDatasource {
Session session,
AccountId accountId,
) async* {
final broadcastChannel = BroadcastChannel(Constant.wsServiceWorkerBroadcastChannel);
Timer? timer;
try {
_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);

final webSocketChannel = WebSocketChannel.connect(
Uri.parse('$webSocketUri?ticket=$webSocketTicket'),
protocols: ["jmap"],
);
await webSocketChannel.ready;
webSocketChannel.sink.add(jsonEncode(WebSocketPushEnableRequest.toJson(
dataTypes: [
TypeName.emailType,
TypeName.mailboxType,
]
)));
timer = Timer.periodic(const Duration(seconds: 10), (timer) {
webSocketChannel.sink.add(jsonEncode(WebSocketEchoRequest.toJson()));
});
yield* webSocketChannel.stream;
} catch (e) {
logError('RemoteWebSocketDatasourceImpl::getWebSocketChannel():error: $e');
timer?.cancel();
rethrow;
}
}
Expand Down Expand Up @@ -77,14 +87,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;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:jmap_dart_client/jmap/core/capability/capability_identifier.dart';

class WebSocketEchoRequest {
static const String type = 'Request';
static const String id = 'R1';
static CapabilityIdentifier using = CapabilityIdentifier.jmapCore;
static const String method = 'Core/echo';

const WebSocketEchoRequest._();

static Map<String, dynamic> toJson() {
return {
'@type': type,
'id': id,
'using': [using.value.toString()],
'methodCalls': [[method, {}, 'c0']],
};
}
}
Original file line number Diff line number Diff line change
@@ -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(),
};
}
}
74 changes: 0 additions & 74 deletions web/web-sockets-worker.js

This file was deleted.

2 changes: 1 addition & 1 deletion web/worker_service/worker_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function fetchServiceWorker() {
// Wait for registration to finish before dropping the <script>tag.
// Otherwise, the browser will load the script multiple times,
// potentially different versions.
navigator.serviceWorker.register('web-sockets-worker.js')
navigator.serviceWorker.register('flutter_service_worker.js?v={{flutter_service_worker_version}}')
.then((reg) => {
function waitForActivation(serviceWorker) {
serviceWorker.addEventListener('statechange', () => {
Expand Down

0 comments on commit 00411b1

Please sign in to comment.