diff --git a/www/common/common-realtime.js b/www/common/common-realtime.js index abf0e1cca..216635514 100644 --- a/www/common/common-realtime.js +++ b/www/common/common-realtime.js @@ -9,23 +9,33 @@ define([ AppConfig.badStateTimeout: 30000; var connected = false; + var intr; + var infiniteSpinnerHandlers = []; /* TODO make this not blow up when disconnected or lagging... */ common.whenRealtimeSyncs = function (Cryptpad, realtime, cb) { - realtime.sync(); - window.setTimeout(function () { if (realtime.getAuthDoc() === realtime.getUserDoc()) { return void cb(); + } else { + realtime.onSettle(cb); } - var to = setTimeout(function () { - if (!connected) { return; } + 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; } realtime.abort(); // don't launch more than one popup if (common.infiniteSpinnerDetected) { return; } + infiniteSpinnerHandlers.forEach(function (ish) { ish(); }); // inform the user their session is in a bad state Cryptpad.confirm(Messages.realtime_unrecoverableError, function (yes) { @@ -33,14 +43,12 @@ define([ window.location.reload(); }); common.infiniteSpinnerDetected = true; - }, BAD_STATE_TIMEOUT); - realtime.onSettle(function () { - clearTimeout(to); - cb(); - }); + }, 2000); }, 0); }; + common.onInfiniteSpinner = function (f) { infiniteSpinnerHandlers.push(f); }; + common.setConnectionState = function (bool) { if (typeof(bool) !== 'boolean') { return; } connected = bool; diff --git a/www/common/sframe-chainpad-netflux-inner.js b/www/common/sframe-chainpad-netflux-inner.js index a1359f522..04659fe17 100644 --- a/www/common/sframe-chainpad-netflux-inner.js +++ b/www/common/sframe-chainpad-netflux-inner.js @@ -67,9 +67,7 @@ define([ logLevel: logLevel }); chainpad.onMessage(function(message, cb) { - sframeChan.query('Q_RT_MESSAGE', message, function (ret) { - if (ret === 'OK') { cb(); } - }); + sframeChan.query('Q_RT_MESSAGE', message, cb); }); chainpad.onPatch(function () { onRemote({ realtime: chainpad }); diff --git a/www/common/sframe-chainpad-netflux-outer.js b/www/common/sframe-chainpad-netflux-outer.js index ff6e522c8..54688c2e3 100644 --- a/www/common/sframe-chainpad-netflux-outer.js +++ b/www/common/sframe-chainpad-netflux-outer.js @@ -155,13 +155,14 @@ define([], function () { // want to keep the same chainpad (realtime) object try { wcObject.wc.bcast(message).then(function() { - cb('OK'); + cb(); }, function(err) { // The message has not been sent, display the error. console.error(err); }); } catch (e) { - cb('ERROR'); + console.log(e); + // Just skip calling back and it will fail on the inside. } } }; @@ -214,6 +215,7 @@ define([], function () { }; network.on('disconnect', function (reason) { + console.log('disconnect'); if (isIntentionallyLeaving) { return; } if (reason === "network.disconnect() called") { return; } sframeChan.event('EV_RT_DISCONNECT'); diff --git a/www/common/sframe-common.js b/www/common/sframe-common.js index ae1c9a6ae..8c0b07a15 100644 --- a/www/common/sframe-common.js +++ b/www/common/sframe-common.js @@ -10,9 +10,10 @@ define([ '/common/metadata-manager.js', '/customize/application_config.js', - '/common/cryptpad-common.js' + '/common/cryptpad-common.js', + '/common/common-realtime.js' ], function ($, nThen, Messages, CpNfInner, SFrameChannel, Title, UI, History, MetadataMgr, - AppConfig, Cryptpad) { + AppConfig, Cryptpad, CommonRealtime) { // Chainpad Netflux Inner var funcs = {}; @@ -292,6 +293,10 @@ define([ if (titleUpdated) { titleUpdated(undefined, title); } }); }); + + ctx.sframeChan.on('EV_RT_CONNECT', function () { CommonRealtime.setConnectionState(true); }); + ctx.sframeChan.on('EV_RT_DISCONNECT', function () { CommonRealtime.setConnectionState(false); }); + cb(funcs); }); } }; diff --git a/www/pad2/main.js b/www/pad2/main.js index 497082f1f..903fb0330 100644 --- a/www/pad2/main.js +++ b/www/pad2/main.js @@ -33,6 +33,7 @@ define([ '/bower_components/nthen/index.js', '/common/sframe-common.js', '/api/config', + '/common/common-realtime.js', '/bower_components/file-saver/FileSaver.min.js', '/bower_components/diff-dom/diffDOM.js', @@ -41,8 +42,24 @@ define([ 'css!/bower_components/components-font-awesome/css/font-awesome.min.css', 'less!/customize/src/less/cryptpad.less', 'less!/customize/src/less/toolbar.less' -], function ($, Crypto, Hyperjson, - Toolbar, Cursor, JsonOT, TypingTest, JSONSortify, TextPatcher, Cryptpad, Cryptget, Links, nThen, SFCommon, ApiConfig) { +], function ( + $, + Crypto, + Hyperjson, + Toolbar, + Cursor, + JsonOT, + TypingTest, + JSONSortify, + TextPatcher, + Cryptpad, + Cryptget, + Links, + nThen, + SFCommon, + ApiConfig, + CommonRealtime) +{ var saveAs = window.saveAs; var Messages = Cryptpad.Messages; var DiffDom = window.diffDOM; @@ -322,6 +339,8 @@ define([ } }; + CommonRealtime.onInfiniteSpinner(function () { setEditable(false); }); + // don't let the user edit until the pad is ready setEditable(false);