fix: handle second nb merge case correctly

we were comparing against nb.dateEdited instead of db.lastSynced
This commit is contained in:
thecodrr
2021-02-25 20:23:51 +05:00
parent bb5aeb7bd6
commit 9aeb6fe1dd
2 changed files with 6 additions and 3 deletions

View File

@@ -117,6 +117,8 @@ test("merge notebook with topic removed that is edited in the local notebook", (
const newNotebook = { ...notebook.data, remote: true }; const newNotebook = { ...notebook.data, remote: true };
newNotebook.topics.splice(1, 1); // remove hello topic newNotebook.topics.splice(1, 1); // remove hello topic
await StorageInterface.write("lastSynced", Date.now());
await delay(500); await delay(500);
await notebook.topics.add({ await notebook.topics.add({

View File

@@ -11,8 +11,9 @@ export default class Notebooks extends Collection {
let localNotebook = this._collection.getItem(id); let localNotebook = this._collection.getItem(id);
if (localNotebook && localNotebook.topics?.length) { if (localNotebook && localNotebook.topics?.length) {
// merge new and old topics const lastSyncedTimestamp = await this._db.lastSynced();
// merge new and old topics
// We need to handle 3 cases: // We need to handle 3 cases:
for (let oldTopic of localNotebook.topics) { for (let oldTopic of localNotebook.topics) {
const newTopicIndex = remoteNotebook.topics.findIndex( const newTopicIndex = remoteNotebook.topics.findIndex(
@@ -21,9 +22,9 @@ export default class Notebooks extends Collection {
const newTopic = remoteNotebook.topics[newTopicIndex]; const newTopic = remoteNotebook.topics[newTopicIndex];
// CASE 1: if topic exists in old notebook but not in new notebook, it's deleted. // CASE 1: if topic exists in old notebook but not in new notebook, it's deleted.
// However, if the dateEdited of topic in the old notebook is > the dateEdited of newNotebook // However, if the dateEdited of topic in the old notebook is > lastSyncedTimestamp
// it was newly added or edited so add it to the new notebook. // it was newly added or edited so add it to the new notebook.
if (!newTopic && oldTopic.dateEdited > remoteNotebook.dateEdited) { if (!newTopic && oldTopic.dateEdited > lastSyncedTimestamp) {
remoteNotebook.topics.push(oldTopic); remoteNotebook.topics.push(oldTopic);
} }