Update to make OT error, client reconnect handling better and refactor client "doc" event

This commit is contained in:
Wu Cheng-Han 2016-10-10 20:44:45 +08:00
parent c9f5cd49b0
commit 521f76f7ad

View file

@ -2219,6 +2219,7 @@ socket.on('error', function (data) {
var retryOnDisconnect = false; var retryOnDisconnect = false;
var retryTimer = null; var retryTimer = null;
socket.on('maintenance', function () { socket.on('maintenance', function () {
cmClient.revision = -1;
retryOnDisconnect = true; retryOnDisconnect = true;
}); });
socket.on('disconnect', function (data) { socket.on('disconnect', function (data) {
@ -2557,16 +2558,14 @@ socket.on('doc', function (obj) {
obj = LZString.decompressFromUTF16(obj); obj = LZString.decompressFromUTF16(obj);
obj = JSON.parse(obj); obj = JSON.parse(obj);
var body = obj.str; var body = obj.str;
var bodyMismatch = (editor.getValue() != body); var bodyMismatch = editor.getValue() !== body;
var setDoc = !cmClient || (cmClient && cmClient.revision === -1) || obj.force;
saveInfo(); saveInfo();
if (bodyMismatch) { if (setDoc && bodyMismatch) {
if (cmClient) if (cmClient) cmClient.editorAdapter.ignoreNextChange = true;
cmClient.editorAdapter.ignoreNextChange = true; if (body) editor.setValue(body);
if (body) else editor.setValue("");
editor.setValue(body);
else
editor.setValue("");
} }
if (!loaded) { if (!loaded) {
@ -2575,12 +2574,8 @@ socket.on('doc', function (obj) {
ui.content.fadeIn(); ui.content.fadeIn();
} else { } else {
//if current doc is equal to the doc before disconnect //if current doc is equal to the doc before disconnect
if (bodyMismatch) if (setDoc && bodyMismatch) editor.clearHistory();
editor.clearHistory(); else if (lastInfo.history) editor.setHistory(lastInfo.history);
else {
if (lastInfo.history)
editor.setHistory(lastInfo.history);
}
lastInfo.history = null; lastInfo.history = null;
} }
@ -2589,7 +2584,7 @@ socket.on('doc', function (obj) {
obj.revision, obj.clients, obj.revision, obj.clients,
new SocketIOAdapter(socket), new CodeMirrorAdapter(editor) new SocketIOAdapter(socket), new CodeMirrorAdapter(editor)
); );
} else { } else if (setDoc) {
if (bodyMismatch) { if (bodyMismatch) {
cmClient.undoManager.undoStack.length = 0; cmClient.undoManager.undoStack.length = 0;
cmClient.undoManager.redoStack.length = 0; cmClient.undoManager.redoStack.length = 0;
@ -2600,7 +2595,7 @@ socket.on('doc', function (obj) {
cmClient.initializeClients(obj.clients); cmClient.initializeClients(obj.clients);
} }
if (bodyMismatch) { if (setDoc && bodyMismatch) {
isDirty = true; isDirty = true;
updateView(); updateView();
} }