mirror of
https://github.com/leanote/desktop-app.git
synced 2025-10-18 17:52:17 +00:00
sync优化, 只有在content不一样的时候才认为conflict
This commit is contained in:
4
node_modules/api.js
generated
vendored
4
node_modules/api.js
generated
vendored
@@ -209,12 +209,8 @@ var Api = {
|
|||||||
return callback && callback(false);
|
return callback && callback(false);
|
||||||
}
|
}
|
||||||
var ret = response.body;
|
var ret = response.body;
|
||||||
log('--------')
|
|
||||||
log(ret);
|
|
||||||
if(Common.isOk(ret)) { // {Content: 'xx', NoteId: 'xxx'}
|
if(Common.isOk(ret)) { // {Content: 'xx', NoteId: 'xxx'}
|
||||||
// Note.updateNoteContentForce(noteId, ret.Content, function() {
|
|
||||||
callback && callback(ret);
|
callback && callback(ret);
|
||||||
// });
|
|
||||||
} else {
|
} else {
|
||||||
callback && callback(false);
|
callback && callback(false);
|
||||||
}
|
}
|
||||||
|
35
node_modules/note.js
generated
vendored
35
node_modules/note.js
generated
vendored
@@ -408,7 +408,7 @@ var Note = {
|
|||||||
var me = this;
|
var me = this;
|
||||||
if(note) {
|
if(note) {
|
||||||
// 如果是本地用户, 则直接删除
|
// 如果是本地用户, 则直接删除
|
||||||
if(User.isLocal) {
|
if(User.isLocal()) {
|
||||||
db.notes.remove({_id: note._id}, function(err, n) {
|
db.notes.remove({_id: note._id}, function(err, n) {
|
||||||
if(n) {
|
if(n) {
|
||||||
me.deleteNoteAllOthers(note);
|
me.deleteNoteAllOthers(note);
|
||||||
@@ -574,9 +574,8 @@ var Note = {
|
|||||||
// 远程修改本地内容
|
// 远程修改本地内容
|
||||||
updateNoteContentForce: function(noteId, content, callback) {
|
updateNoteContentForce: function(noteId, content, callback) {
|
||||||
var me = this;
|
var me = this;
|
||||||
|
// 修复内容, 修改图片, 附件链接为本地链接
|
||||||
content = me.fixNoteContent(content);
|
content = me.fixNoteContent(content);
|
||||||
|
|
||||||
db.notes.update({NoteId: noteId}, { $set: {Content: content, InitSync: false, IsContentDirty: false} }, {}, function (err, numReplaced) {
|
db.notes.update({NoteId: noteId}, { $set: {Content: content, InitSync: false, IsContentDirty: false} }, {}, function (err, numReplaced) {
|
||||||
if(err) {
|
if(err) {
|
||||||
log(err);
|
log(err);
|
||||||
@@ -587,21 +586,20 @@ var Note = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
/*
|
// sync调用, 用于判断是否真的有冲突
|
||||||
// 同步内容
|
// 从服务器上获取内容
|
||||||
updateNoteContentForce: function(noteId, content, callback) {
|
getNoteContentFromServer: function (serverNoteId, callback) {
|
||||||
// 将笔记内容中
|
var me = this;
|
||||||
|
Api.getNoteContent(serverNoteId, function(noteContent) {
|
||||||
db.notes.update({NoteId: noteId}, { $set: {Content: content, InitSync: false} }, {}, function (err, numReplaced) {
|
// 同步到本地
|
||||||
if(err) {
|
if(Common.isOk(noteContent)) {
|
||||||
log(err);
|
var content = me.fixNoteContent(noteContent.Content);
|
||||||
callback && callback(false);
|
callback(content);
|
||||||
} else {
|
} else {
|
||||||
callback && callback(content);
|
callback(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
*/
|
|
||||||
|
|
||||||
// 得到笔记内容
|
// 得到笔记内容
|
||||||
// noteId是本地Id
|
// noteId是本地Id
|
||||||
@@ -654,6 +652,7 @@ var Note = {
|
|||||||
|
|
||||||
// 同步到本地
|
// 同步到本地
|
||||||
if(Common.isOk(noteContent)) {
|
if(Common.isOk(noteContent)) {
|
||||||
|
|
||||||
me.updateNoteContentForce(noteId, noteContent.Content, function(content) {
|
me.updateNoteContentForce(noteId, noteContent.Content, function(content) {
|
||||||
noteContent.Content = content;
|
noteContent.Content = content;
|
||||||
noteContent.NoteId = noteId;
|
noteContent.NoteId = noteId;
|
||||||
@@ -809,6 +808,11 @@ var Note = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// send change后, 发现内容是一样的, 此时修改本地Usn和server的一样, 下次再push
|
||||||
|
updateNoteUsn: function (noteId, usn) {
|
||||||
|
db.notes.update({NoteId: note.NoteId}, {$set: {Usn: usn, IsDirty: true}});
|
||||||
|
},
|
||||||
|
|
||||||
// sync <- 时
|
// sync <- 时
|
||||||
// 更新笔记, 合并之, 内容要重新获取
|
// 更新笔记, 合并之, 内容要重新获取
|
||||||
// note是服务器传过来的, 需要处理下fix
|
// note是服务器传过来的, 需要处理下fix
|
||||||
@@ -1392,6 +1396,9 @@ var Note = {
|
|||||||
} else {
|
} else {
|
||||||
// 每一个笔记得到图片, 附件信息和数据
|
// 每一个笔记得到图片, 附件信息和数据
|
||||||
async.eachSeries(notes, function(note, cb) {
|
async.eachSeries(notes, function(note, cb) {
|
||||||
|
// LocalContent 留作副本, 用于冲突判断
|
||||||
|
note.LocalContent = note.Content;
|
||||||
|
|
||||||
note.Content = me.fixContentUrl(note.Content);
|
note.Content = me.fixContentUrl(note.Content);
|
||||||
me.getNoteFiles(note, function(files) {
|
me.getNoteFiles(note, function(files) {
|
||||||
note.Content = me.fixNoteContentForSend(note.Content);
|
note.Content = me.fixNoteContentForSend(note.Content);
|
||||||
|
125
node_modules/sync.js
generated
vendored
125
node_modules/sync.js
generated
vendored
@@ -141,9 +141,9 @@ var Sync = {
|
|||||||
function canCall() {
|
function canCall() {
|
||||||
// 是最后一块, 且
|
// 是最后一块, 且
|
||||||
me._addSyncNotebookNum();
|
me._addSyncNotebookNum();
|
||||||
console.log(me._syncNotebookIsLastChunk);
|
// console.log(me._syncNotebookIsLastChunk);
|
||||||
console.log(me._totalHasSyncNotebookNum + ' ' + me._totalSyncNotebookNum);
|
// console.log(me._totalHasSyncNotebookNum + ' ' + me._totalSyncNotebookNum);
|
||||||
console.log(me._syncInfo.notebook.ok);
|
// console.log(me._syncInfo.notebook.ok);
|
||||||
if(me._syncNotebookIsLastChunk &&
|
if(me._syncNotebookIsLastChunk &&
|
||||||
me._totalHasSyncNotebookNum >= me._totalSyncNotebookNum) {
|
me._totalHasSyncNotebookNum >= me._totalSyncNotebookNum) {
|
||||||
// 防止多次callback
|
// 防止多次callback
|
||||||
@@ -171,8 +171,8 @@ var Sync = {
|
|||||||
|
|
||||||
// 1) 服务器端删除了, 本地肯定删除
|
// 1) 服务器端删除了, 本地肯定删除
|
||||||
if(notebook.IsDeleted) {
|
if(notebook.IsDeleted) {
|
||||||
console.log('delete: ');
|
// console.log('delete: ');
|
||||||
console.log(notebook);
|
// console.log(notebook);
|
||||||
Notebook.getNotebookIdByServerNotebookId(notebookId, function(localNotebookId) {
|
Notebook.getNotebookIdByServerNotebookId(notebookId, function(localNotebookId) {
|
||||||
Notebook.deleteNotebookForce(notebookId, function() {
|
Notebook.deleteNotebookForce(notebookId, function() {
|
||||||
me._syncInfo.notebook.deletes.push(localNotebookId);
|
me._syncInfo.notebook.deletes.push(localNotebookId);
|
||||||
@@ -332,6 +332,7 @@ var Sync = {
|
|||||||
|
|
||||||
// 2.2 本地是否修改了, 冲突, 报告给前端, 前端处理
|
// 2.2 本地是否修改了, 冲突, 报告给前端, 前端处理
|
||||||
// 冲突, 将本地修改的笔记复制一份(设置冲突字段, ConflictNoteId), 远程的覆盖本地的
|
// 冲突, 将本地修改的笔记复制一份(设置冲突字段, ConflictNoteId), 远程的覆盖本地的
|
||||||
|
// 新方法: 冲突后, 得到最新内容, 看是否与本地内容一致, 如果一致, 则不冲突, 其它数据用服务器上的
|
||||||
if(noteLocal.IsDirty) {
|
if(noteLocal.IsDirty) {
|
||||||
console.log('note 冲突.... serverNoteId: ' + noteId);
|
console.log('note 冲突.... serverNoteId: ' + noteId);
|
||||||
// console.log(noteLocal.NoteId);
|
// console.log(noteLocal.NoteId);
|
||||||
@@ -339,7 +340,28 @@ var Sync = {
|
|||||||
// console.log(noteLocal);
|
// console.log(noteLocal);
|
||||||
// note.ServerNoteId = note.NoteId;
|
// note.ServerNoteId = note.NoteId;
|
||||||
// note.NoteId = noteLocal.NoteId;
|
// note.NoteId = noteLocal.NoteId;
|
||||||
me._syncInfo.note.conflicts.push({server: note, local: noteLocal});
|
|
||||||
|
Note.getNoteContentFromServer(noteId, function (content) {
|
||||||
|
// 表示没有获取到content, 则只能标志为冲突了
|
||||||
|
// 内容不一样, 也标为冲突
|
||||||
|
if (content === false || content != noteLocal.Content) {
|
||||||
|
me._syncInfo.note.conflicts.push({server: note, local: noteLocal});
|
||||||
|
}
|
||||||
|
// 否则, 内容一样, 标为不冲突, 需要更新
|
||||||
|
else {
|
||||||
|
|
||||||
|
// 2.3 服务器是最新的, 用服务器的
|
||||||
|
// 服务器是最新的, 本地没动过, 则覆盖之
|
||||||
|
Note.updateNoteForce(note, function(note) {
|
||||||
|
if(note) {
|
||||||
|
me._syncInfo.note.updates.push(note);
|
||||||
|
}
|
||||||
|
canCall();
|
||||||
|
}, false);
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return canCall();
|
return canCall();
|
||||||
// 2.3 服务器是最新的, 用服务器的
|
// 2.3 服务器是最新的, 用服务器的
|
||||||
} else {
|
} else {
|
||||||
@@ -364,7 +386,7 @@ var Sync = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Api.getSyncNotes(afterUsn, me._noteMaxEntry, function(notes) {
|
Api.getSyncNotes(afterUsn, me._noteMaxEntry, function(notes) {
|
||||||
console.log('syncNote---')
|
console.log('syncNote---');
|
||||||
console.log(notes);
|
console.log(notes);
|
||||||
if(Common.isOk(notes)) {
|
if(Common.isOk(notes)) {
|
||||||
me._totalSyncNoteNum += notes.length;
|
me._totalSyncNoteNum += notes.length;
|
||||||
@@ -874,70 +896,55 @@ var Sync = {
|
|||||||
if(typeof ret == 'object') {
|
if(typeof ret == 'object') {
|
||||||
if(ret.Msg == 'conflict') {
|
if(ret.Msg == 'conflict') {
|
||||||
console.error('updateNote 冲突-----------');
|
console.error('updateNote 冲突-----------');
|
||||||
me._syncInfo.note.changeConflicts.push(note);
|
|
||||||
|
// 这种情况有很少见, 原因是先pull, 肯定会pull过来
|
||||||
|
// 处理方法和pull一样, 看内容是否一样
|
||||||
|
// 如果一样, 则不标志为冲突, 修改本Usn为serverUsn, 等下次再send changes
|
||||||
|
Note.getNoteContentFromServer(note.ServerNoteId, function (content) {
|
||||||
|
if (content === false || content != note.LocalContent) {
|
||||||
|
me._syncInfo.note.changeConflicts.push(note);
|
||||||
|
cb();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// 不冲突, 修改之Usn
|
||||||
|
Api.getNote(note.ServerNoteId, function (serverNote) {
|
||||||
|
// 取不到, 当作冲突
|
||||||
|
if (!serverNote) {
|
||||||
|
me._syncInfo.note.changeConflicts.push(note);
|
||||||
|
cb();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Note.updateNoteUsn(note.NoteId, serverNote.Usn);
|
||||||
|
cb();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
else if(ret.Msg == 'notExists') {
|
else if(ret.Msg == 'notExists') {
|
||||||
// 可能服务器上已删除, 此时应该要作为添加而不是更新
|
// 可能服务器上已删除, 此时应该要作为添加而不是更新
|
||||||
me._syncInfo.note.changeNeedAdds.push(note);
|
me._syncInfo.note.changeNeedAdds.push(note);
|
||||||
|
cb();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return cb();
|
// 更新失败了, 服务器返回异常
|
||||||
|
else {
|
||||||
|
cb();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// 更新成功
|
// 更新成功
|
||||||
// TODO 返回是真note
|
else {
|
||||||
ret.ServerNoteId = ret.NoteId;
|
ret.ServerNoteId = ret.NoteId;
|
||||||
ret.NoteId = note.NoteId;
|
ret.NoteId = note.NoteId;
|
||||||
Note.updateNoteForceForSendChange(ret, false);
|
Note.updateNoteForceForSendChange(ret, false);
|
||||||
me._syncInfo.note.changeUpdates.push(note);
|
me._syncInfo.note.changeUpdates.push(note);
|
||||||
|
|
||||||
me.checkNeedIncSyncAgain(ret.Usn);
|
me.checkNeedIncSyncAgain(ret.Usn);
|
||||||
|
|
||||||
return cb();
|
return cb();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
//...... 过时
|
|
||||||
/*
|
|
||||||
api.call(Api, note, function(ret) {
|
|
||||||
// 更新失败
|
|
||||||
if(!newNote) {
|
|
||||||
return cb();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 删除操作
|
|
||||||
if(note.LocalIsDelete) {
|
|
||||||
return cb();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 更新成功, 是否有冲突?
|
|
||||||
// newNote是服务器上的笔记
|
|
||||||
// 没有更新成功, 那么要处理冲突
|
|
||||||
if(!newNote.NoteId) {
|
|
||||||
if(newNote.Msg == 'conflict') {
|
|
||||||
me._syncInfo.note.changeConflicts.push(note);
|
|
||||||
} else if(newNote.Msg == 'notExists') {
|
|
||||||
// 可能服务器上已删除, 此时应该要作为添加而不是更新
|
|
||||||
me._syncInfo.note.changeNeedAdds.push(note);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 更新成功, 修改到本地
|
|
||||||
else {
|
|
||||||
// 更新
|
|
||||||
Note.updateNoteForceForSendChange(note.NoteId, newNote);
|
|
||||||
|
|
||||||
if(note.LocalIsNew) {
|
|
||||||
newNote.ServerNoteId = newNote.NoteId;
|
|
||||||
newNote.NoteId = note.NoteId;
|
|
||||||
me._syncInfo.note.changeAdds.push(newNote);
|
|
||||||
} else {
|
|
||||||
me._syncInfo.note.updates.push(newNote);
|
|
||||||
}
|
|
||||||
|
|
||||||
me.checkNeedIncSyncAgain(newNote.Usn);
|
|
||||||
}
|
|
||||||
cb();
|
|
||||||
});
|
|
||||||
*/
|
|
||||||
}, function() {
|
}, function() {
|
||||||
callback && callback();
|
callback && callback();
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user