From ab6deb8e2ec7d99001ec22e8491a373495667852 Mon Sep 17 00:00:00 2001
From: Wu Cheng-Han <jacky_cute0808@hotmail.com>
Date: Fri, 25 Sep 2015 13:59:28 +0800
Subject: [PATCH] Fixed when doc reconnect might get wrong OT operation,
 reorder initialize instructions in proper sequence

---
 public/js/index.js | 57 ++++++++++++++++++++++++----------------------
 1 file changed, 30 insertions(+), 27 deletions(-)

diff --git a/public/js/index.js b/public/js/index.js
index 199be88..d470776 100644
--- a/public/js/index.js
+++ b/public/js/index.js
@@ -998,6 +998,18 @@ socket.on('refresh', function (data) {
     lastchangeui = ui.infobar.lastchange;
     updateLastChange();
     checkPermission();
+    if (!loaded) {
+        changeMode(currentMode);
+        loaded = true;
+        emitUserStatus(); //send first user status
+        updateOnlineStatus(); //update first online status
+        setTimeout(function () {
+            //work around editor not refresh or doc not fully loaded
+            windowResizeInner();
+            //work around might not scroll to hash
+            scrollToHash();
+        }, 1);
+    }
 });
 
 var EditorClient = ot.EditorClient;
@@ -1013,43 +1025,18 @@ socket.on('doc', function (obj) {
 
     saveInfo();
     if (bodyMismatch) {
+        if (cmClient)
+            cmClient.editorAdapter.ignoreNextChange = true;
         if (body)
             editor.setValue(body);
         else
             editor.setValue("");
     }
-    if (!cmClient) {
-        cmClient = window.cmClient = new EditorClient(
-            obj.revision, obj.clients,
-            new SocketIOAdapter(socket), new CodeMirrorAdapter(editor)
-        );
-    } else {
-        cmClient.revision = obj.revision;
-        cmClient.initializeClients(obj.clients);
-        if (bodyMismatch) {
-            cmClient.undoManager.undoStack.length = 0;
-            cmClient.undoManager.redoStack.length = 0;
-        }
-    }
 
     if (!loaded) {
         editor.clearHistory();
         ui.spinner.hide();
         ui.content.fadeIn();
-        changeMode();
-        loaded = true;
-        emitUserStatus(); //send first user status
-        updateOnlineStatus(); //update first online status
-        setTimeout(function () {
-            //work around editor not refresh
-            editor.refresh();
-            //work around cursor not refresh
-            for (var i = 0; i < onlineUsers.length; i++) {
-                buildCursor(onlineUsers[i]);
-            }
-            //work around might not scroll to hash
-            scrollToHash();
-        }, 1);
     } else {
         //if current doc is equal to the doc before disconnect
         if (bodyMismatch)
@@ -1061,6 +1048,22 @@ socket.on('doc', function (obj) {
         lastInfo.history = null;
     }
 
+    if (!cmClient) {
+        cmClient = window.cmClient = new EditorClient(
+            obj.revision, obj.clients,
+            new SocketIOAdapter(socket), new CodeMirrorAdapter(editor)
+        );
+    } else {
+        if (bodyMismatch) {
+            cmClient.undoManager.undoStack.length = 0;
+            cmClient.undoManager.redoStack.length = 0;
+        }
+        cmClient.revision = obj.revision;
+        cmClient.setState(new ot.Client.Synchronized());
+        cmClient.initializeClientList();
+        cmClient.initializeClients(obj.clients);
+    }
+
     if (bodyMismatch) {
         isDirty = true;
         updateView();