mirror of
https://github.com/laurent22/joplin.git
synced 2025-01-11 18:24:43 +02:00
clean up
This commit is contained in:
parent
ad436b5bb4
commit
4f64831f19
@ -20,12 +20,6 @@ class NoteFolderService extends BaseService {
|
||||
}
|
||||
}
|
||||
|
||||
// console.info(item);
|
||||
// console.info(oldItem);
|
||||
// console.info('DIFF', diff);
|
||||
// return Promise.resolve();
|
||||
|
||||
|
||||
let ItemClass = null;
|
||||
if (type == 'note') {
|
||||
ItemClass = Note;
|
||||
@ -79,38 +73,25 @@ class NoteFolderService extends BaseService {
|
||||
});
|
||||
}
|
||||
|
||||
static itemsThatNeedSync(context = null, limit = 100) {
|
||||
if (!context) {
|
||||
context = {
|
||||
hasMoreFolders: true,
|
||||
hasMoreNotes: true,
|
||||
noteOffset: 0,
|
||||
folderOffset: 0,
|
||||
hasMore: true,
|
||||
};
|
||||
}
|
||||
|
||||
context.folderOffset = 0;
|
||||
context.noteOffset = 0;
|
||||
|
||||
static itemsThatNeedSync(limit = 100) {
|
||||
// Process folder first, then notes so that folders are created before
|
||||
// adding notes to them. However, it will be the opposite when deleting
|
||||
// folders (TODO).
|
||||
|
||||
if (context.hasMoreFolders) {
|
||||
return BaseModel.db().selectAll('SELECT * FROM folders WHERE sync_time < updated_time LIMIT ' + limit + ' OFFSET ' + context.folderOffset).then((items) => {
|
||||
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 getFolders(limit) {
|
||||
return BaseModel.db().selectAll('SELECT * FROM folders WHERE sync_time < updated_time LIMIT ' + limit);
|
||||
}
|
||||
|
||||
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 };
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
require('babel-plugin-transform-runtime');
|
||||
|
||||
import { Log } from 'src/log.js';
|
||||
import { Setting } from 'src/models/setting.js';
|
||||
import { Change } from 'src/models/change.js';
|
||||
@ -258,7 +260,7 @@ class Synchronizer {
|
||||
}
|
||||
|
||||
processSyncAction(action) {
|
||||
console.info('Sync action: ', action);
|
||||
//console.info('Sync action: ', action);
|
||||
//console.info('Sync action: ' + JSON.stringify(action));
|
||||
|
||||
if (!action) return Promise.resolve();
|
||||
@ -324,89 +326,52 @@ class Synchronizer {
|
||||
return Promise.resolve(); // TODO
|
||||
}
|
||||
|
||||
processLocalItem(dbItem) {
|
||||
async processLocalItem(dbItem) {
|
||||
let localItem = this.dbItemToSyncItem(dbItem);
|
||||
|
||||
return this.api().stat(localItem.path).then((remoteItem) => {
|
||||
let action = this.syncAction(localItem, remoteItem, []);
|
||||
return this.processSyncAction(action);
|
||||
}).then(() => {
|
||||
dbItem.sync_time = time.unix();
|
||||
if (localItem.type == 'folder') {
|
||||
return Folder.save(dbItem);
|
||||
} else {
|
||||
return Note.save(dbItem);
|
||||
}
|
||||
});
|
||||
let remoteItem = await this.api().stat(localItem.path);
|
||||
let action = this.syncAction(localItem, remoteItem, []);
|
||||
await this.processSyncAction(action);
|
||||
|
||||
dbItem.sync_time = time.unix();
|
||||
if (localItem.type == 'folder') {
|
||||
return Folder.save(dbItem);
|
||||
} else {
|
||||
return Note.save(dbItem);
|
||||
}
|
||||
}
|
||||
|
||||
processRemoteItem(remoteItem) {
|
||||
let remoteSyncItem = null;
|
||||
return this.api().get(remoteItem.path).then((content) => {
|
||||
remoteItem.content = Note.fromFriendlyString(content);
|
||||
remoteSyncItem = this.remoteItemToSyncItem(remoteItem);
|
||||
return BaseItem.loadItemByPath(remoteItem.path);
|
||||
}).then((dbItem) => {
|
||||
let localSyncItem = this.dbItemToSyncItem(dbItem);
|
||||
let action = this.syncAction(localSyncItem, remoteSyncItem, []);
|
||||
return this.processSyncAction(action);
|
||||
});
|
||||
async processRemoteItem(remoteItem) {
|
||||
let content = await this.api().get(remoteItem.path);
|
||||
remoteItem.content = Note.fromFriendlyString(content);
|
||||
let remoteSyncItem = this.remoteItemToSyncItem(remoteItem);
|
||||
|
||||
let dbItem = await BaseItem.loadItemByPath(remoteItem.path);
|
||||
let localSyncItem = this.dbItemToSyncItem(dbItem);
|
||||
|
||||
let action = this.syncAction(localSyncItem, remoteSyncItem, []);
|
||||
return this.processSyncAction(action);
|
||||
}
|
||||
|
||||
processState_uploadChanges() {
|
||||
return new Promise((resolve, reject) => {
|
||||
var context = null;
|
||||
let limit = 2;
|
||||
let finishedReading = false;
|
||||
let isReading = false;
|
||||
|
||||
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);
|
||||
});
|
||||
async processState_uploadChanges() {
|
||||
while (true) {
|
||||
let result = await NoteFolderService.itemsThatNeedSync(50);
|
||||
for (let i = 0; i < result.items.length; i++) {
|
||||
let item = result.items[i];
|
||||
await this.processLocalItem(item);
|
||||
}
|
||||
|
||||
let iid = setInterval(() => {
|
||||
if (isReading) return;
|
||||
if (finishedReading) {
|
||||
clearInterval(iid);
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
readItems();
|
||||
}, 100);
|
||||
}).then(() => {
|
||||
//console.info('DOWNLOAD DISABLED');
|
||||
return this.processState('downloadChanges');
|
||||
});
|
||||
if (!result.hasMore) break;
|
||||
}
|
||||
|
||||
return this.processState('downloadChanges');
|
||||
}
|
||||
|
||||
processState_downloadChanges() {
|
||||
return this.api().list().then((items) => {
|
||||
let chain = [];
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
chain.push(() => {
|
||||
return this.processRemoteItem(items[i]);
|
||||
});
|
||||
}
|
||||
return promiseChain(chain);
|
||||
});
|
||||
async processState_downloadChanges() {
|
||||
let items = await this.api().list();
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
await this.processRemoteItem(items[i]);
|
||||
}
|
||||
}
|
||||
|
||||
start() {
|
||||
|
Loading…
Reference in New Issue
Block a user