Fix race condition and infinite loading screen in offline mode

pull/1/head
yflory 4 years ago
parent 82624c10a5
commit 2928cd2c7e

@ -313,9 +313,30 @@ button:not(.btn).primary:hover{
}; };
var hasErrored = false; var hasErrored = false;
var isOffline = false;
var updateLoadingProgress = function (data) { var updateLoadingProgress = function (data) {
if (!built || !data) { return; } if (!built || !data) { return; }
// If we receive a "offline" event, show the warning text
if (data.type === "offline") {
try {
isOffline = true;
Messages.offlineError = "OFFLINE MODE NOT AVAILABLE"; // XXX
document.querySelector('#cp-loading-message').setAttribute('style', 'display:block;');
document.querySelector('#cp-loading-message').innerText = Messages.offlineError;
} catch (e) { console.error(e); }
return;
}
// If we receive a new event and we were offline, remove
// the offline warning text
if (isOffline) {
try {
isOffline = false;
document.querySelector('#cp-loading-message').setAttribute('style', 'display:none;');
} catch (e) { console.error(e); }
}
// Make sure progress doesn't go backward // Make sure progress doesn't go backward
var c = types.indexOf(data.type); var c = types.indexOf(data.type);
if (c < current) { return console.debug(data); } if (c < current) { return console.debug(data); }

@ -492,23 +492,38 @@ define([
}); });
}; };
// This function is used when we want to open a pad. We first need
// to check if it exists. With the cached drive, we need to wait for
// the network to be available before we can continue.
common.isNewChannel = function (href, password, _cb) { common.isNewChannel = function (href, password, _cb) {
var cb = Util.once(Util.mkAsync(_cb)); var cb = Util.once(Util.mkAsync(_cb));
var channel = Hash.hrefToHexChannelId(href, password); var channel = Hash.hrefToHexChannelId(href, password);
var error; var error;
var inCache = false;
Nthen(function (waitFor) { Nthen(function (waitFor) {
// If it's not in the cache or it's not a blob, try to get the value from the server Cache.getChannelCache(channel, waitFor(function(err, data) {
postMessage('IS_NEW_CHANNEL', {channel: channel}, waitFor(function (obj) { if (err || !data) { return; }
if (obj && obj.error) { error = obj.error; return; }
if (!obj) { error = "INVALID_RESPONSE"; return; }
waitFor.abort(); waitFor.abort();
cb(undefined, obj.isNew); cb(undefined, false);
})); }));
}).nThen(function () { }).nThen(function () {
Cache.getChannelCache(channel, function(err, data) { // If it's not in the cache try to get the value from the server
if (err || !data) { return void cb(error); } var isNew = function () {
cb(null, false); error = undefined;
}); postMessage('IS_NEW_CHANNEL', {channel: channel}, function (obj) {
if (obj && obj.error) { error = obj.error; }
if (!obj) { error = "INVALID_RESPONSE"; }
if (error === "ANON_RPC_NOT_READY") {
// Try again in 1s
return void setTimeout(isNew, 100);
} else if (error) {
return void cb(error);
}
cb(undefined, obj.isNew);
}, {timeout: -1});
};
isNew();
}); });
}; };

@ -2762,6 +2762,22 @@ define([
store.realtime = info.realtime; store.realtime = info.realtime;
store.networkPromise = info.networkPromise; store.networkPromise = info.networkPromise;
store.cacheReturned = returned; store.cacheReturned = returned;
// Check if we can connect
var to = setTimeout(function () {
console.error('TO');
broadcast([], "LOADING_DRIVE", {
type: "offline"
});
}, 5000);
store.networkPromise.then(function () {
clearTimeout(to);
}, function (err) {
console.error(err);
clearTimeout(to);
});
if (!data.cache) { return; } if (!data.cache) { return; }
// Make sure we have a valid user object before emitting cacheready // Make sure we have a valid user object before emitting cacheready

Loading…
Cancel
Save