Disconnect from shared/service worker

pull/1/head
yflory 7 years ago
parent f05e2225d6
commit 02b282a1a5

@ -768,7 +768,7 @@ define([
var msgEv = Util.mkEvent(); var msgEv = Util.mkEvent();
var postMsg, worker; var postMsg, worker;
Nthen(function (waitFor2) { Nthen(function (waitFor2) {
if (SharedWorker) { if (typeof(SharedWorker) !== "undefined") {
worker = new SharedWorker('/common/outer/sharedworker.js?' + urlArgs); worker = new SharedWorker('/common/outer/sharedworker.js?' + urlArgs);
worker.onerror = function (e) { worker.onerror = function (e) {
console.error(e); console.error(e);
@ -783,6 +783,10 @@ define([
worker.port.postMessage(data); worker.port.postMessage(data);
}; };
postMsg('INIT'); postMsg('INIT');
window.addEventListener('beforeunload', function () {
postMsg('CLOSE');
});
} else if (false && 'serviceWorker' in navigator) { } else if (false && 'serviceWorker' in navigator) {
var initializing = true; var initializing = true;
var stopWaiting = waitFor2(); // Call this function when we're ready var stopWaiting = waitFor2(); // Call this function when we're ready
@ -830,6 +834,10 @@ define([
}).catch(function(error) { }).catch(function(error) {
/**/console.log('Registration failed with ' + error); /**/console.log('Registration failed with ' + error);
}); });
window.addEventListener('beforeunload', function () {
postMsg('CLOSE');
});
} else if (Worker) { } else if (Worker) {
worker = new Worker('/common/outer/webworker.js?' + urlArgs); worker = new Worker('/common/outer/webworker.js?' + urlArgs);
worker.onmessage = function (ev) { worker.onmessage = function (ev) {

@ -1143,6 +1143,14 @@ define([
var driveEventClients = []; var driveEventClients = [];
var messengerEventClients = []; 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) { Store._removeClient = function (clientId) {
var driveIdx = driveEventClients.indexOf(clientId); var driveIdx = driveEventClients.indexOf(clientId);
if (driveIdx !== -1) { if (driveIdx !== -1) {
@ -1157,6 +1165,9 @@ define([
if (chanIdx !== -1) { if (chanIdx !== -1) {
Store.channels[chanId].clients.splice(chanIdx, 1); Store.channels[chanId].clients.splice(chanIdx, 1);
} }
if (Store.channels[chanId].clients.length === 0) {
dropChannel(chanId);
}
}); });
}; };

@ -125,6 +125,10 @@ var init = function (client, cb) {
}, true); }, true);
self.tabs[client.id].msgEv = msgEv; 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 () { init(e.source, function () {
postMsg(e.source, 'SW_READY'); 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) { } else if (self.tabs[cId] && self.tabs[cId].msgEv) {
self.tabs[cId].msgEv.fire(e); self.tabs[cId].msgEv.fire(e);
} }

@ -127,6 +127,10 @@ var init = function (client, cb) {
}, true); }, true);
client.msgEv = msgEv; client.msgEv = msgEv;
client.close = function () {
Rpc._removeClient(client.id);
};
}); });
}); });
}; };
@ -147,6 +151,11 @@ onconnect = function(e) {
init(client, function () { init(client, function () {
postMsg(client, 'SW_READY'); postMsg(client, 'SW_READY');
}); });
} else if (e.data === "CLOSE") {
if (client && client.close) {
console.log('leave');
client.close();
}
} else if (client && client.msgEv) { } else if (client && client.msgEv) {
client.msgEv.fire(e); client.msgEv.fire(e);
} }

Loading…
Cancel
Save