1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-26 18:58:21 +02:00
This commit is contained in:
Laurent Cozic 2017-06-15 22:46:53 +01:00
parent ad436b5bb4
commit 4f64831f19
2 changed files with 53 additions and 107 deletions

View File

@ -20,12 +20,6 @@ class NoteFolderService extends BaseService {
} }
} }
// console.info(item);
// console.info(oldItem);
// console.info('DIFF', diff);
// return Promise.resolve();
let ItemClass = null; let ItemClass = null;
if (type == 'note') { if (type == 'note') {
ItemClass = Note; ItemClass = Note;
@ -79,38 +73,25 @@ class NoteFolderService extends BaseService {
}); });
} }
static itemsThatNeedSync(context = null, limit = 100) { static itemsThatNeedSync(limit = 100) {
if (!context) {
context = {
hasMoreFolders: true,
hasMoreNotes: true,
noteOffset: 0,
folderOffset: 0,
hasMore: true,
};
}
context.folderOffset = 0;
context.noteOffset = 0;
// Process folder first, then notes so that folders are created before // Process folder first, then notes so that folders are created before
// adding notes to them. However, it will be the opposite when deleting // adding notes to them. However, it will be the opposite when deleting
// folders (TODO). // folders (TODO).
if (context.hasMoreFolders) { function getFolders(limit) {
return BaseModel.db().selectAll('SELECT * FROM folders WHERE sync_time < updated_time LIMIT ' + limit + ' OFFSET ' + context.folderOffset).then((items) => { return BaseModel.db().selectAll('SELECT * FROM folders WHERE sync_time < updated_time LIMIT ' + limit);
context.hasMoreFolders = items.length >= limit;
context.folderOffset += items.length;
return { context: context, items: items };
});
} else {
return BaseModel.db().selectAll('SELECT * FROM notes WHERE sync_time < updated_time LIMIT ' + limit + ' OFFSET ' + context.noteOffset).then((items) => {
context.hasMoreNotes = items.length >= limit;
context.noteOffset += items.length;
context.hasMore = context.hasMoreNotes;
return { context: context, items: items };
});
} }
function getNotes(limit) {
return BaseModel.db().selectAll('SELECT * FROM notes WHERE sync_time < updated_time LIMIT ' + limit);
}
return getFolders(limit).then((items) => {
if (items.length) return { hasMore: true, items: items };
return getNotes(limit).then((items) => {
return { hasMore: items.length >= limit, items: items };
});
});
} }
} }

View File

@ -1,3 +1,5 @@
require('babel-plugin-transform-runtime');
import { Log } from 'src/log.js'; import { Log } from 'src/log.js';
import { Setting } from 'src/models/setting.js'; import { Setting } from 'src/models/setting.js';
import { Change } from 'src/models/change.js'; import { Change } from 'src/models/change.js';
@ -258,7 +260,7 @@ class Synchronizer {
} }
processSyncAction(action) { processSyncAction(action) {
console.info('Sync action: ', action); //console.info('Sync action: ', action);
//console.info('Sync action: ' + JSON.stringify(action)); //console.info('Sync action: ' + JSON.stringify(action));
if (!action) return Promise.resolve(); if (!action) return Promise.resolve();
@ -324,89 +326,52 @@ class Synchronizer {
return Promise.resolve(); // TODO return Promise.resolve(); // TODO
} }
processLocalItem(dbItem) { async processLocalItem(dbItem) {
let localItem = this.dbItemToSyncItem(dbItem); let localItem = this.dbItemToSyncItem(dbItem);
return this.api().stat(localItem.path).then((remoteItem) => { let remoteItem = await this.api().stat(localItem.path);
let action = this.syncAction(localItem, remoteItem, []); let action = this.syncAction(localItem, remoteItem, []);
return this.processSyncAction(action); await this.processSyncAction(action);
}).then(() => {
dbItem.sync_time = time.unix(); dbItem.sync_time = time.unix();
if (localItem.type == 'folder') { if (localItem.type == 'folder') {
return Folder.save(dbItem); return Folder.save(dbItem);
} else { } else {
return Note.save(dbItem); return Note.save(dbItem);
} }
});
} }
processRemoteItem(remoteItem) { async processRemoteItem(remoteItem) {
let remoteSyncItem = null; let content = await this.api().get(remoteItem.path);
return this.api().get(remoteItem.path).then((content) => { remoteItem.content = Note.fromFriendlyString(content);
remoteItem.content = Note.fromFriendlyString(content); let remoteSyncItem = this.remoteItemToSyncItem(remoteItem);
remoteSyncItem = this.remoteItemToSyncItem(remoteItem);
return BaseItem.loadItemByPath(remoteItem.path); let dbItem = await BaseItem.loadItemByPath(remoteItem.path);
}).then((dbItem) => { let localSyncItem = this.dbItemToSyncItem(dbItem);
let localSyncItem = this.dbItemToSyncItem(dbItem);
let action = this.syncAction(localSyncItem, remoteSyncItem, []); let action = this.syncAction(localSyncItem, remoteSyncItem, []);
return this.processSyncAction(action); return this.processSyncAction(action);
});
} }
processState_uploadChanges() { async processState_uploadChanges() {
return new Promise((resolve, reject) => { while (true) {
var context = null; let result = await NoteFolderService.itemsThatNeedSync(50);
let limit = 2; for (let i = 0; i < result.items.length; i++) {
let finishedReading = false; let item = result.items[i];
let isReading = false; await this.processLocalItem(item);
let readItems = () => {
isReading = true;
return NoteFolderService.itemsThatNeedSync(context, limit).then((result) => {
context = result.context;
let chain = [];
for (let i = 0; i < result.items.length; i++) {
let item = result.items[i];
chain.push(() => {
return this.processLocalItem(item);
});
}
return promiseChain(chain).then(() => {
if (!context.hasMore) finishedReading = true;
isReading = false;
});
}).catch((error) => {
rejec(error);
});
} }
let iid = setInterval(() => { if (!result.hasMore) break;
if (isReading) return; }
if (finishedReading) {
clearInterval(iid); return this.processState('downloadChanges');
resolve();
return;
}
readItems();
}, 100);
}).then(() => {
//console.info('DOWNLOAD DISABLED');
return this.processState('downloadChanges');
});
} }
processState_downloadChanges() { async processState_downloadChanges() {
return this.api().list().then((items) => { let items = await this.api().list();
let chain = []; for (let i = 0; i < items.length; i++) {
for (let i = 0; i < items.length; i++) { await this.processRemoteItem(items[i]);
chain.push(() => { }
return this.processRemoteItem(items[i]);
});
}
return promiseChain(chain);
});
} }
start() { start() {