Fix race conditions and multiple tabs on the same worker

This commit is contained in:
yflory 2021-01-08 15:13:45 +01:00
parent e2969f5ac3
commit 181a4efd8b
4 changed files with 39 additions and 29 deletions

View file

@ -35,6 +35,7 @@ define([
Crypto, ChainPad, CpNetflux, Listmap, nThen, Saferphore) { Crypto, ChainPad, CpNetflux, Listmap, nThen, Saferphore) {
var onReadyEvt = Util.mkEvent(true); var onReadyEvt = Util.mkEvent(true);
var onCacheReadyEvt = Util.mkEvent(true);
// Default settings for new users // Default settings for new users
var NEW_USER_SETTINGS = { var NEW_USER_SETTINGS = {
@ -2651,7 +2652,8 @@ define([
Feedback.init(returned.feedback); Feedback.init(returned.feedback);
// "cb" may have already been called by onCacheReady // "cb" may have already been called by onCacheReady
if (typeof(cb) === 'function') { cb(returned); } store.returned = returned;
if (typeof(cb) === 'function') { cb(); }
store.offline = false; store.offline = false;
sendDriveEvent('NETWORK_RECONNECT'); // Tell inner that we're now online sendDriveEvent('NETWORK_RECONNECT'); // Tell inner that we're now online
@ -2766,6 +2768,7 @@ define([
// Check if we can connect // Check if we can connect
var to = setTimeout(function () { var to = setTimeout(function () {
console.error('TO'); console.error('TO');
store.networkTimeout = true;
broadcast([], "LOADING_DRIVE", { broadcast([], "LOADING_DRIVE", {
type: "offline" type: "offline"
}); });
@ -2785,8 +2788,10 @@ define([
onCacheReady(clientId, function () { onCacheReady(clientId, function () {
if (typeof(cb) === "function") { cb(returned); } if (typeof(cb) === "function") { cb(returned); }
onCacheReadyEvt.fire();
}); });
}).on('ready', function (info) { }).on('ready', function (info) {
delete store.networkTimeout;
if (store.ready) { return; } // the store is already ready, it is a reconnection if (store.ready) { return; } // the store is already ready, it is a reconnection
store.driveMetadata = info.metadata; store.driveMetadata = info.metadata;
if (!rt.proxy.drive || typeof(rt.proxy.drive) !== 'object') { rt.proxy.drive = {}; } if (!rt.proxy.drive || typeof(rt.proxy.drive) !== 'object') { rt.proxy.drive = {}; }
@ -2875,15 +2880,26 @@ define([
Store.init = function (clientId, data, _callback) { Store.init = function (clientId, data, _callback) {
var callback = Util.once(_callback); var callback = Util.once(_callback);
if (!store.returned && data.cache && store.cacheReturned) {
return void onCacheReady(clientId, function () { // If this is not the first tab and we're offline, callback only if the app
// supports offline mode
if (initialized && !store.returned && data.cache) {
return void onCacheReadyEvt.reg(function () {
callback({ callback({
state: 'ALREADY_INIT', state: 'ALREADY_INIT',
returned: store.cacheReturned returned: store.cacheReturned
}); });
}); });
} }
// If this is not the first tab (initialized is true), it means either we don't
// support offline or we're already online
if (initialized) { if (initialized) {
if (store.networkTimeout) {
postMessage(clientId, "LOADING_DRIVE", {
type: "offline"
});
}
return void whenReady(function () { return void whenReady(function () {
callback({ callback({
state: 'ALREADY_INIT', state: 'ALREADY_INIT',
@ -2906,8 +2922,6 @@ define([
} }
if (ret && ret.error) { if (ret && ret.error) {
initialized = false; initialized = false;
} else {
store.returned = ret;
} }
callback(ret); callback(ret);

View file

@ -73,6 +73,7 @@ var init = function (client, cb) {
}); });
chan.on('CONNECT', function (cfg, cb) { chan.on('CONNECT', function (cfg, cb) {
debug('SharedW connecting to store...'); debug('SharedW connecting to store...');
/*
if (self.store) { if (self.store) {
debug('Store already exists!'); debug('Store already exists!');
if (cfg.driveEvents) { if (cfg.driveEvents) {
@ -80,31 +81,35 @@ var init = function (client, cb) {
} }
return void cb(self.store); return void cb(self.store);
} }
*/
debug('Loading new async store'); if (!self.store) {
// One-time initialization (init async-store) debug('Loading new async store');
cfg.query = function (cId, cmd, data, cb) { // One-time initialization (init async-store)
cb = cb || function () {}; cfg.query = function (cId, cmd, data, cb) {
self.tabs[cId].chan.query(cmd, data, function (err, data2) { cb = cb || function () {};
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) { 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) { Rpc.queries['CONNECT'](clientId, cfg, function (data) {
if (cfg.driveEvents) { if (cfg.driveEvents) {
Rpc._subscribeToDrive(clientId); Rpc._subscribeToDrive(clientId);
} }
if (data && data.state === "ALREADY_INIT") { if (data && data.state === "ALREADY_INIT") {
debug('Store already exists!');
self.store = data.returned; self.store = data.returned;
return void cb(data.returned); return void cb(data.returned);
} }

View file

@ -498,11 +498,6 @@ define([
// We've received a link without /p/ and it doesn't work without a password: abort // We've received a link without /p/ and it doesn't work without a password: abort
return void todo(); return void todo();
} }
if (e === "ANON_RPC_NOT_READY") {
// We're currently offline and the pad is not in our cache
w.abort();
return void sframeChan.event('EV_OFFLINE');
}
// Wrong password or deleted file? // Wrong password or deleted file?
askPassword(true, passwordCfg); askPassword(true, passwordCfg);
})); }));

View file

@ -772,10 +772,6 @@ define([
UI.errorLoadingScreen(Messages.restrictedError); UI.errorLoadingScreen(Messages.restrictedError);
}); });
ctx.sframeChan.on("EV_OFFLINE", function () {
UI.errorLoadingScreen(Messages.offlineNoCacheError);
});
ctx.sframeChan.on("EV_PAD_PASSWORD_ERROR", function () { ctx.sframeChan.on("EV_PAD_PASSWORD_ERROR", function () {
UI.errorLoadingScreen(Messages.password_error_seed); UI.errorLoadingScreen(Messages.password_error_seed);
}); });