From f3e02ac20eb3743a2798c6ba2560cef18531437a Mon Sep 17 00:00:00 2001 From: life Date: Sat, 2 May 2015 23:01:10 +0800 Subject: [PATCH] =?UTF-8?q?sync=E5=89=8D=E4=BF=9D=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/node_modules/api.js | 7 ++--- src/node_modules/sync.js | 61 ++++++++++++++++++++++++++------------- src/public/js/app/note.js | 9 ++---- src/public/js/app/page.js | 19 +++++++++--- src/public/js/common.js | 5 ++++ 5 files changed, 67 insertions(+), 34 deletions(-) diff --git a/src/node_modules/api.js b/src/node_modules/api.js index 02987843..b795ca73 100644 --- a/src/node_modules/api.js +++ b/src/node_modules/api.js @@ -157,6 +157,7 @@ var Api = { getSyncNotebooks: function(afterUsn, maxEntry, callback) { var me = this; var url = this.getUrl('notebook/getSyncNotebooks', {afterUsn: afterUsn, maxEntry: maxEntry}); + // console.log(url); needle.get(url, function(error, response) { me.checkError(error, response); @@ -215,17 +216,15 @@ var Api = { }, getLastSyncState: function(callback) { var me = this; - log('--getSyncState--') var url = this.getUrl('user/getSyncState'); - log(url); needle.get(url, function(error, response) { me.checkError(error, response); if(error) { return callback && callback(false); } var ret = response.body; - log('--getSyncState--ret---') - log(ret); + console.log('--getSyncState--ret---') + console.log(ret); if(Common.isOk(ret)) { callback && callback(ret); } else { diff --git a/src/node_modules/sync.js b/src/node_modules/sync.js index 818d957a..20fcb45e 100644 --- a/src/node_modules/sync.js +++ b/src/node_modules/sync.js @@ -622,36 +622,57 @@ var Sync = { // 没有上次同步的时间, 则需要进行一次全量同步, 不可能会发生 if(!lastSyncUsn) { console.error('getLastSyncState error!!'); + me.setSyncFinished(); return; } - // 同步笔记本 - me.syncNotebook(lastSyncUsn, function(ok) { - if(ok) { - console.log('-------incr notebook ok-----------' + lastSyncUsn); - me.addSyncProcessStatus(10); - // 同步笔记 - me.syncNote(lastSyncUsn, function(ok) { + // 先从服务器上得到usn, 与本地的判断, 是否需要pull + console.log('先从服务器上得到usn, 与本地的判断, 是否需要pull'); + Api.getLastSyncState(function(serverState) { + if(!serverState) { + console.error('get Server LastSyncState error!!'); + me.setSyncFinished(); + return; + } + console.log(serverState.LastSyncUsn + ' ' + lastSyncUsn); + if(serverState.LastSyncUsn > lastSyncUsn) { + // 需要同步笔记本 + console.log('需要pull'); + + // 同步笔记本 + me.syncNotebook(lastSyncUsn, function(ok) { if(ok) { - console.log('-------incr note ok-----------' + lastSyncUsn); - me.addSyncProcessStatus(30); - // 同步标签 - me.syncTag(lastSyncUsn, function() { - console.log('-------incr tag ok-----------' + lastSyncUsn); - me.addSyncProcessStatus(10); - // 更新上次同步时间 - me.updateLastSyncState(function() { - // send changes - me.sendChanges(again); - }); + console.log('-------incr notebook ok-----------' + lastSyncUsn); + me.addSyncProcessStatus(10); + // 同步笔记 + me.syncNote(lastSyncUsn, function(ok) { + if(ok) { + console.log('-------incr note ok-----------' + lastSyncUsn); + me.addSyncProcessStatus(30); + // 同步标签 + me.syncTag(lastSyncUsn, function() { + console.log('-------incr tag ok-----------' + lastSyncUsn); + me.addSyncProcessStatus(10); + // 更新上次同步时间 + me.updateLastSyncState(function() { + // send changes + me.sendChanges(again); + }); + }); + } else { + console.log('-------incr note not ok-----------') + me.fixConflicts(); + } }); } else { - console.log('-------incr note not ok-----------') me.fixConflicts(); } }); + } else { - me.fixConflicts(); + console.log('不需要pull'); + me.addSyncProcessStatus(50); + me.sendChanges(again); } }); }); diff --git a/src/public/js/app/note.js b/src/public/js/app/note.js index 094c823f..63774796 100644 --- a/src/public/js/app/note.js +++ b/src/public/js/app/note.js @@ -1203,6 +1203,7 @@ Note.syncFinished = function() { Note.hideSyncProgress(); }; +// 过时 Note.sync = function() { var me = this; me.showSpin(); @@ -1270,11 +1271,7 @@ Note.saveNote = function(e) { var num = e.which ? e.which : e.keyCode; // 保存 if((e.ctrlKey || e.metaKey) && num == 83 ) { // ctrl + s or command + s - Note.curChangedSaveIt(true, function(note) { - console.log('after updated:'); - console.log(note); - Note.sync(); - }); + incrSync(true); e.preventDefault(); return false; } else { @@ -2700,7 +2697,7 @@ $(function() { Note._syncWarningE = $('#syncWarning'); // sync Note._syncRefreshE.click(function() { - Note.sync(); + incrSync(true); }); Note._syncWarningE.click(function() { diff --git a/src/public/js/app/page.js b/src/public/js/app/page.js index e66b08b3..207f5acc 100644 --- a/src/public/js/app/page.js +++ b/src/public/js/app/page.js @@ -1100,10 +1100,21 @@ function fullSyncForce() { } // 增量同步 -function incrSync() { - log('incr sync'); +function _incrSync(saveNoteBefore) { + console.log('incr sync'); Note.showSpin(); SyncService.incrSync(); + Note.syncProgress(2); +} +function incrSync(saveNoteBefore) { + if(saveNoteBefore) { + Note.curChangedSaveIt(true, function() { + _incrSync(); + }); + } + else { + _incrSync(); + } } // 历史, 恢复原貌 @@ -1184,7 +1195,7 @@ var State = { // end // 打开时,同步一下 setTimeout(function() { - incrSync(); + incrSync(false); }, 500); initedCallback && initedCallback(); @@ -1868,7 +1879,7 @@ function userMenu() { this.sync = new gui.MenuItem({ label: getMsg('Sync now'), click: function(e) { - incrSync(); + incrSync(true); } }); this.fullSync = new gui.MenuItem({ diff --git a/src/public/js/common.js b/src/public/js/common.js index 255b1a1a..7f8794c0 100644 --- a/src/public/js/common.js +++ b/src/public/js/common.js @@ -339,6 +339,11 @@ function switchEditor(isMarkdown) { var previewToken = "
FORTOKEN
" var clearIntervalForSetContent; function setEditorContent(content, isMarkdown, preview) { + setTimeout(function() { + _setEditorContent(content, isMarkdown, preview); + }); +} +function _setEditorContent(content, isMarkdown, preview) { if(!content) { content = ""; }