From a60eb213d8cb57ea20a8edda75b8a9b58c3f4f43 Mon Sep 17 00:00:00 2001 From: life Date: Tue, 24 Nov 2015 12:27:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E5=86=85=E5=AE=B9=E7=9A=84?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E5=8F=AA=E5=9C=A8=E5=89=8D=E7=AB=AF=E5=8F=91?= =?UTF-8?q?=E8=B5=B7,=20=E6=AF=8F=E4=B8=AA=E6=9C=89800ms=E9=97=B4=E9=9A=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/leanote/desktop-app/issues/85 --- node_modules/note.js | 18 ++++--- public/js/app/note.js | 117 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 107 insertions(+), 28 deletions(-) diff --git a/node_modules/note.js b/node_modules/note.js index f99cc49c..b9a77509 100644 --- a/node_modules/note.js +++ b/node_modules/note.js @@ -636,7 +636,7 @@ var Note = { inSyncTimes: {}, // 10次就要再尝试了 getNoteContent: function(noteId, callback) { var me = this; - console.log('getNoteContent------' + noteId); + // console.trace('getNoteContent------' + noteId); // 如果是正在sync的话, 返回 /* if(me.inSyncContent[noteId]) { @@ -777,6 +777,7 @@ var Note = { }); }); }, + // 添加笔记本, note object // note是服务器传过来的, 需要处理下fix // NoteId, ServerNoteId, NotebookId(本地的) @@ -831,7 +832,8 @@ var Note = { Notebook.reCountNotebookNumberNotes(note.NotebookId); // 下载内容, 图片, 附件 - me.syncContentAndImagesAndAttachs(newDoc, 2000); + // 添加时不要, 这个请求由前端发出 + // me.syncContentAndImagesAndAttachs(newDoc, 2000); } }); }); @@ -924,13 +926,13 @@ var Note = { Notebook.getNotebookIdByServerNotebookId(note.NotebookId, function(localNotebookId) { note['NotebookId'] = localNotebookId; - console.log("updateNoteForce 后的") - console.log(note); - console.log(note.ServerNoteId + " " + note.IsDirty); + // console.log("updateNoteForce 后的") + // console.log(note); + // console.log(note.ServerNoteId + " " + note.IsDirty); - console.log('ever note'); - console.log(everNote.NoteId); - console.log(everNote); + // console.log('ever note'); + // console.log(everNote.NoteId); + // console.log(everNote); // 不要服务器上的 delete note['UpdatedTime']; diff --git a/public/js/app/note.js b/public/js/app/note.js index 3df10f1f..bb1d8b12 100644 --- a/public/js/app/note.js +++ b/public/js/app/note.js @@ -940,6 +940,10 @@ Note.renderNotes = function(notes, forNewNote, isShared) { Note.noteItemListO.html(""); // 清空 } + // 重置获取内容 + // console.trace('--------render notes-----------'); + Note.resetGetNoteContentLazy(); + // 20个一次 var len = notes.length; var c = Math.ceil(len/20); @@ -1022,8 +1026,9 @@ Note._renderNotes = function(notes, forNewNote, isShared, tang) { // 第几趟 continue; } + // 这里, 如果没有内容, 则添加到异步池中 if(note.InitSync) { - Note.getNoteContentLazy(note.NoteId); + Note.addGetNoteContentLazy(note.NoteId); } if(!note.Desc && note.Content) { @@ -1057,22 +1062,6 @@ Note._renderNotes = function(notes, forNewNote, isShared, tang) { // 第几趟 } Note.noteItemListO.append(tmp); - - /* - // 共享的note也放在Note的cache一份 - if(isShared) { - note.IsShared = true; // 注明是共享的 - } - - // 不清空 - // 之前是addNoteCache, 如果是搜索出的, 会把内容都重置了 - Note.setNoteCache(note, false); - - // 如果是共享的笔记本, 缓存也放在Share下 - if(isShared) { - Share.setCache(note); - } - */ } }; @@ -2088,17 +2077,105 @@ Note.contentSynced = function(noteId, content) { } }; +//------------------------ +// 异步加载note content + +// 池, 最大10个 +Note._loadContentPool = []; +Note._loadContentPoolSeq = 0; +Note._startedGetNoteContentLazy = false; +Note._stopGetNoteContentLazy = false; + +Note._loadContentRunSeq = 0; + +Note._loadContentStarted = {}; + +// 在render notes时 // 延迟加载内容 -Note.getNoteContentLazy = function(noteId) { - setTimeout(function() { + +// 重新render notes时, 重置pool +Note.resetGetNoteContentLazy = function() { + var me = this; + me._loadContentPool = []; + me._loadContentPoolSeq = 0; + me._stopGetNoteContentLazy = false; + me._startedGetNoteContentLazy = false; + me._loadContentRunSeq++; +}; + +// 添加到池子中 +Note.addGetNoteContentLazy = function(noteId) { + var me = this; + Note._loadContentPool.push(noteId); + me.startGetNoteContentLazy(); +}; + +// render notes后, +// 开始加载 +Note.startGetNoteContentLazy = function() { + var me = this; + + if (me._loadContentStarted[me._loadContentRunSeq]) { + return; + } + me._loadContentStarted[me._loadContentRunSeq] = true; + + me.getNoteContentLazy(me._loadContentRunSeq); +}; + +// 得到下一个要处理的noteId +Note._getNextNoteId = function () { + var me = this; + var noteId = me._loadContentPool[me._loadContentPoolSeq]; + me._loadContentPoolSeq++; + return noteId; +}; + +Note.getNoteContentLazy = function(runSeq) { + var me = this; + + // // 暂停了 + // if (me._stopGetNoteContentLazy) { + // return; + // } + + // 不是一个时候了 + if (runSeq != me._loadContentRunSeq) { + console.log('不是一个时候了 ' + runSeq + '_' + me._loadContentRunSeq); + return; + } + + var noteId = me._getNextNoteId(); + if (!noteId) { + return; + } + + var note = me.getNote(noteId); + if (note && !note.InitSync) { + console.log('不用加载'); + me.getNoteContentLazy(runSeq); + return; + } + + console.log('正在加载....' + noteId); + + setTimeout(function () { NoteService.getNoteContent(noteId, function(contentO) { if(typeof contentO == 'object') { Note.contentSynced(noteId, contentO.Content); + me.getNoteContentLazy(runSeq); } }); - }, 10); + }, 800); }; +Note.stopGetNoteContentLazy = function() { + var me = this; + me._stopGetNoteContentLazy = true; +}; + +// +//-------------- // 这里速度不慢, 很快 Note.getContextNotebooks = function(notebooks) {