core: get rid of noteIds in notebook topics

This is a BREAKING change in the core & will require updating the
clients. The way it works is instead of keeping noteIds of all the
notes in the topic, it keeps an in-memory cache. This in-memory
cache `topicReferences` lives in the notes collection & decouples
notes from notebooks/topics. This also simplifies all the different
actions where references would persist after the note was deleted.
Since the note acts as the source of truth for where it currently is,
there is nothing else to do except rebuild the `topicReferences`
cache.
This commit is contained in:
Abdullah Atta
2022-09-06 22:57:54 +05:00
committed by Abdullah Atta
parent ab38d89314
commit 201366b39e
14 changed files with 238 additions and 275 deletions

View File

@@ -22,7 +22,6 @@ import Notebook from "../models/notebook";
import getId from "../utils/id";
import { CHECK_IDS, checkIsUserPremium } from "../common";
import { qclone } from "qclone";
import setManipulator from "../utils/set";
export default class Notebooks extends Collection {
async merge(remoteNotebook) {
@@ -36,7 +35,6 @@ export default class Notebooks extends Collection {
const lastSyncedTimestamp = await this._db.lastSynced();
let isChanged = false;
// merge new and old topics
// We need to handle 3 cases:
for (let oldTopic of localNotebook.topics) {
const newTopicIndex = remoteNotebook.topics.findIndex(
(t) => t.id === oldTopic.id
@@ -60,25 +58,10 @@ export default class Notebooks extends Collection {
else if (newTopic && oldTopic.dateEdited > newTopic.dateEdited) {
remoteNotebook.topics[newTopicIndex] = {
...oldTopic,
notes: setManipulator.union(oldTopic.notes, newTopic.notes),
dateEdited: Date.now()
};
isChanged = true;
}
// CASE 4: if topic exists in both notebooks:
// if newTopic.dateEdited > oldTopic.dateEdited: we iterate
// on all notes that are not in newTopic (if any)
// and dereference them.
else if (newTopic && newTopic.dateEdited > oldTopic.dateEdited) {
const removedNotes = setManipulator.complement(
oldTopic.notes,
newTopic.notes
);
await this.notebook(remoteNotebook.id)
.topics.topic(oldTopic.id)
.delete(...removedNotes);
}
}
remoteNotebook.remote = !isChanged;
}
@@ -163,20 +146,10 @@ export default class Notebooks extends Collection {
let notebook = this.notebook(id);
if (!notebook) continue;
const notebookData = qclone(notebook.data);
await notebook.topics.delete(...notebook.data.topics);
// await notebook.topics.delete(...notebook.data.topics);
await this._collection.removeItem(id);
await this._db.settings.unpin(id);
await this._db.trash.add(notebookData);
}
}
async repairReferences() {
for (let notebook of this.all) {
const _notebook = this.notebook(notebook);
for (let topic of notebook.topics) {
const _topic = _notebook.topics.topic(topic.id);
await _topic.add(...topic.notes);
}
}
}
}