diff --git a/node_modules/api.js b/node_modules/api.js index 15f86bfc..ab89eed4 100644 --- a/node_modules/api.js +++ b/node_modules/api.js @@ -173,7 +173,7 @@ var Api = { return callback && callback(false); } var ret = response.body; - console.log(ret); + // console.log(ret); if(Common.isOk(ret)) { callback && callback(ret); } else { diff --git a/node_modules/note.js b/node_modules/note.js index b9a77509..ad623cc4 100644 --- a/node_modules/note.js +++ b/node_modules/note.js @@ -722,9 +722,9 @@ var Note = { if(doc.length > 1) { console.error(doc.length + '. ..'); } - console.log('note length: ' + doc.length + '. ..'); + // console.log('note length: ' + doc.length + '. ..'); if(err || !doc || !doc.length) { - log('getNoteByServerNoteId 不存在' + noteId); + // log('getNoteByServerNoteId 不存在' + noteId); callback && callback(false); } else { doc = doc[0]; @@ -736,7 +736,7 @@ var Note = { var me = this; db.notes.findOne({ServerNoteId: noteId}, function(err, doc) { if(err || !doc) { - log('getNoteIdByServerNoteId 不存在' + noteId); + // log('getNoteIdByServerNoteId 不存在' + noteId); callback && callback(false); } else { callback && callback(doc.NoteId); @@ -1767,7 +1767,7 @@ var Note = { updateNoteToDeleteTag: function(title, callback) { var updates = {}; // noteId => var userId = User.getCurActiveUserId(); - console.log('updateNoteToDeleteTag--'); + // console.log('updateNoteToDeleteTag--'); db.notes.find({UserId: userId, LocalIsDelete: false , Tags: {$in: [title]}}, function(err, notes) { console.log(notes); if(!err && notes && notes.length > 0) { @@ -1791,8 +1791,8 @@ var Note = { }); } } else { - console.log('updateNoteToDeleteTag'); - console.log(err); + // console.log('updateNoteToDeleteTag'); + // console.log(err); callback({}); } }); diff --git a/node_modules/sync.js b/node_modules/sync.js index b9d75a1d..9fe38179 100644 --- a/node_modules/sync.js +++ b/node_modules/sync.js @@ -12,7 +12,7 @@ var Note = require('note'); var Web = require('web'); function log(o) { - console.log(o); + // console.log(o); } // 同步服务 @@ -224,7 +224,7 @@ var Sync = { } Api.getSyncNotebooks(afterUsn, me._notebookMaxEntry, function(notebooks) { console.log('syncNotebook') - console.log(notebooks); + // console.log(notebooks); if(Common.isOk(notebooks)) { me._totalSyncNotebookNum += notebooks.length; // 证明可能还有要同步的 @@ -232,10 +232,19 @@ var Sync = { me._syncNotebookToLocal(notebooks, callback); var last = notebooks[notebooks.length-1]; me.syncNotebook(last.Usn, callback); + + // 更新Usn + me.updateSyncUsn('NotebookUsn', last.Usn); + } else { console.log('no more'); me._syncNotebookIsLastChunk = true; me._syncNotebookToLocal(notebooks, callback); + + if (notebooks.length) { + var last = notebooks[notebooks.length-1]; + me.updateSyncUsn('NotebookUsn', last.Usn); + } } } else { // 同步失败 @@ -386,26 +395,39 @@ var Sync = { } Api.getSyncNotes(afterUsn, me._noteMaxEntry, function(notes) { - console.log('syncNote---'); - console.log(notes); + // console.log('syncNote---'); + // console.log(notes); if(Common.isOk(notes)) { me._totalSyncNoteNum += notes.length; // 证明可能还有要同步的 if(notes.length == me._noteMaxEntry) { me._syncNoteToLocal(notes, callback); var last = notes[notes.length-1]; - me.syncNote(last.Usn, callback); + + // 500ms延迟 + setTimeout(function () { + me.syncNote(last.Usn, callback); + }, 500); + + // 更新Usn + me.updateSyncUsn('NoteUsn', last.Usn); + } else { log('no more'); me._syncNoteIsLastChunk = true; me._syncNoteToLocal(notes, callback); + + if (notes.length) { + var last = notes[notes.length-1]; + me.updateSyncUsn('NoteUsn', last.Usn); + } } } else { // 同步失败 me._syncInfo.note.ok = false; me._syncInfo.note.msg = "cann't get all chunks"; - console.log(notes); - console.log('eeeeeeeeeeeeeeeeeerrror') + console.error('eeeeeeeeeeeeeeeeeerrror') + console.error(notes); callback && callback(false); } }); @@ -508,10 +530,18 @@ var Sync = { me._syncTagToLocal(tags, callback); var last = tags[tags.length-1]; me.syncTag(last.Usn, callback); + + // 更新Usn + me.updateSyncUsn('TagUsn', last.Usn); } else { log('no more'); me._syncTagIsLastChunk = true; me._syncTagToLocal(tags, callback); + + if (tags.length) { + var last = tags[tags.length-1]; + me.updateSyncUsn('TagUsn', last.Usn); + } } } else { // 同步失败 @@ -534,40 +564,70 @@ var Sync = { }); }, + // 为了避免全都重新来过, 这里保存每次 + updateSyncUsn: function (type, usn) { + console.error('-------------------------------------------------'); + console.log(type + ' = ' + usn); + User.updateEachSyncState(type, usn, function() { + }); + }, + // 全量同步 fullSync: function(callback) { var me = this; me._stop = false; me._initSyncInfo(); - // Web.syncNotebookFinish(); - // 同步笔记本 - me.syncNotebook(-1, function(ok) { - if(ok) { - // Web.syncNoteFinish(); - // console.log('------------------') - // 同步笔记 - me.syncNote(-1, function(ok) { - if(ok) { - // Web.syncTagFinish(); - // 同步标签 - me.syncTag(-1, function() { - // 更新上次同步时间 - me.updateLastSyncState(function() { - // send changes - // me.sendChanges(); - callback && callback(me._syncInfo, true); - }); - }); - } else { - console.error('syncNote error.... 跳过tag'); - callback && callback(me._syncInfo, false); - } - }); - } else { - console.error('syncNotebook error.... 跳过note,tag'); - callback && callback(me._syncInfo, false); + User.getAllLastSyncState(function(lastUsn, notebookUsn, noteUsn, tagUsn) { + // 不可能会有lastUsn吧 + if (lastUsn && lastUsn > 0) { + notebookUsn = -1; + noteUsn = -1; + tagUsn = -1; } + + if (!notebookUsn) { + notebookUsn = -1; + } + if (!noteUsn) { + noteUsn = -1; + } + if (!tagUsn) { + tagUsn = -1; + } + + // console.log('fullSync------ ' + notebookUsn + ' ' + noteUsn + ' ' + tagUsn); + + // Web.syncNotebookFinish(); + // 同步笔记本 + me.syncNotebook(notebookUsn, function(ok) { + if(ok) { + // Web.syncNoteFinish(); + // console.log('------------------') + // 同步笔记 + me.syncNote(noteUsn, function(ok) { + if(ok) { + // Web.syncTagFinish(); + // 同步标签 + me.syncTag(tagUsn, function() { + // 更新上次同步时间 + me.updateLastSyncState(function() { + // send changes + // me.sendChanges(); + callback && callback(me._syncInfo, true); + }); + }); + } else { + console.error('syncNote error.... 跳过tag'); + callback && callback(me._syncInfo, false); + } + }); + } else { + console.error('syncNotebook error.... 跳过note,tag'); + callback && callback(me._syncInfo, false); + } + }); + }); }, @@ -577,9 +637,9 @@ var Sync = { fixConflicts: function(callback) { var me = this; var afterInfo = me._syncInfo; - log('处理冲突....'); - log(me._syncInfo); - log(me._syncInfo.tag); + // log('处理冲突....'); + // log(me._syncInfo); + // log(me._syncInfo.tag); var tag = me._syncInfo.tag; // 如果是incSync, 则要前端处理 diff --git a/node_modules/tag.js b/node_modules/tag.js index 59d178c7..0a6b8ed8 100644 --- a/node_modules/tag.js +++ b/node_modules/tag.js @@ -72,7 +72,7 @@ var Tag = { }); // var Note = require('./note'); - console.log(Note); + // console.log(Note); Note.updateNoteToDeleteTag(title, function(updates) { callback && callback(updates); }); diff --git a/node_modules/user.js b/node_modules/user.js index 03af248c..b8c79a7b 100644 --- a/node_modules/user.js +++ b/node_modules/user.js @@ -379,7 +379,7 @@ User = { fullSyncForce: function(callback) { var me = this; var userId = me.getCurActiveUserId(); - db.users.update({UserId: userId}, {$set: {LastSyncUsn: -1}}, function() { + db.users.update({UserId: userId}, {$set: {LastSyncUsn: -1, NotebookUsn: -1, NoteUsn: -1, TagUsn: -1}}, function() { // 删除本地账户所有数据 me.deleteUserAllData(userId, function () { callback && callback(); @@ -396,10 +396,11 @@ User = { } Api.getLastSyncState(function(state) { if(state) { - // console.error('--updateLastSyncState---') - // console.log(state); me.LastSyncUsn = state.LastSyncUsn; me.LastSyncTime = state.LastSyncTime; + me.NotebookUsn = -1; + me.NoteUsn = -1; + me.TagUsn = -1; db.users.update({UserId: me.getCurActiveUserId()}, {$set: state}); } callback(); @@ -414,6 +415,17 @@ User = { var me = this; return me.LastSyncUsn; }, + + getAllLastSyncState: function (callback) { + var me = this; + me.getCurUser(function (user) { + if (!user) { + return callback(false); + } + callback(user.LastSyncUsn, user.NotebookUsn, user.NoteUsn, user.TagUsn); + }); + }, + // 更新 send changes要用 updateLastSyncUsn: function(usn) { var me = this; @@ -421,6 +433,14 @@ User = { db.users.update({UserId: me.getCurActiveUserId()}, {$set: {LastSyncUsn: usn}}); }, + // 更新每一个类型的USN, 仅在全量同步时有用 + updateEachSyncState: function (type, usn, callback) { + var me = this; + var updates = {}; + updates[type] = usn; + db.users.update({UserId: me.getCurActiveUserId()}, {$set: updates}); + }, + // 全局配置 getG: function(callback) { var me = this;