Merge branch 'reconnect' into staging

pull/1/head
yflory 5 years ago
commit e15196bd53

@ -325,7 +325,7 @@ const storeMessage = function (Env, channel, msg, isCp, optionalMessageHash) {
* it has a side-effect of filling the index cache if it's empty * it has a side-effect of filling the index cache if it's empty
1. if you provided a lastKnownHash and that message does not exist in the history: 1. if you provided a lastKnownHash and that message does not exist in the history:
* either the client has made a mistake or the history they knew about no longer exists * either the client has made a mistake or the history they knew about no longer exists
* call back with EINVAL * call back with EUNKNOWN
2. if you did not provide a lastKnownHash 2. if you did not provide a lastKnownHash
* and there are fewer than two checkpoints: * and there are fewer than two checkpoints:
* return 0 (read from the start of the file) * return 0 (read from the start of the file)
@ -360,7 +360,7 @@ const getHistoryOffset = (Env, channelName, lastKnownHash, _cb) => {
// QUESTION: does this mean mailboxes are causing the server to store too much stuff in memory? // QUESTION: does this mean mailboxes are causing the server to store too much stuff in memory?
if (lastKnownHash && typeof(lkh) !== "number") { if (lastKnownHash && typeof(lkh) !== "number") {
waitFor.abort(); waitFor.abort();
return void cb(new Error('EINVAL')); return void cb(new Error('EUNKNOWN'));
} }
// Since last 2 checkpoints // Since last 2 checkpoints

@ -1499,6 +1499,13 @@ define([
return; return;
} }
var onError = function (err) {
channel.bcast("PAD_ERROR", err);
// If this is a DELETED, EXPIRED or RESTRICTED pad, leave the channel
if (["EDELETED", "EEXPIRED", "ERESTRICTED"].indexOf(err.type) === -1) { return; }
Store.leavePad(null, data, function () {});
};
var conf = { var conf = {
onReady: function (pad) { onReady: function (pad) {
var padData = pad.metadata || {}; var padData = pad.metadata || {};
@ -1522,14 +1529,8 @@ define([
onLeave: function (m) { onLeave: function (m) {
channel.bcast("PAD_LEAVE", m); channel.bcast("PAD_LEAVE", m);
}, },
onError: function (err) { onError: onError,
channel.bcast("PAD_ERROR", err); onChannelError: onError,
Store.leavePad(null, data, function () {});
},
onChannelError: function (err) {
channel.bcast("PAD_ERROR", err);
Store.leavePad(null, data, function () {});
},
onRejected: function (allowed, _cb) { onRejected: function (allowed, _cb) {
var cb = Util.once(Util.mkAsync(_cb)); var cb = Util.once(Util.mkAsync(_cb));

@ -325,6 +325,11 @@ define([
UI.updateLoadingProgress({ state: -1 }, false); UI.updateLoadingProgress({ state: -1 }, false);
if (toolbar) {
// Check if we have a new chainpad instance
toolbar.resetChainpad(cpNfInner.chainpad);
}
var newPad = false; var newPad = false;
if (newContentStr === '') { newPad = true; } if (newContentStr === '') { newPad = true; }
@ -364,6 +369,9 @@ define([
}).nThen(function () { }).nThen(function () {
stateChange(STATE.READY); stateChange(STATE.READY);
firstConnection = false; firstConnection = false;
oldContent = undefined;
if (!readOnly) { onLocal(); } if (!readOnly) { onLocal(); }
evOnReady.fire(newPad); evOnReady.fire(newPad);

@ -49,8 +49,10 @@ define([
config = undefined; config = undefined;
var evPatchSent = Util.mkEvent(); var evPatchSent = Util.mkEvent();
var chainpad;
var chainpad = ChainPad.create({ var makeChainPad = function () {
var _chainpad = ChainPad.create({
userName: userName, userName: userName,
initialState: initialState, initialState: initialState,
patchTransformer: patchTransformer, patchTransformer: patchTransformer,
@ -58,15 +60,18 @@ define([
avgSyncMilliseconds: avgSyncMilliseconds, avgSyncMilliseconds: avgSyncMilliseconds,
logLevel: logLevel logLevel: logLevel
}); });
chainpad.onMessage(function(message, cb) { _chainpad.onMessage(function(message, cb) {
sframeChan.query('Q_RT_MESSAGE', message, function (err) { sframeChan.query('Q_RT_MESSAGE', message, function (err) {
if (!err) { evPatchSent.fire(); } if (!err) { evPatchSent.fire(); }
cb(err); cb(err);
}); });
}); });
chainpad.onPatch(function () { _chainpad.onPatch(function () {
onRemote({ realtime: chainpad }); onRemote({ realtime: chainpad });
}); });
return _chainpad;
};
chainpad = makeChainPad();
var myID; var myID;
var isReady = false; var isReady = false;
@ -96,6 +101,11 @@ define([
sframeChan.on('EV_RT_ERROR', function (err) { sframeChan.on('EV_RT_ERROR', function (err) {
isReady = false; isReady = false;
chainpad.abort(); chainpad.abort();
if (err.type === 'EUNKNOWN') { // XXX
// Recoverable error: make a new chainpad
chainpad = makeChainPad();
return;
}
onError(err); onError(err);
}); });
sframeChan.on('EV_RT_CONNECT', function (content) { sframeChan.on('EV_RT_CONNECT', function (content) {
@ -149,15 +159,18 @@ define([
}); });
}; };
return Object.freeze({ var cpNfInner = {
getMyID: function () { return myID; }, getMyID: function () { return myID; },
metadataMgr: metadataMgr, metadataMgr: metadataMgr,
whenRealtimeSyncs: whenRealtimeSyncs, whenRealtimeSyncs: whenRealtimeSyncs,
onInfiniteSpinner: evInfiniteSpinner.reg, onInfiniteSpinner: evInfiniteSpinner.reg,
onPatchSent: evPatchSent.reg, onPatchSent: evPatchSent.reg,
offPatchSent: evPatchSent.unreg, offPatchSent: evPatchSent.unreg,
chainpad: chainpad, };
cpNfInner.__defineGetter__("chainpad", function () {
return chainpad;
}); });
return Object.freeze(cpNfInner);
}; };
return Object.freeze(module.exports); return Object.freeze(module.exports);
}); });

@ -1331,6 +1331,15 @@ MessengerUI, Messages) {
showColors = true; showColors = true;
}; };
// If we had to create a new chainpad instance, reset the one used in the toolbar
toolbar.resetChainpad = function (chainpad) {
if (config.realtime !== chainpad) {
config.realtime = chainpad;
config.realtime.onPatch(ks(toolbar, config));
config.realtime.onMessage(ks(toolbar, config, true));
}
};
// On log out, remove permanently the realtime elements of the toolbar // On log out, remove permanently the realtime elements of the toolbar
Common.onLogout(function () { Common.onLogout(function () {
failed(); failed();

@ -794,6 +794,11 @@ define([
var userDoc = JSON.stringify(proxy); var userDoc = JSON.stringify(proxy);
if (userDoc === "" || userDoc === "{}") { isNew = true; } if (userDoc === "" || userDoc === "{}") { isNew = true; }
if (APP.toolbar && APP.rt.cpCnInner) {
// Check if we have a new chainpad instance
APP.toolbar.resetChainpad(APP.rt.cpCnInner.chainpad);
}
if (!isNew) { if (!isNew) {
if (proxy.info) { if (proxy.info) {
// Migration // Migration

Loading…
Cancel
Save