Skip to content

Commit 521f76f

Browse files
committed
Update to make OT error, client reconnect handling better and refactor client "doc" event
1 parent c9f5cd4 commit 521f76f

1 file changed

Lines changed: 11 additions & 16 deletions

File tree

public/js/index.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2219,6 +2219,7 @@ socket.on('error', function (data) {
22192219
var retryOnDisconnect = false;
22202220
var retryTimer = null;
22212221
socket.on('maintenance', function () {
2222+
cmClient.revision = -1;
22222223
retryOnDisconnect = true;
22232224
});
22242225
socket.on('disconnect', function (data) {
@@ -2557,16 +2558,14 @@ socket.on('doc', function (obj) {
25572558
obj = LZString.decompressFromUTF16(obj);
25582559
obj = JSON.parse(obj);
25592560
var body = obj.str;
2560-
var bodyMismatch = (editor.getValue() != body);
2561+
var bodyMismatch = editor.getValue() !== body;
2562+
var setDoc = !cmClient || (cmClient && cmClient.revision === -1) || obj.force;
25612563

25622564
saveInfo();
2563-
if (bodyMismatch) {
2564-
if (cmClient)
2565-
cmClient.editorAdapter.ignoreNextChange = true;
2566-
if (body)
2567-
editor.setValue(body);
2568-
else
2569-
editor.setValue("");
2565+
if (setDoc && bodyMismatch) {
2566+
if (cmClient) cmClient.editorAdapter.ignoreNextChange = true;
2567+
if (body) editor.setValue(body);
2568+
else editor.setValue("");
25702569
}
25712570

25722571
if (!loaded) {
@@ -2575,12 +2574,8 @@ socket.on('doc', function (obj) {
25752574
ui.content.fadeIn();
25762575
} else {
25772576
//if current doc is equal to the doc before disconnect
2578-
if (bodyMismatch)
2579-
editor.clearHistory();
2580-
else {
2581-
if (lastInfo.history)
2582-
editor.setHistory(lastInfo.history);
2583-
}
2577+
if (setDoc && bodyMismatch) editor.clearHistory();
2578+
else if (lastInfo.history) editor.setHistory(lastInfo.history);
25842579
lastInfo.history = null;
25852580
}
25862581

@@ -2589,7 +2584,7 @@ socket.on('doc', function (obj) {
25892584
obj.revision, obj.clients,
25902585
new SocketIOAdapter(socket), new CodeMirrorAdapter(editor)
25912586
);
2592-
} else {
2587+
} else if (setDoc) {
25932588
if (bodyMismatch) {
25942589
cmClient.undoManager.undoStack.length = 0;
25952590
cmClient.undoManager.redoStack.length = 0;
@@ -2600,7 +2595,7 @@ socket.on('doc', function (obj) {
26002595
cmClient.initializeClients(obj.clients);
26012596
}
26022597

2603-
if (bodyMismatch) {
2598+
if (setDoc && bodyMismatch) {
26042599
isDirty = true;
26052600
updateView();
26062601
}

0 commit comments

Comments
 (0)