网络断开时, auth检查

sync还有问题
This commit is contained in:
life
2015-02-11 23:50:02 +08:00
parent 225bcd7cf0
commit 269f68620c
13 changed files with 322 additions and 72 deletions

103
node_modules/sync.js generated vendored
View File

@@ -111,7 +111,7 @@ var Sync = {
function canCall() {
// 是最后一块, 且
me._addSyncNotebookNum();
log(me._totalHasSyncNotebookNum + ' ' + me._totalSyncNotebookNum);
console.log(me._totalHasSyncNotebookNum + ' ' + me._totalSyncNotebookNum);
if(me._syncNotebookIsLastChunk && me._totalHasSyncNotebookNum >= me._totalSyncNotebookNum) {
// 防止多次callback
if(!me._syncInfo.notebook.ok) {
@@ -136,8 +136,8 @@ var Sync = {
// 1) 服务器端删除了, 本地肯定删除
if(notebook.IsDeleted) {
log('delete: ');
log(notebook);
console.log('delete: ');
console.log(notebook);
Notebook.getNotebookIdByServerNotebookId(notebookId, function(localNotebookId) {
Notebook.deleteNotebookForce(notebookId, function() {
me._syncInfo.notebook.deletes.push(localNotebookId);
@@ -150,7 +150,7 @@ var Sync = {
Notebook.getNotebookByServerNotebookId(notebookId, function(notebookLocal) {
// 2.1 本地没有, 表示是新建
if(!notebookLocal) {
log('add: ...')
console.log('add: ...')
// TODO
Notebook.addNotebookForce(notebook, function(notebook) {
me._syncInfo.notebook.adds.push(notebook);
@@ -159,7 +159,7 @@ var Sync = {
} else {
// 2.2 本地是否修改了, 需要合并, 使用服务端的数据
if(notebook.IsDirty) {
log('冲突....')
console.log('冲突....')
// 2.3 服务器是最新的, 用服务器的
} else {
}
@@ -179,8 +179,8 @@ var Sync = {
syncNotebook: function(afterUsn, callback) {
var me = this;
Api.getSyncNotebooks(afterUsn, me._notebookMaxEntry, function(notebooks) {
log('syncNotebook')
log(notebooks);
console.log('syncNotebook')
console.log(notebooks);
if(Common.isOk(notebooks)) {
me._totalSyncNotebookNum += notebooks.length;
// 证明可能还有要同步的
@@ -189,7 +189,7 @@ var Sync = {
var last = notebooks[notebooks.length-1];
me.syncNotebook(last.Usn, callback);
} else {
log('no more');
console.log('no more');
me._syncNotebookIsLastChunk = true;
me._syncNotebookToLocal(notebooks, callback);
}
@@ -488,52 +488,73 @@ var Sync = {
var afterInfo = me._syncInfo;
log('处理冲突....');
log(me._syncInfo);
if(me._notebookWeb) {
Notebook.fixConflicts(me._syncInfo.notebook, me._notebookWeb);
}
if(me._noteWeb) {
Note.fixConflicts(me._syncInfo.note, me._noteWeb, function() {
// 如果是incSync, 则要前端处理
if(me.incrSyncStart) {
Notebook.fixConflicts(me._syncInfo.notebook);
Note.fixConflicts(me._syncInfo.note, function() {
// 避免无限循环, 别send changes了
if(!me._needIncrSyncAgain) {
// alert("?")
console.log(">>>>>>>>>>>>>>>>>>>>>>>>>")
console.log(">>>>>>>>_needIncrSyncAgain>>>>>>>>>>>>>>>>>")
// send changes
callback && callback();
}
});
// 添加或删除一些tag
Web.addOrDeleteTagFromSync(me._syncInfo.tag);
}
},
fixConflictsForSendChanges: function(callback) {
var me = this;
me.fixConflicts(function() {
callback();
// 已结束
Web.syncFinished();
me.incrSyncStart = false;
})
},
// 增量同步
incrSyncStart: false,
// 如果第一次insync, 网络错误导致incrSyncStart不结束, 第二次就会永远转动
setSyncFinished: function() {
var me = this;
me.incrSyncStart = false;
},
incrSync: function() {
var me = this;
me._initSyncInfo();
if(me.incrSyncStart) {
return;
}
// 添加或删除一些tag
Web.addOrDeleteTagFromSync(me._syncInfo.tag);
},
me.incrSyncStart = true;
// 增量同步
incrSync: function(notebookWeb, noteWeb) {
var me = this;
me._notebookWeb = notebookWeb;
me._noteWeb = noteWeb;
me._initSyncInfo();
log('inc sync start');
console.log('inc sync start');
// 得到当前LastSyncUsn
User.getLastSyncState(function(lastSyncUsn, lastSyncTime) {
// 没有上次同步的时间, 则需要进行一次全量同步, 不可能会发生
if(!lastSyncUsn) {
log('error!!');
console.log('error!!');
return;
}
// 同步笔记本
me.syncNotebook(lastSyncUsn, function(ok) {
if(ok) {
log('-------incr notebook ok-----------' + lastSyncUsn)
console.log('-------incr notebook ok-----------' + lastSyncUsn)
// 同步笔记
me.syncNote(lastSyncUsn, function(ok) {
if(ok) {
log('-------incr note ok-----------' + lastSyncUsn)
console.log('-------incr note ok-----------' + lastSyncUsn)
// 同步标签
me.syncTag(lastSyncUsn, function() {
log('-------incr tag ok-----------' + lastSyncUsn)
console.log('-------incr tag ok-----------' + lastSyncUsn)
// 更新上次同步时间
me.updateLastSyncState(function() {
// send changes
@@ -541,7 +562,7 @@ var Sync = {
});
});
} else {
log('-------incr note not ok-----------')
console.log('-------incr note not ok-----------')
me.fixConflicts();
}
});
@@ -561,8 +582,8 @@ var Sync = {
var me = this;
// 获取所有笔记本的更改
Notebook.getDirtyNotebooks(function(notebooks) {
log('dirty notebooks');
log(notebooks);
console.log('dirty notebooks');
console.log(notebooks);
if(!notebooks) {
callback && callback();
} else {
@@ -648,8 +669,8 @@ var Sync = {
var me = this;
// 获取所有笔记本的更改
Note.getDirtyNotes(function(notes) {
log('dirty notes');
log(notes);
console.error('>>>>>>>>>>>> dirty notes <<<<<<<<<<<<<<< ');
console.log(notes);
if(!notes) {
callback && callback();
} else {
@@ -690,7 +711,7 @@ var Sync = {
console.log(ret);
if(typeof ret == 'object') {
if(ret.Msg == 'conflict') {
console.log('updateNote 冲突-----------');
console.error('updateNote 冲突-----------');
me._syncInfo.note.changeConflicts.push(note);
}
else if(ret.Msg == 'notExists') {
@@ -766,8 +787,8 @@ var Sync = {
var me = this;
// 获取所有笔记本的更改
Tag.getDirtyTags(function(tags) {
log('dirty tags');
log(tags);
console.log('dirty tags');
console.log(tags);
if(!tags) {
callback && callback();
} else {
@@ -804,11 +825,11 @@ var Sync = {
sendChanges: function() {
var me = this;
log('send changes before...')
console.log('send changes before...')
// 先处理冲突, 可以同时进行
me.fixConflicts(function() {
// send changes
log('send changes');
console.log('send changes');
me._initSyncInfo(); // 重新初始化[]
async.series([
function(cb) {
@@ -823,11 +844,11 @@ var Sync = {
], function() {
// 重新再来一次增量同步
if(me._needIncrSyncAgain) {
log('-- _needIncrSyncAgain -- ')
console.log('-- _needIncrSyncAgain -- ')
me.incrSync(me._notebookWeb, me._noteWeb);
} else {
log('send changes 后解决冲突');
me.fixConflicts();
console.log('send changes 后解决冲突');
me.fixConflictsForSendChanges(function(){});
}
});
});