Keep all notes in memory

This commit is contained in:
Adrian Gruntkowski 2025-02-08 00:42:54 +01:00
parent ec998dccc4
commit fb80d4e1f9
2 changed files with 38 additions and 26 deletions

View file

@ -34,6 +34,7 @@ const isLoggedIn = !!document.cookie
// Network sync is only enabled is user is logged in. // Network sync is only enabled is user is logged in.
const endpoint = isLoggedIn ? "/" : null; const endpoint = isLoggedIn ? "/" : null;
const Notes = new SyncedNoteStore("jenot-app", endpoint); const Notes = new SyncedNoteStore("jenot-app", endpoint);
await Notes.init();
// Reset metadata to force full sync // Reset metadata to force full sync
if (URL_PARAMS.has("reset-meta")) { if (URL_PARAMS.has("reset-meta")) {
@ -181,7 +182,7 @@ async function render() {
}); });
currentNotes = {}; currentNotes = {};
notes.forEach((n) => (currentNotes[n.id] = n)); notes.forEach((n) => (currentNotes[n.id] = structuredClone(n)));
} }
function renderNote(note) { function renderNote(note) {

View file

@ -1,4 +1,5 @@
let databases = {}; let databases = {};
let memory = {};
async function connect(dbName) { async function connect(dbName) {
if (!databases[dbName]) { if (!databases[dbName]) {
@ -110,8 +111,20 @@ export class SyncedNoteStore extends EventTarget {
this.webStore = webStore || (endpoint && new WebNoteStore(endpoint)); this.webStore = webStore || (endpoint && new WebNoteStore(endpoint));
} }
async all(since, includeDeleted) { async init() {
await this.fetchAll().then((allNotes) => {
memory[this.dbName] = {};
memory[this.dbName][this.storeName] = {};
allNotes.forEach((note) => {
memory[this.dbName][this.storeName][note.id] = note;
});
});
}
async fetchAll() {
const that = this; const that = this;
return connect(this.dbName).then( return connect(this.dbName).then(
(db) => (db) =>
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
@ -119,34 +132,26 @@ export class SyncedNoteStore extends EventTarget {
.transaction([that.storeName], "readonly") .transaction([that.storeName], "readonly")
.objectStore(that.storeName) .objectStore(that.storeName)
.getAll().onsuccess = (data) => { .getAll().onsuccess = (data) => {
const results = data.target.result return resolve(data.target.result);
.filter((n) => (includeDeleted || !n.deleted) && n.id !== "meta")
.toSorted((a, b) => b.created - a.created);
if (since > 0) {
return resolve(results.filter((n) => n.updated > since));
}
return resolve(results);
}; };
}), }),
); );
} }
async get(id) { async all(since, includeDeleted) {
const that = this; const results = Object.values(memory[this.dbName][this.storeName])
let result; .filter((n) => (includeDeleted || !n.deleted) && n.id !== "meta")
.toSorted((a, b) => b.created - a.created);
return connect(this.dbName).then( if (since > 0) {
(db) => return results.filter((n) => n.updated > since);
new Promise( }
(resolve, reject) =>
(db return results;
.transaction([that.storeName], "readonly") }
.objectStore(that.storeName)
.get(id).onsuccess = (data) => resolve(data.target.result)), async get(id) {
), return memory[this.dbName][this.storeName][id];
);
} }
async getMeta() { async getMeta() {
@ -192,7 +197,10 @@ export class SyncedNoteStore extends EventTarget {
), ),
) )
.then(() => { .then(() => {
(async () => this.webStore?.add(entry))(); memory[this.dbName][this.storeName][entry.id] = structuredClone(entry);
this.webStore?.add(entry);
return entry; return entry;
}); });
} }
@ -224,7 +232,10 @@ export class SyncedNoteStore extends EventTarget {
), ),
) )
.then(() => { .then(() => {
(async () => (skipNetwork ? null : this.webStore?.add(note)))(); memory[this.dbName][this.storeName][note.id] = structuredClone(note);
if (!skipNetwork) this.webStore?.add(note);
return note; return note;
}); });
} }