Hopefully fix infinite spinner whenever there is a disconnect (pad)

This commit is contained in:
Caleb James DeLisle
2017-09-11 15:46:21 +02:00
parent 684a12ce2e
commit 7334173b4a
10 changed files with 128 additions and 43 deletions

View File

@@ -15,11 +15,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
define([
'/common/common-util.js',
'/customize/application_config.js',
'/bower_components/chainpad/chainpad.dist.js'
], function () {
], function (Util, AppConfig) {
var ChainPad = window.ChainPad;
var module = { exports: {} };
var badStateTimeout = typeof(AppConfig.badStateTimeout) === 'number' ?
AppConfig.badStateTimeout : 30000;
var verbose = function (x) { console.log(x); };
verbose = function () {}; // comment out to enable verbose logging
@@ -44,9 +49,25 @@ define([
var chainpad;
var myID;
var isReady = false;
var evConnected = Util.mkEvent(true);
var evInfiniteSpinner = Util.mkEvent(true);
window.setInterval(function () {
if (!chainpad || !myID) { return; }
var l;
try {
l = chainpad.getLag();
} catch (e) {
throw new Error("ChainPad.getLag() does not exist, please `bower update`");
}
if (l.lag < badStateTimeout) { return; }
chainpad.abort();
evInfiniteSpinner.fire();
}, 2000);
sframeChan.on('EV_RT_DISCONNECT', function () {
isReady = false;
if (chainpad) { chainpad.abort(); }
onConnectionChange({ state: false });
});
sframeChan.on('EV_RT_CONNECT', function (content) {
@@ -55,6 +76,7 @@ define([
isReady = false;
if (chainpad) {
// it's a reconnect
if (chainpad) { chainpad.start(); }
onConnectionChange({ state: true, myId: myID });
return;
}
@@ -77,6 +99,7 @@ define([
realtime: chainpad,
readOnly: readOnly
});
evConnected.fire();
});
sframeChan.on('Q_RT_MESSAGE', function (content, cb) {
if (isReady) {
@@ -92,9 +115,22 @@ define([
setMyID({ myID: myID });
onReady({ realtime: chainpad });
});
var whenRealtimeSyncs = function (cb) {
evConnected.reg(function () {
if (chainpad.getAuthDoc() === chainpad.getUserDoc()) {
return void cb();
} else {
chainpad.onSettle(cb);
}
});
};
return Object.freeze({
getMyID: function () { return myID; },
metadataMgr: metadataMgr
metadataMgr: metadataMgr,
whenRealtimeSyncs: whenRealtimeSyncs,
onInfiniteSpinner: evInfiniteSpinner.reg
});
};
return Object.freeze(module.exports);