1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-06-15 23:00:36 +02:00

Fixed delta API for sync

This commit is contained in:
Laurent Cozic
2017-07-18 21:03:07 +01:00
parent 927894e940
commit 0c30c1b70b
11 changed files with 525 additions and 443 deletions

View File

@ -12,6 +12,7 @@ class FileApiDriverMemory {
constructor() {
this.items_ = [];
this.deletedItems_ = [];
}
itemIndexByPath(path) {
@ -102,6 +103,10 @@ class FileApiDriverMemory {
delete(path) {
let index = this.itemIndexByPath(path);
if (index >= 0) {
let item = Object.assign({}, this.items_[index]);
item.isDeleted = true;
item.updated_time = time.unixMs();
this.deletedItems_.push(item);
this.items_.splice(index, 1);
}
return Promise.resolve();
@ -120,6 +125,52 @@ class FileApiDriverMemory {
return Promise.resolve();
}
async delta(path, options = null) {
let limit = 3;
let output = {
hasMore: false,
context: {},
items: [],
};
let context = options ? options.context : null;
let fromTime = 0;
if (context) fromTime = context.fromTime;
let sortedItems = this.items_.slice().concat(this.deletedItems_);
sortedItems.sort((a, b) => {
if (a.updated_time < b.updated_time) return -1;
if (a.updated_time > b.updated_time) return +1;
return 0;
});
let hasMore = false;
let items = [];
let maxTime = 0;
for (let i = 0; i < sortedItems.length; i++) {
let item = sortedItems[i];
if (item.updated_time >= fromTime) {
item = Object.assign({}, item);
item.path = item.path.substr(path.length + 1);
items.push(item);
if (item.updated_time > maxTime) maxTime = item.updated_time;
}
if (items.length >= limit) {
hasMore = true;
break;
}
}
output.items = items;
output.hasMore = hasMore;
output.context = { fromTime: maxTime };
return output;
}
}
export { FileApiDriverMemory };