From 48dc8c78b4557fd3bd7ac74e5986476c117b25bb Mon Sep 17 00:00:00 2001 From: yflory Date: Wed, 6 Jun 2018 18:10:58 +0200 Subject: [PATCH] Fix cache and merge issues --- www/common/cryptpad-common.js | 10 +- www/common/outer/async-store.js | 2 +- www/common/outer/serviceworker.js | 200 ++++++++++++------------ www/common/outer/sharedworker.js | 243 ++++++++++++------------------ 4 files changed, 198 insertions(+), 257 deletions(-) diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 719f5486b..b04660c51 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -769,8 +769,10 @@ define([ var postMsg, worker; Nthen(function (waitFor2) { if (SharedWorker) { - worker = new SharedWorker('/common/outer/sharedworker.js' + urlArgs); - console.log(worker); + worker = new SharedWorker('/common/outer/sharedworker.js?' + urlArgs); + worker.onerror = function (e) { + console.error(e); + }; worker.port.onmessage = function (ev) { if (ev.data === "SW_READY") { return; @@ -789,7 +791,7 @@ define([ if (worker) { return void worker.postMessage(data); } }; - navigator.serviceWorker.register('/common/outer/serviceworker.js' + urlArgs, {scope: '/'}) + navigator.serviceWorker.register('/common/outer/serviceworker.js?' + urlArgs, {scope: '/'}) .then(function(reg) { // Add handler for receiving messages from the service worker navigator.serviceWorker.addEventListener('message', function (ev) { @@ -829,7 +831,7 @@ define([ /**/console.log('Registration failed with ' + error); }); } else if (Worker) { - worker = new Worker('/common/outer/webworker.js' + urlArgs); + worker = new Worker('/common/outer/webworker.js?' + urlArgs); worker.onmessage = function (ev) { msgEv.fire(ev); }; diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index 5e6b1b261..172ff406d 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -1049,7 +1049,7 @@ define([ channel.queue.forEach(function (data) { channel.sendMessage(data.message, clientId); }); - postMessage("PAD_CONNECT", { + channel.bcast("PAD_CONNECT", { myID: wc.myID, id: wc.id, members: wc.members diff --git a/www/common/outer/serviceworker.js b/www/common/outer/serviceworker.js index d7660c311..9c56a1250 100644 --- a/www/common/outer/serviceworker.js +++ b/www/common/outer/serviceworker.js @@ -1,22 +1,5 @@ /* jshint ignore:start */ importScripts('/bower_components/requirejs/require.js'); -require.config({ - // fix up locations so that relative urls work. - baseUrl: '/', - paths: { - // jquery declares itself as literally "jquery" so it cannot be pulled by path :( - "jquery": "/bower_components/jquery/dist/jquery.min", - // json.sortify same - "json.sortify": "/bower_components/json.sortify/dist/JSON.sortify", - cm: '/bower_components/codemirror' - }, - map: { - '*': { - 'css': '/bower_components/require-css/css.js', - 'less': '/common/RequireLess.js', - } - } -}); window = self; localStorage = { @@ -24,11 +7,12 @@ localStorage = { getItem: function (k) { return localStorage[k]; } }; +self.tabs = {}; + var postMsg = function (client, data) { client.postMessage(data); }; - var debug = function (msg) { console.log(msg); }; // debug = function () {}; @@ -36,111 +20,115 @@ var init = function (client, cb) { debug('SW INIT'); require([ - '/common/common-util.js', - '/common/outer/worker-channel.js', - '/common/outer/store-rpc.js' - ], function (Util, Channel, Rpc) { - debug('SW Required ressources loaded'); - var msgEv = Util.mkEvent(); + '/common/requireconfig.js' + ], function (RequireConfig) { + require.config(RequireConfig()); + require([ + '/common/common-util.js', + '/common/outer/worker-channel.js', + '/common/outer/store-rpc.js' + ], function (Util, Channel, Rpc) { + debug('SW Required ressources loaded'); + var msgEv = Util.mkEvent(); - var postToClient = function (data) { - postMsg(client, data); - }; - Channel.create(msgEv, postToClient, function (chan) { - debug('SW Channel created'); + var postToClient = function (data) { + postMsg(client, data); + }; + Channel.create(msgEv, postToClient, function (chan) { + debug('SW Channel created'); - var clientId = client.id; - self.tabs[clientId].chan = chan; - Object.keys(Rpc.queries).forEach(function (q) { - if (q === 'CONNECT') { return; } - if (q === 'JOIN_PAD') { return; } - if (q === 'SEND_PAD_MSG') { return; } - chan.on(q, function (data, cb) { - try { - Rpc.queries[q](clientId, data, cb); - } catch (e) { - console.error('Error in webworker when executing query ' + q); - console.error(e); - console.log(data); - } + var clientId = client.id; + self.tabs[clientId].chan = chan; + Object.keys(Rpc.queries).forEach(function (q) { + if (q === 'CONNECT') { return; } + if (q === 'JOIN_PAD') { return; } + if (q === 'SEND_PAD_MSG') { return; } + chan.on(q, function (data, cb) { + try { + Rpc.queries[q](clientId, data, cb); + } catch (e) { + console.error('Error in webworker when executing query ' + q); + console.error(e); + console.log(data); + } + }); }); - }); - chan.on('CONNECT', function (cfg, cb) { - debug('SW Connect callback'); - if (self.store) { - if (cfg.driveEvents) { - Rpc._subscribeToDrive(clientId); + chan.on('CONNECT', function (cfg, cb) { + debug('SW Connect callback'); + if (self.store) { + if (cfg.driveEvents) { + Rpc._subscribeToDrive(clientId); + } + if (cfg.messenger) { + Rpc._subscribeToMessenger(clientId); + } + return void cb(self.store); } - if (cfg.messenger) { - Rpc._subscribeToMessenger(clientId); - } - return void cb(self.store); - } - debug('Loading new async store'); - // One-time initialization (init async-store) - cfg.query = function (cId, cmd, data, cb) { - cb = cb || function () {}; - self.tabs[cId].chan.query(cmd, data, function (err, data2) { - if (err) { return void cb({error: err}); } - cb(data2); - }); - }; - cfg.broadcast = function (excludes, cmd, data, cb) { - cb = cb || function () {}; - Object.keys(self.tabs).forEach(function (cId) { - if (excludes.indexOf(cId) !== -1) { return; } + debug('Loading new async store'); + // One-time initialization (init async-store) + cfg.query = function (cId, cmd, data, cb) { + cb = cb || function () {}; self.tabs[cId].chan.query(cmd, data, function (err, data2) { if (err) { return void cb({error: err}); } cb(data2); }); + }; + cfg.broadcast = function (excludes, cmd, data, cb) { + cb = cb || function () {}; + Object.keys(self.tabs).forEach(function (cId) { + if (excludes.indexOf(cId) !== -1) { return; } + self.tabs[cId].chan.query(cmd, data, function (err, data2) { + if (err) { return void cb({error: err}); } + cb(data2); + }); + }); + }; + Rpc.queries['CONNECT'](clientId, cfg, function (data) { + if (cfg.driveEvents) { + Rpc._subscribeToDrive(clientId); + } + if (cfg.messenger) { + Rpc._subscribeToMessenger(clientId); + } + if (data && data.state === "ALREADY_INIT") { + return void cb(data.returned); + } + self.store = data; + cb(data); }); - }; - Rpc.queries['CONNECT'](clientId, cfg, function (data) { - if (cfg.driveEvents) { - Rpc._subscribeToDrive(clientId); - } - if (cfg.messenger) { - Rpc._subscribeToMessenger(clientId); + }); + chan.on('JOIN_PAD', function (data, cb) { + self.tabs[clientId].channelId = data.channel; + try { + Rpc.queries['JOIN_PAD'](clientId, data, cb); + } catch (e) { + console.error('Error in webworker when executing query JOIN_PAD'); + console.error(e); + console.log(data); } - if (data && data.state === "ALREADY_INIT") { - return void cb(data.returned); + }); + chan.on('SEND_PAD_MSG', function (msg, cb) { + var data = { + msg: msg, + channel: self.tabs[clientId].channelId + }; + try { + Rpc.queries['SEND_PAD_MSG'](clientId, data, cb); + } catch (e) { + console.error('Error in webworker when executing query SEND_PAD_MSG'); + console.error(e); + console.log(data); } - self.store = data; - cb(data); }); - }); - chan.on('JOIN_PAD', function (data, cb) { - self.tabs[clientId].channelId = data.channel; - try { - Rpc.queries['JOIN_PAD'](clientId, data, cb); - } catch (e) { - console.error('Error in webworker when executing query JOIN_PAD'); - console.error(e); - console.log(data); - } - }); - chan.on('SEND_PAD_MSG', function (msg, cb) { - var data = { - msg: msg, - channel: self.tabs[clientId].channelId - }; - try { - Rpc.queries['SEND_PAD_MSG'](clientId, data, cb); - } catch (e) { - console.error('Error in webworker when executing query SEND_PAD_MSG'); - console.error(e); - console.log(data); - } - }); - cb(); - }, true); + cb(); + }, true); - self.tabs[client.id].msgEv = msgEv; + self.tabs[client.id].msgEv = msgEv; + }); }); }; -self.tabs = {}; self.addEventListener('message', function (e) { var cId = e.source.id; if (e.data === "INIT") { diff --git a/www/common/outer/sharedworker.js b/www/common/outer/sharedworker.js index 9d5aa1187..3ca82f262 100644 --- a/www/common/outer/sharedworker.js +++ b/www/common/outer/sharedworker.js @@ -1,23 +1,5 @@ -console.log('SW!'); /* jshint ignore:start */ importScripts('/bower_components/requirejs/require.js'); -require.config({ - // fix up locations so that relative urls work. - baseUrl: '/', - paths: { - // jquery declares itself as literally "jquery" so it cannot be pulled by path :( - "jquery": "/bower_components/jquery/dist/jquery.min", - // json.sortify same - "json.sortify": "/bower_components/json.sortify/dist/JSON.sortify", - cm: '/bower_components/codemirror' - }, - map: { - '*': { - 'css': '/bower_components/require-css/css.js', - 'less': '/common/RequireLess.js', - } - } -}); window = self; localStorage = { @@ -26,16 +8,6 @@ localStorage = { }; self.tabs = {}; -var findTab = function (port) { - var tab; - Object.keys(self.tabs).some(function (id) { - if (self.tabs[id].port === port) { - tab = port; - return true; - } - }); - return tab; -}; var postMsg = function (client, data) { client.port.postMessage(data); @@ -48,114 +20,119 @@ var init = function (client, cb) { debug('SharedW INIT'); require([ - '/common/common-util.js', - '/common/outer/worker-channel.js', - '/common/outer/store-rpc.js' - ], function (Util, Channel, Rpc) { - debug('SharedW Required ressources loaded'); - var msgEv = Util.mkEvent(); - - var postToClient = function (data) { - postMsg(client, data); - }; - Channel.create(msgEv, postToClient, function (chan) { - debug('SharedW Channel created'); - - var clientId = client.id; - client.chan = chan; - Object.keys(Rpc.queries).forEach(function (q) { - if (q === 'CONNECT') { return; } - if (q === 'JOIN_PAD') { return; } - if (q === 'SEND_PAD_MSG') { return; } - chan.on(q, function (data, cb) { - try { - Rpc.queries[q](clientId, data, cb); - } catch (e) { - console.error('Error in webworker when executing query ' + q); - console.error(e); - console.log(data); - } + '/common/requireconfig.js' + ], function (RequireConfig) { + require.config(RequireConfig()); + require([ + '/common/common-util.js', + '/common/outer/worker-channel.js', + '/common/outer/store-rpc.js' + ], function (Util, Channel, Rpc) { + debug('SharedW Required ressources loaded'); + var msgEv = Util.mkEvent(); + + var postToClient = function (data) { + postMsg(client, data); + }; + Channel.create(msgEv, postToClient, function (chan) { + debug('SharedW Channel created'); + + var clientId = client.id; + client.chan = chan; + Object.keys(Rpc.queries).forEach(function (q) { + if (q === 'CONNECT') { return; } + if (q === 'JOIN_PAD') { return; } + if (q === 'SEND_PAD_MSG') { return; } + chan.on(q, function (data, cb) { + try { + Rpc.queries[q](clientId, data, cb); + } catch (e) { + console.error('Error in webworker when executing query ' + q); + console.error(e); + console.log(data); + } + }); }); - }); - chan.on('CONNECT', function (cfg, cb) { - debug('SharedW connecting to store...'); - if (self.store) { - debug('Store already exists!'); - if (cfg.driveEvents) { - Rpc._subscribeToDrive(clientId); + chan.on('CONNECT', function (cfg, cb) { + debug('SharedW connecting to store...'); + if (self.store) { + debug('Store already exists!'); + if (cfg.driveEvents) { + Rpc._subscribeToDrive(clientId); + } + if (cfg.messenger) { + Rpc._subscribeToMessenger(clientId); + } + return void cb(self.store); } - if (cfg.messenger) { - Rpc._subscribeToMessenger(clientId); - } - return void cb(self.store); - } - debug('Loading new async store'); - // One-time initialization (init async-store) - cfg.query = function (cId, cmd, data, cb) { - cb = cb || function () {}; - self.tabs[cId].chan.query(cmd, data, function (err, data2) { - if (err) { return void cb({error: err}); } - cb(data2); - }); - }; - cfg.broadcast = function (excludes, cmd, data, cb) { - cb = cb || function () {}; - Object.keys(self.tabs).forEach(function (cId) { - if (excludes.indexOf(cId) !== -1) { return; } + debug('Loading new async store'); + // One-time initialization (init async-store) + cfg.query = function (cId, cmd, data, cb) { + cb = cb || function () {}; self.tabs[cId].chan.query(cmd, data, function (err, data2) { if (err) { return void cb({error: err}); } cb(data2); }); + }; + cfg.broadcast = function (excludes, cmd, data, cb) { + cb = cb || function () {}; + Object.keys(self.tabs).forEach(function (cId) { + if (excludes.indexOf(cId) !== -1) { return; } + self.tabs[cId].chan.query(cmd, data, function (err, data2) { + if (err) { return void cb({error: err}); } + cb(data2); + }); + }); + }; + Rpc.queries['CONNECT'](clientId, cfg, function (data) { + if (cfg.driveEvents) { + Rpc._subscribeToDrive(clientId); + } + if (cfg.messenger) { + Rpc._subscribeToMessenger(clientId); + } + if (data && data.state === "ALREADY_INIT") { + self.store = data.returned; + return void cb(data.returned); + } + self.store = data; + cb(data); }); - }; - Rpc.queries['CONNECT'](clientId, cfg, function (data) { - if (cfg.driveEvents) { - Rpc._subscribeToDrive(clientId); - } - if (cfg.messenger) { - Rpc._subscribeToMessenger(clientId); + }); + chan.on('JOIN_PAD', function (data, cb) { + client.channelId = data.channel; + try { + Rpc.queries['JOIN_PAD'](clientId, data, cb); + } catch (e) { + console.error('Error in webworker when executing query JOIN_PAD'); + console.error(e); + console.log(data); } - if (data && data.state === "ALREADY_INIT") { - self.store = data.returned; - return void cb(data.returned); + }); + chan.on('SEND_PAD_MSG', function (msg, cb) { + var data = { + msg: msg, + channel: client.channelId + }; + try { + Rpc.queries['SEND_PAD_MSG'](clientId, data, cb); + } catch (e) { + console.error('Error in webworker when executing query SEND_PAD_MSG'); + console.error(e); + console.log(data); } - self.store = data; - cb(data); }); - }); - chan.on('JOIN_PAD', function (data, cb) { - client.channelId = data.channel; - try { - Rpc.queries['JOIN_PAD'](clientId, data, cb); - } catch (e) { - console.error('Error in webworker when executing query JOIN_PAD'); - console.error(e); - console.log(data); - } - }); - chan.on('SEND_PAD_MSG', function (msg, cb) { - var data = { - msg: msg, - channel: client.channelId - }; - try { - Rpc.queries['SEND_PAD_MSG'](clientId, data, cb); - } catch (e) { - console.error('Error in webworker when executing query SEND_PAD_MSG'); - console.error(e); - console.log(data); - } - }); - cb(); - }, true); + cb(); + }, true); - client.msgEv = msgEv; + client.msgEv = msgEv; + }); }); }; onconnect = function(e) { - debug('New ShardWorker client'); + debug('New SharedWorker client'); var port = e.ports[0]; var cId = Number(Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)) var client = self.tabs[cId] = { @@ -176,29 +153,3 @@ onconnect = function(e) { }; }; -self.tabs = {}; -self.addEventListener('message', function (e) { - var cId = e.source.id; - if (e.data === "INIT") { - if (tabs[cId]) { return; } - tabs[cId] = { - client: e.source - }; - init(e.source, function () { - postMsg(e.source, 'SW_READY'); - }); - } else if (self.tabs[cId] && self.tabs[cId].msgEv) { - self.tabs[cId].msgEv.fire(e); - } -}); -self.addEventListener('install', function (e) { - debug('V1 installing…'); - self.skipWaiting(); -}); - -self.addEventListener('activate', function (e) { - debug('V1 now ready to handle fetches!'); -}); - - -