From 02b282a1a5c2b499e24ad23820158d8569b07a63 Mon Sep 17 00:00:00 2001 From: yflory Date: Fri, 8 Jun 2018 16:45:07 +0200 Subject: [PATCH] Disconnect from shared/service worker --- www/common/cryptpad-common.js | 10 +++++++++- www/common/outer/async-store.js | 11 +++++++++++ www/common/outer/serviceworker.js | 9 +++++++++ www/common/outer/sharedworker.js | 9 +++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 38b2c9318..fd4288d4f 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -768,7 +768,7 @@ define([ var msgEv = Util.mkEvent(); var postMsg, worker; Nthen(function (waitFor2) { - if (SharedWorker) { + if (typeof(SharedWorker) !== "undefined") { worker = new SharedWorker('/common/outer/sharedworker.js?' + urlArgs); worker.onerror = function (e) { console.error(e); @@ -783,6 +783,10 @@ define([ worker.port.postMessage(data); }; postMsg('INIT'); + + window.addEventListener('beforeunload', function () { + postMsg('CLOSE'); + }); } else if (false && 'serviceWorker' in navigator) { var initializing = true; var stopWaiting = waitFor2(); // Call this function when we're ready @@ -830,6 +834,10 @@ define([ }).catch(function(error) { /**/console.log('Registration failed with ' + error); }); + + window.addEventListener('beforeunload', function () { + postMsg('CLOSE'); + }); } else if (Worker) { worker = new Worker('/common/outer/webworker.js?' + urlArgs); worker.onmessage = function (ev) { diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index d8b0c6642..3015e6ca1 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -1143,6 +1143,14 @@ define([ var driveEventClients = []; var messengerEventClients = []; + var dropChannel = function (chanId) { + if (!Store.channels[chanId]) { return; } + + if (Store.channels[chanId].wc) { + Store.channels[chanId].wc.leave(''); + } + delete Store.channels[chanId]; + }; Store._removeClient = function (clientId) { var driveIdx = driveEventClients.indexOf(clientId); if (driveIdx !== -1) { @@ -1157,6 +1165,9 @@ define([ if (chanIdx !== -1) { Store.channels[chanId].clients.splice(chanIdx, 1); } + if (Store.channels[chanId].clients.length === 0) { + dropChannel(chanId); + } }); }; diff --git a/www/common/outer/serviceworker.js b/www/common/outer/serviceworker.js index 9c56a1250..214df9abf 100644 --- a/www/common/outer/serviceworker.js +++ b/www/common/outer/serviceworker.js @@ -125,6 +125,10 @@ var init = function (client, cb) { }, true); self.tabs[client.id].msgEv = msgEv; + + self.tabs[client.id].close = function () { + Rpc._removeClient(client.id); + }; }); }); }; @@ -139,6 +143,11 @@ self.addEventListener('message', function (e) { init(e.source, function () { postMsg(e.source, 'SW_READY'); }); + } else if (e.data === "CLOSE") { + if (tabs[cId] && tabs[cId].close) { + console.log('leave'); + tabs[cId].close(); + } } else if (self.tabs[cId] && self.tabs[cId].msgEv) { self.tabs[cId].msgEv.fire(e); } diff --git a/www/common/outer/sharedworker.js b/www/common/outer/sharedworker.js index 3ca82f262..14bfa1250 100644 --- a/www/common/outer/sharedworker.js +++ b/www/common/outer/sharedworker.js @@ -127,6 +127,10 @@ var init = function (client, cb) { }, true); client.msgEv = msgEv; + + client.close = function () { + Rpc._removeClient(client.id); + }; }); }); }; @@ -147,6 +151,11 @@ onconnect = function(e) { init(client, function () { postMsg(client, 'SW_READY'); }); + } else if (e.data === "CLOSE") { + if (client && client.close) { + console.log('leave'); + client.close(); + } } else if (client && client.msgEv) { client.msgEv.fire(e); }