From 521f76f7add074c4bbd200dc270cefc57e38f806 Mon Sep 17 00:00:00 2001 From: Wu Cheng-Han Date: Mon, 10 Oct 2016 20:44:45 +0800 Subject: [PATCH] Update to make OT error, client reconnect handling better and refactor client "doc" event --- public/js/index.js | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/public/js/index.js b/public/js/index.js index 323da28..575baca 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -2219,6 +2219,7 @@ socket.on('error', function (data) { var retryOnDisconnect = false; var retryTimer = null; socket.on('maintenance', function () { + cmClient.revision = -1; retryOnDisconnect = true; }); socket.on('disconnect', function (data) { @@ -2557,16 +2558,14 @@ socket.on('doc', function (obj) { obj = LZString.decompressFromUTF16(obj); obj = JSON.parse(obj); var body = obj.str; - var bodyMismatch = (editor.getValue() != body); + var bodyMismatch = editor.getValue() !== body; + var setDoc = !cmClient || (cmClient && cmClient.revision === -1) || obj.force; saveInfo(); - if (bodyMismatch) { - if (cmClient) - cmClient.editorAdapter.ignoreNextChange = true; - if (body) - editor.setValue(body); - else - editor.setValue(""); + if (setDoc && bodyMismatch) { + if (cmClient) cmClient.editorAdapter.ignoreNextChange = true; + if (body) editor.setValue(body); + else editor.setValue(""); } if (!loaded) { @@ -2575,12 +2574,8 @@ socket.on('doc', function (obj) { ui.content.fadeIn(); } else { //if current doc is equal to the doc before disconnect - if (bodyMismatch) - editor.clearHistory(); - else { - if (lastInfo.history) - editor.setHistory(lastInfo.history); - } + if (setDoc && bodyMismatch) editor.clearHistory(); + else if (lastInfo.history) editor.setHistory(lastInfo.history); lastInfo.history = null; } @@ -2589,7 +2584,7 @@ socket.on('doc', function (obj) { obj.revision, obj.clients, new SocketIOAdapter(socket), new CodeMirrorAdapter(editor) ); - } else { + } else if (setDoc) { if (bodyMismatch) { cmClient.undoManager.undoStack.length = 0; cmClient.undoManager.redoStack.length = 0; @@ -2600,7 +2595,7 @@ socket.on('doc', function (obj) { cmClient.initializeClients(obj.clients); } - if (bodyMismatch) { + if (setDoc && bodyMismatch) { isDirty = true; updateView(); }