Fix race condition and infinite loading screen in offline mode

This commit is contained in:
yflory 2021-01-08 14:33:05 +01:00
parent 82624c10a5
commit 2928cd2c7e
3 changed files with 61 additions and 9 deletions

View File

@ -313,9 +313,30 @@ button:not(.btn).primary:hover{
};
var hasErrored = false;
var isOffline = false;
var updateLoadingProgress = function (data) {
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
var c = types.indexOf(data.type);
if (c < current) { return console.debug(data); }

View File

@ -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) {
var cb = Util.once(Util.mkAsync(_cb));
var channel = Hash.hrefToHexChannelId(href, password);
var error;
var inCache = false;
Nthen(function (waitFor) {
// If it's not in the cache or it's not a blob, try to get the value from the server
postMessage('IS_NEW_CHANNEL', {channel: channel}, waitFor(function (obj) {
if (obj && obj.error) { error = obj.error; return; }
if (!obj) { error = "INVALID_RESPONSE"; return; }
Cache.getChannelCache(channel, waitFor(function(err, data) {
if (err || !data) { return; }
waitFor.abort();
cb(undefined, obj.isNew);
cb(undefined, false);
}));
}).nThen(function () {
Cache.getChannelCache(channel, function(err, data) {
if (err || !data) { return void cb(error); }
cb(null, false);
});
// If it's not in the cache try to get the value from the server
var isNew = function () {
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();
});
};

View File

@ -2762,6 +2762,22 @@ define([
store.realtime = info.realtime;
store.networkPromise = info.networkPromise;
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; }
// Make sure we have a valid user object before emitting cacheready