cryptpad/www/common/common-realtime.js

63 lines
2.1 KiB
JavaScript
Raw Normal View History

2017-08-11 08:59:54 +00:00
define([
'/customize/application_config.js',
'/customize/messages.js',
], function (AppConfig, Messages) {
var common = {};
common.infiniteSpinnerDetected = false;
var BAD_STATE_TIMEOUT = typeof(AppConfig.badStateTimeout) === 'number'?
AppConfig.badStateTimeout: 30000;
var connected = false;
var intr;
var infiniteSpinnerHandlers = [];
2017-08-11 08:59:54 +00:00
/*
TODO make this not blow up when disconnected or lagging...
*/
2017-08-23 09:06:55 +00:00
common.whenRealtimeSyncs = function (Cryptpad, realtime, cb) {
if (typeof(realtime.getAuthDoc) !== 'function') {
return void console.error('improper use of this function');
}
2017-08-11 08:59:54 +00:00
window.setTimeout(function () {
if (realtime.getAuthDoc() === realtime.getUserDoc()) {
return void cb();
} else {
realtime.onSettle(cb);
2017-08-11 08:59:54 +00:00
}
if (intr) { return; }
intr = window.setInterval(function () {
var l;
try {
l = realtime.getLag();
} catch (e) {
throw new Error("ChainPad.getLag() does not exist, please `bower update`");
}
if (l.lag < BAD_STATE_TIMEOUT || !connected) { return; }
2017-08-11 08:59:54 +00:00
realtime.abort();
// don't launch more than one popup
if (common.infiniteSpinnerDetected) { return; }
infiniteSpinnerHandlers.forEach(function (ish) { ish(); });
2017-08-11 08:59:54 +00:00
// inform the user their session is in a bad state
2017-08-23 09:06:55 +00:00
Cryptpad.confirm(Messages.realtime_unrecoverableError, function (yes) {
2017-08-11 08:59:54 +00:00
if (!yes) { return; }
window.parent.location.reload();
2017-08-11 08:59:54 +00:00
});
common.infiniteSpinnerDetected = true;
}, 2000);
2017-08-11 08:59:54 +00:00
}, 0);
};
common.onInfiniteSpinner = function (f) { infiniteSpinnerHandlers.push(f); };
common.setConnectionState = function (bool) {
if (typeof(bool) !== 'boolean') { return; }
connected = bool;
};
2017-08-11 08:59:54 +00:00
return common;
});