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
+1 -17
View File
@@ -4,7 +4,6 @@ const iosPlatform = 'iOS';
const androidPlatform = 'android';
const otherPlatform = 'other';
const timeoutDuration = 4000;
var serviceWorkerVersion = null;
var scriptLoaded = false;
function loadMainDartJs() {
@@ -24,16 +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('firebase-messaging-sw.js').then(serviceWorkerRegistration => {
console.info('[TwakeMail] fetchServiceWorker(): Service worker firebase-messaging was registered.');
}).catch(error => {
console.error(
'[TwakeMail] fetchServiceWorker(): An error occurred while registering the service worker firebase-messaging.'
);
console.error(error);
});
var serviceWorkerUrl = 'flutter_service_worker.js?v=' + serviceWorkerVersion;
navigator.serviceWorker.register(serviceWorkerUrl)
navigator.serviceWorker.register('web-sockets-worker.js')
.then((reg) => {
function waitForActivation(serviceWorker) {
serviceWorker.addEventListener('statechange', () => {
@@ -47,12 +37,6 @@ function fetchServiceWorker() {
// No active web worker and we have installed or are installing
// one for the first time. Simply wait for it to activate.
waitForActivation(reg.installing || reg.waiting);
} else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) {
// When the app updates the serviceWorkerVersion changes, so we
// need to ask the service worker to update.
console.log('[TwakeMail] fetchServiceWorker(): New service worker available.');
reg.update();
waitForActivation(reg.installing);
} else {
// Existing service worker is still good.
console.log('[TwakeMail] fetchServiceWorker(): Loading app from service worker.');