diff --git a/web/src/lib/client.ts b/web/src/lib/client.ts index c278094..7bbea79 100644 --- a/web/src/lib/client.ts +++ b/web/src/lib/client.ts @@ -73,6 +73,7 @@ interface FullVersionResponse { createdAt: Date } +// Some of these could just be local variables as they're not used globally export const currentUser: Writable = writable(null) export const currentFullNote: Writable = writable(null) export const availableNotes: Writable = writable(null) @@ -87,6 +88,8 @@ class ApiClient { private lastAtUpdate = new Date(0) private lastCsrfUpdate = new Date(0) private refreshInProgress = false + private loadedNotesCache = new Map() + // TODO: private loadedVersionsCache = new Map() constructor(baseUrl: string) { this.baseUrl = baseUrl @@ -543,6 +546,12 @@ class ApiClient { throw new Error("Invalid note ID format.") } + const cachedNote = this.loadedNotesCache.get(noteID) + if (cachedNote != null) { + console.log(`cache hit ${noteID}`) + return cachedNote + } + return this.handleRequest( async () => { const response = await fetch(`${this.baseUrl}/notes/${noteID}`, { @@ -556,7 +565,8 @@ class ApiClient { }) const note = this.deserializeFullNote(data) - console.log(note) + console.log(`caching ${noteID}`) + this.loadedNotesCache.set(noteID, note) return note }, diff --git a/web/src/lib/components/NoteView.svelte b/web/src/lib/components/NoteView.svelte index 9cb6123..fc8e301 100644 --- a/web/src/lib/components/NoteView.svelte +++ b/web/src/lib/components/NoteView.svelte @@ -102,9 +102,6 @@ } const selectNote = async (noteId: string) => { - // TODO: check from cache props first before requesting from API - - console.log(`loading ${noteId}`) const note = await apiClient.getFullNote(noteId) if (note) { diff --git a/web/src/lib/components/Sidebar.svelte b/web/src/lib/components/Sidebar.svelte index 7e549ad..a1ec4eb 100644 --- a/web/src/lib/components/Sidebar.svelte +++ b/web/src/lib/components/Sidebar.svelte @@ -15,8 +15,6 @@ export let createNewNote: () => Promise export let selectNote: (noteId: string) => Promise - // TODO: add caching props -> e.g. `export const previousNotes: FullNote[]` - const formatDate = (dateString: string | Date): string => { if (!dateString) { return "" @@ -51,8 +49,7 @@ : notes - - +