Fix cache issues with workers in firefox

This commit is contained in:
yflory 2018-08-22 15:26:42 +02:00
parent 2259d1b6ac
commit b084b892bd
3 changed files with 303 additions and 294 deletions

View File

@ -19,127 +19,130 @@ var debug = function (msg) { console.log(msg); };
var init = function (client, cb) { var init = function (client, cb) {
debug('SW INIT'); debug('SW INIT');
require([ require(['/api/config?cb=' + (+new Date()).toString(16)], function (ApiConfig) {
'/common/requireconfig.js' if (ApiConfig.requireConf) { require.config(ApiConfig.requireConf); }
], function (RequireConfig) {
require.config(RequireConfig());
require([ require([
'/common/common-util.js', '/common/requireconfig.js'
'/common/outer/worker-channel.js', ], function (RequireConfig) {
'/common/outer/store-rpc.js' require.config(RequireConfig());
], function (Util, Channel, SRpc) { require([
debug('SW Required ressources loaded'); '/common/common-util.js',
var msgEv = Util.mkEvent(); '/common/outer/worker-channel.js',
'/common/outer/store-rpc.js'
], function (Util, Channel, SRpc) {
debug('SW Required ressources loaded');
var msgEv = Util.mkEvent();
if (!self.Rpc) { if (!self.Rpc) {
self.Rpc = SRpc(); self.Rpc = SRpc();
} }
var Rpc = self.Rpc; var Rpc = self.Rpc;
var postToClient = function (data) { var postToClient = function (data) {
postMsg(client, data); postMsg(client, data);
}; };
Channel.create(msgEv, postToClient, function (chan) { Channel.create(msgEv, postToClient, function (chan) {
debug('SW Channel created'); debug('SW Channel created');
var clientId = client.id; var clientId = client.id;
self.tabs[clientId].chan = chan; self.tabs[clientId].chan = chan;
Object.keys(Rpc.queries).forEach(function (q) { Object.keys(Rpc.queries).forEach(function (q) {
if (q === 'CONNECT') { return; } if (q === 'CONNECT') { return; }
if (q === 'JOIN_PAD') { return; } if (q === 'JOIN_PAD') { return; }
if (q === 'SEND_PAD_MSG') { return; } if (q === 'SEND_PAD_MSG') { return; }
chan.on(q, function (data, cb) { chan.on(q, function (data, cb) {
try { try {
Rpc.queries[q](clientId, data, cb); Rpc.queries[q](clientId, data, cb);
} catch (e) { } catch (e) {
console.error('Error in webworker when executing query ' + q); console.error('Error in webworker when executing query ' + q);
console.error(e); console.error(e);
console.log(data); console.log(data);
} }
if (q === "DISCONNECT") { if (q === "DISCONNECT") {
console.log('Deleting existing store!'); console.log('Deleting existing store!');
delete self.Rpc; delete self.Rpc;
delete self.store; delete self.store;
} }
});
});
chan.on('CONNECT', function (cfg, cb) {
debug('SW Connect callback');
if (self.store) {
debug('Store already exists!');
if (cfg.driveEvents) {
Rpc._subscribeToDrive(clientId);
}
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) { chan.on('CONNECT', function (cfg, cb) {
cb = cb || function () {}; debug('SW Connect callback');
Object.keys(self.tabs).forEach(function (cId) { if (self.store) {
if (excludes.indexOf(cId) !== -1) { return; } debug('Store already exists!');
if (cfg.driveEvents) {
Rpc._subscribeToDrive(clientId);
}
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) { self.tabs[cId].chan.query(cmd, data, function (err, data2) {
if (err) { return void cb({error: err}); } if (err) { return void cb({error: err}); }
cb(data2); 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);
}
if (data && data.state === "ALREADY_INIT") {
return void cb(data.returned);
}
self.store = data;
cb(data);
}); });
}); chan.on('JOIN_PAD', function (data, cb) {
chan.on('JOIN_PAD', function (data, cb) { self.tabs[clientId].channelId = data.channel;
self.tabs[clientId].channelId = data.channel; try {
try { Rpc.queries['JOIN_PAD'](clientId, data, cb);
Rpc.queries['JOIN_PAD'](clientId, data, cb); } catch (e) {
} catch (e) { console.error('Error in webworker when executing query JOIN_PAD');
console.error('Error in webworker when executing query JOIN_PAD'); console.error(e);
console.error(e); console.log(data);
console.log(data); }
} });
}); chan.on('SEND_PAD_MSG', function (msg, cb) {
chan.on('SEND_PAD_MSG', function (msg, cb) { var data = {
var data = { msg: msg,
msg: msg, channel: self.tabs[clientId].channelId
channel: self.tabs[clientId].channelId };
}; try {
try { Rpc.queries['SEND_PAD_MSG'](clientId, data, cb);
Rpc.queries['SEND_PAD_MSG'](clientId, data, cb); } catch (e) {
} catch (e) { console.error('Error in webworker when executing query SEND_PAD_MSG');
console.error('Error in webworker when executing query SEND_PAD_MSG'); console.error(e);
console.error(e); console.log(data);
console.log(data); }
} });
}); cb();
cb(); }, true);
}, true);
self.tabs[client.id].msgEv = msgEv; self.tabs[client.id].msgEv = msgEv;
self.tabs[client.id].close = function () { self.tabs[client.id].close = function () {
Rpc._removeClient(client.id); Rpc._removeClient(client.id);
}; };
});
}); });
}); });
}; };

View File

@ -19,128 +19,131 @@ var debug = function (msg) { console.log(msg); };
var init = function (client, cb) { var init = function (client, cb) {
debug('SharedW INIT'); debug('SharedW INIT');
require([ require(['/api/config?cb=' + (+new Date()).toString(16)], function (ApiConfig) {
'/common/requireconfig.js' if (ApiConfig.requireConf) { require.config(ApiConfig.requireConf); }
], function (RequireConfig) {
require.config(RequireConfig());
require([ require([
'/common/common-util.js', '/common/requireconfig.js'
'/common/outer/worker-channel.js', ], function (RequireConfig) {
'/common/outer/store-rpc.js' require.config(RequireConfig());
], function (Util, Channel, SRpc) { require([
debug('SharedW Required ressources loaded'); '/common/common-util.js',
var msgEv = Util.mkEvent(); '/common/outer/worker-channel.js',
'/common/outer/store-rpc.js'
], function (Util, Channel, SRpc) {
debug('SharedW Required ressources loaded');
var msgEv = Util.mkEvent();
if (!self.Rpc) { if (!self.Rpc) {
self.Rpc = SRpc(); self.Rpc = SRpc();
} }
var Rpc = self.Rpc; var Rpc = self.Rpc;
var postToClient = function (data) { var postToClient = function (data) {
postMsg(client, data); postMsg(client, data);
}; };
Channel.create(msgEv, postToClient, function (chan) { Channel.create(msgEv, postToClient, function (chan) {
debug('SharedW Channel created'); debug('SharedW Channel created');
var clientId = client.id; var clientId = client.id;
client.chan = chan; client.chan = chan;
Object.keys(Rpc.queries).forEach(function (q) { Object.keys(Rpc.queries).forEach(function (q) {
if (q === 'CONNECT') { return; } if (q === 'CONNECT') { return; }
if (q === 'JOIN_PAD') { return; } if (q === 'JOIN_PAD') { return; }
if (q === 'SEND_PAD_MSG') { return; } if (q === 'SEND_PAD_MSG') { return; }
chan.on(q, function (data, cb) { chan.on(q, function (data, cb) {
try { try {
Rpc.queries[q](clientId, data, cb); Rpc.queries[q](clientId, data, cb);
} catch (e) { } catch (e) {
console.error('Error in webworker when executing query ' + q); console.error('Error in webworker when executing query ' + q);
console.error(e); console.error(e);
console.log(data); console.log(data);
} }
if (q === "DISCONNECT") { if (q === "DISCONNECT") {
console.log('Deleting existing store!'); console.log('Deleting existing store!');
delete self.Rpc; delete self.Rpc;
delete self.store; delete self.store;
} }
});
});
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);
}
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) { chan.on('CONNECT', function (cfg, cb) {
cb = cb || function () {}; debug('SharedW connecting to store...');
Object.keys(self.tabs).forEach(function (cId) { if (self.store) {
if (excludes.indexOf(cId) !== -1) { return; } debug('Store already exists!');
if (cfg.driveEvents) {
Rpc._subscribeToDrive(clientId);
}
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) { self.tabs[cId].chan.query(cmd, data, function (err, data2) {
if (err) { return void cb({error: err}); } if (err) { return void cb({error: err}); }
cb(data2); 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);
}
if (data && data.state === "ALREADY_INIT") {
self.store = data.returned;
return void cb(data.returned);
}
self.store = data;
cb(data);
}); });
}); chan.on('JOIN_PAD', function (data, cb) {
chan.on('JOIN_PAD', function (data, cb) { client.channelId = data.channel;
client.channelId = data.channel; try {
try { Rpc.queries['JOIN_PAD'](clientId, data, cb);
Rpc.queries['JOIN_PAD'](clientId, data, cb); } catch (e) {
} catch (e) { console.error('Error in webworker when executing query JOIN_PAD');
console.error('Error in webworker when executing query JOIN_PAD'); console.error(e);
console.error(e); console.log(data);
console.log(data); }
} });
}); chan.on('SEND_PAD_MSG', function (msg, cb) {
chan.on('SEND_PAD_MSG', function (msg, cb) { var data = {
var data = { msg: msg,
msg: msg, channel: client.channelId
channel: client.channelId };
}; try {
try { Rpc.queries['SEND_PAD_MSG'](clientId, data, cb);
Rpc.queries['SEND_PAD_MSG'](clientId, data, cb); } catch (e) {
} catch (e) { console.error('Error in webworker when executing query SEND_PAD_MSG');
console.error('Error in webworker when executing query SEND_PAD_MSG'); console.error(e);
console.error(e); console.log(data);
console.log(data); }
} });
}); cb();
cb(); }, true);
}, true);
client.msgEv = msgEv; client.msgEv = msgEv;
client.close = function () { client.close = function () {
Rpc._removeClient(client.id); Rpc._removeClient(client.id);
}; };
});
}); });
}); });
}; };

View File

@ -7,94 +7,97 @@ localStorage = {
getItem: function (k) { return localStorage[k]; } getItem: function (k) { return localStorage[k]; }
}; };
require([ require(['/api/config?cb=' + (+new Date()).toString(16)], function (ApiConfig) {
'/common/requireconfig.js' if (ApiConfig.requireConf) { require.config(ApiConfig.requireConf); }
], function (RequireConfig) {
require.config(RequireConfig());
require([ require([
'/common/common-util.js', '/common/requireconfig.js'
'/common/outer/worker-channel.js', ], function (RequireConfig) {
'/common/outer/store-rpc.js' require.config(RequireConfig());
], function (Util, Channel, SRpc) { require([
var msgEv = Util.mkEvent(); '/common/common-util.js',
'/common/outer/worker-channel.js',
'/common/outer/store-rpc.js'
], function (Util, Channel, SRpc) {
var msgEv = Util.mkEvent();
var Rpc = SRpc(); var Rpc = SRpc();
Channel.create(msgEv, postMessage, function (chan) { Channel.create(msgEv, postMessage, function (chan) {
var clientId = '1'; var clientId = '1';
Object.keys(Rpc.queries).forEach(function (q) { Object.keys(Rpc.queries).forEach(function (q) {
if (q === 'CONNECT') { return; } if (q === 'CONNECT') { return; }
if (q === 'JOIN_PAD') { return; } if (q === 'JOIN_PAD') { return; }
if (q === 'SEND_PAD_MSG') { return; } if (q === 'SEND_PAD_MSG') { return; }
chan.on(q, function (data, cb) { 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) {
// load Store here, with cfg, and pass a "query" (chan.query)
// cId is a clientId used in ServiceWorker or SharedWorker
cfg.query = function (cId, cmd, data, cb) {
cb = cb || function () {};
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 () {};
if (excludes.indexOf(clientId) !== -1) { return; }
chan.query(cmd, data, function (err, data2) {
if (err) { return void cb({error: err}); }
cb(data2);
});
};
Rpc.queries['CONNECT'](clientId, cfg, function (data) {
if (data && data.state === "ALREADY_INIT") {
return void cb(data);
}
if (cfg.driveEvents) {
Rpc._subscribeToDrive(clientId);
}
if (cfg.messenger) {
Rpc._subscribeToMessenger(clientId);
}
cb(data);
});
});
var chanId;
chan.on('JOIN_PAD', function (data, cb) {
chanId = data.channel;
try { try {
Rpc.queries[q](clientId, data, cb); Rpc.queries['JOIN_PAD'](clientId, data, cb);
} catch (e) { } catch (e) {
console.error('Error in webworker when executing query ' + q); console.error('Error in webworker when executing query JOIN_PAD');
console.error(e); console.error(e);
console.log(data); console.log(data);
} }
}); });
}); chan.on('SEND_PAD_MSG', function (msg, cb) {
chan.on('CONNECT', function (cfg, cb) { var data = {
// load Store here, with cfg, and pass a "query" (chan.query) msg: msg,
// cId is a clientId used in ServiceWorker or SharedWorker channel: chanId
cfg.query = function (cId, cmd, data, cb) { };
cb = cb || function () {}; try {
chan.query(cmd, data, function (err, data2) { Rpc.queries['SEND_PAD_MSG'](clientId, data, cb);
if (err) { return void cb({error: err}); } } catch (e) {
cb(data2); console.error('Error in webworker when executing query SEND_PAD_MSG');
}); console.error(e);
}; console.log(data);
cfg.broadcast = function (excludes, cmd, data, cb) {
cb = cb || function () {};
if (excludes.indexOf(clientId) !== -1) { return; }
chan.query(cmd, data, function (err, data2) {
if (err) { return void cb({error: err}); }
cb(data2);
});
};
Rpc.queries['CONNECT'](clientId, cfg, function (data) {
if (data && data.state === "ALREADY_INIT") {
return void cb(data);
} }
if (cfg.driveEvents) {
Rpc._subscribeToDrive(clientId);
}
if (cfg.messenger) {
Rpc._subscribeToMessenger(clientId);
}
cb(data);
}); });
}); }, true);
var chanId;
chan.on('JOIN_PAD', function (data, cb) {
chanId = 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: chanId
};
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);
}
});
}, true);
onmessage = function (e) { onmessage = function (e) {
msgEv.fire(e); msgEv.fire(e);
}; };
});
}); });
}); });