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

pull/1/head
Caleb James DeLisle 7 years ago
parent 684a12ce2e
commit 7334173b4a

@ -19,14 +19,16 @@ define([
if (typeof(realtime.getAuthDoc) !== 'function') { if (typeof(realtime.getAuthDoc) !== 'function') {
return void console.error('improper use of this function'); return void console.error('improper use of this function');
} }
window.setTimeout(function () { window.setTimeout(function () {
if (realtime.getAuthDoc() === realtime.getUserDoc()) { if (realtime.getAuthDoc() === realtime.getUserDoc()) {
return void cb(); return void cb();
} else { } else {
realtime.onSettle(cb); realtime.onSettle(cb);
} }
}, 0);
};
common.beginDetectingInfiniteSpinner = function (Cryptpad, realtime) {
if (intr) { return; } if (intr) { return; }
intr = window.setInterval(function () { intr = window.setInterval(function () {
var l; var l;
@ -48,7 +50,6 @@ define([
}); });
common.infiniteSpinnerDetected = true; common.infiniteSpinnerDetected = true;
}, 2000); }, 2000);
}, 0);
}; };
common.onInfiniteSpinner = function (f) { infiniteSpinnerHandlers.push(f); }; common.onInfiniteSpinner = function (f) { infiniteSpinnerHandlers.push(f); };

@ -1,6 +1,28 @@
define([], function () { define([], function () {
var Util = {}; var Util = {};
// If once is true, after the event has been fired, any further handlers which are
// registered will fire immediately, and this type of event cannot be fired twice.
Util.mkEvent = function (once) {
var handlers = [];
var fired = false;
return {
reg: function (cb) {
if (once && fired) { return void setTimeout(cb); }
handlers.push(cb);
},
unreg: function (cb) {
if (handlers.indexOf(cb) === -1) { throw new Error("Not registered"); }
handlers.splice(handlers.indexOf(cb), 1);
},
fire: function () {
if (fired) { return; }
fired = true;
handlers.forEach(function (h) { h(); });
}
};
};
Util.find = function (map, path) { Util.find = function (map, path) {
return (map && path.reduce(function (p, n) { return (map && path.reduce(function (p, n) {
return typeof(p[n]) !== 'undefined' && p[n]; return typeof(p[n]) !== 'undefined' && p[n];

@ -138,6 +138,10 @@ define([
Realtime.whenRealtimeSyncs(common, realtime, cb); Realtime.whenRealtimeSyncs(common, realtime, cb);
}; };
common.beginDetectingInfiniteSpinner = function (realtime) {
Realtime.beginDetectingInfiniteSpinner(common, realtime);
};
// Userlist // Userlist
common.createUserList = UserList.create; common.createUserList = UserList.create;

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

@ -298,6 +298,14 @@ define([
Cryptpad.useTemplate(href, Cryptget, cb); Cryptpad.useTemplate(href, Cryptget, cb);
}); });
sframeChan.on('EV_GOTO_URL', function (url) {
if (url) {
window.location.href = url;
} else {
window.location.reload();
}
});
CpNfOuter.start({ CpNfOuter.start({
sframeChan: sframeChan, sframeChan: sframeChan,
channel: secret.channel, channel: secret.channel,

@ -12,9 +12,10 @@ define([
'/customize/application_config.js', '/customize/application_config.js',
'/common/cryptpad-common.js', '/common/cryptpad-common.js',
'/common/common-realtime.js' '/common/common-realtime.js',
'/common/common-util.js'
], function ($, nThen, Messages, CpNfInner, SFrameChannel, Title, UI, History, File, MetadataMgr, ], function ($, nThen, Messages, CpNfInner, SFrameChannel, Title, UI, History, File, MetadataMgr,
AppConfig, Cryptpad, CommonRealtime) { AppConfig, Cryptpad, CommonRealtime, Util) {
// Chainpad Netflux Inner // Chainpad Netflux Inner
var funcs = {}; var funcs = {};
@ -22,12 +23,15 @@ define([
funcs.Messages = Messages; funcs.Messages = Messages;
var evRealtimeSynced = Util.mkEvent(true);
funcs.startRealtime = function (options) { funcs.startRealtime = function (options) {
if (ctx.cpNfInner) { return ctx.cpNfInner; } if (ctx.cpNfInner) { return ctx.cpNfInner; }
options.sframeChan = ctx.sframeChan; options.sframeChan = ctx.sframeChan;
options.metadataMgr = ctx.metadataMgr; options.metadataMgr = ctx.metadataMgr;
ctx.cpNfInner = CpNfInner.start(options); ctx.cpNfInner = CpNfInner.start(options);
ctx.cpNfInner.metadataMgr.onChangeLazy(options.onLocal); ctx.cpNfInner.metadataMgr.onChangeLazy(options.onLocal);
ctx.cpNfInner.whenRealtimeSyncs(function () { evRealtimeSynced.fire(); });
return ctx.cpNfInner; return ctx.cpNfInner;
}; };
@ -197,6 +201,10 @@ define([
}); });
}; */ }; */
funcs.gotoURL = function (url) { ctx.sframeChan.event('EV_GOTO_URL', url); };
funcs.whenRealtimeSyncs = evRealtimeSynced.reg;
Object.freeze(funcs); Object.freeze(funcs);
return { create: function (cb) { return { create: function (cb) {
nThen(function (waitFor) { nThen(function (waitFor) {

@ -109,4 +109,7 @@ define({
'Q_UPLOAD_FILE': true, 'Q_UPLOAD_FILE': true,
'EV_FILE_UPLOAD_STATE': true, 'EV_FILE_UPLOAD_STATE': true,
'Q_CANCEL_PENDING_FILE_UPLOAD': true, 'Q_CANCEL_PENDING_FILE_UPLOAD': true,
// Make the browser window navigate to a given URL, if no URL is passed then it will reload.
'EV_GOTO_URL': true,
}); });

@ -711,6 +711,7 @@ define([
}, local ? 0 : SPINNER_DISAPPEAR_TIME); }, local ? 0 : SPINNER_DISAPPEAR_TIME);
}; };
if (Cryptpad) { if (Cryptpad) {
Cryptpad.beginDetectingInfiniteSpinner(config.realtime);
Cryptpad.whenRealtimeSyncs(config.realtime, onSynced); Cryptpad.whenRealtimeSyncs(config.realtime, onSynced);
return; return;
} }
@ -733,6 +734,9 @@ define([
// receive a patch. // receive a patch.
if (Cryptpad) { if (Cryptpad) {
typing = 0; typing = 0;
// We're just placing this detector here because it used to be triggered by
// whenRealtimeSyncs() and now it is not because in sframe it is handled differently.
Cryptpad.beginDetectingInfiniteSpinner(config.realtime);
Cryptpad.whenRealtimeSyncs(config.realtime, function () { Cryptpad.whenRealtimeSyncs(config.realtime, function () {
kickSpinner(toolbar, config); kickSpinner(toolbar, config);
}); });

@ -673,11 +673,7 @@ define([
$spin.text(Messages.saved); $spin.text(Messages.saved);
}, local ? 0 : SPINNER_DISAPPEAR_TIME); }, local ? 0 : SPINNER_DISAPPEAR_TIME);
}; };
if (Cryptpad) { config.sfCommon.whenRealtimeSyncs(onSynced);
Cryptpad.whenRealtimeSyncs(config.realtime, onSynced);
return;
}
onSynced();
}; };
var ks = function (toolbar, config, local) { var ks = function (toolbar, config, local) {
return function () { return function () {
@ -694,12 +690,10 @@ define([
} }
// without this, users in read-only mode say 'synchronizing' until they // without this, users in read-only mode say 'synchronizing' until they
// receive a patch. // receive a patch.
if (Cryptpad) {
typing = 0; typing = 0;
Cryptpad.whenRealtimeSyncs(config.realtime, function () { config.sfCommon.whenRealtimeSyncs(function () {
kickSpinner(toolbar, config); kickSpinner(toolbar, config);
}); });
}
return $spin; return $spin;
}; };

@ -33,7 +33,6 @@ define([
'/bower_components/nthen/index.js', '/bower_components/nthen/index.js',
'/common/sframe-common.js', '/common/sframe-common.js',
'/api/config', '/api/config',
'/common/common-realtime.js',
'/bower_components/file-saver/FileSaver.min.js', '/bower_components/file-saver/FileSaver.min.js',
'/bower_components/diff-dom/diffDOM.js', '/bower_components/diff-dom/diffDOM.js',
@ -56,8 +55,7 @@ define([
Links, Links,
nThen, nThen,
SFCommon, SFCommon,
ApiConfig, ApiConfig)
CommonRealtime)
{ {
var saveAs = window.saveAs; var saveAs = window.saveAs;
var Messages = Cryptpad.Messages; var Messages = Cryptpad.Messages;
@ -339,8 +337,6 @@ define([
} }
}; };
CommonRealtime.onInfiniteSpinner(function () { setEditable(false); });
// don't let the user edit until the pad is ready // don't let the user edit until the pad is ready
setEditable(false); setEditable(false);
@ -717,6 +713,15 @@ define([
cpNfInner = common.startRealtime(realtimeOptions); cpNfInner = common.startRealtime(realtimeOptions);
metadataMgr = cpNfInner.metadataMgr; metadataMgr = cpNfInner.metadataMgr;
cpNfInner.onInfiniteSpinner(function () {
setEditable(false);
Cryptpad.confirm(Messages.realtime_unrecoverableError, function (yes) {
if (!yes) { return; }
common.gotoURL();
//window.parent.location.reload();
});
});
Cryptpad.onLogout(function () { setEditable(false); }); Cryptpad.onLogout(function () { setEditable(false); });
/* hitting enter makes a new line, but places the cursor inside /* hitting enter makes a new line, but places the cursor inside

Loading…
Cancel
Save