From 84789393ad516562e89c77e987d6ad19df98d0c5 Mon Sep 17 00:00:00 2001 From: Julien Calixte Date: Sun, 27 Oct 2024 12:33:26 +0100 Subject: [PATCH] fix: distinct multiple layer of cache --- src/hooks/useFile.hook.ts | 27 ++++++++++++++++------ src/hooks/useOfflineNote.hook.ts | 4 ++-- src/modules/note/cache/prepareNoteCache.ts | 26 +++++++++++++++++---- src/modules/repo/services/repo.ts | 4 ++-- 4 files changed, 46 insertions(+), 15 deletions(-) diff --git a/src/hooks/useFile.hook.ts b/src/hooks/useFile.hook.ts index 3299bd7..c1cf4c3 100644 --- a/src/hooks/useFile.hook.ts +++ b/src/hooks/useFile.hook.ts @@ -18,19 +18,20 @@ export const useFile = (sha: Ref | string, retrieveContent = true) => { renderFromUTF8, getRawContent: getRawContentFromFile } = useMarkdown(toValue(sha)) + const { getCachedNote, saveCacheNote } = prepareNoteCache( toValue(sha), toValue(path) ) - const fromCache = ref(false) + const fromCache = ref(false) const rawContent = ref('') const content = computed(() => rawContent.value ? renderFromUTF8(rawContent.value) : '' ) const getEditedSha = async () => { - const note = await getCachedNote() + const { note } = await getCachedNote() if (!note) { return null @@ -40,26 +41,38 @@ export const useFile = (sha: Ref | string, retrieveContent = true) => { } const getCachedFileContent = async (): Promise => { - const cachedNote = await getCachedNote() + const { note: cachedNote, from } = await getCachedNote() fromCache.value = !!cachedNote if (cachedNote) { + if (from === 'path') { + queryFileContent(store.user, store.repo, toValue(sha)).then( + (fileContent) => { + if (!fileContent) { + return + } + saveCacheNote(fileContent) + rawContent.value = getRawContentFromFile(fileContent) + } + ) + } + return cachedNote.content } - const contentFile = await queryFileContent( + const fileContent = await queryFileContent( store.user, store.repo, toValue(sha) ) - if (!contentFile) { + if (!fileContent) { return null } - saveCacheNote(contentFile) - return contentFile + saveCacheNote(fileContent) + return fileContent } const getRawContent = async () => { diff --git a/src/hooks/useOfflineNote.hook.ts b/src/hooks/useOfflineNote.hook.ts index e8a6223..4e6a8f1 100644 --- a/src/hooks/useOfflineNote.hook.ts +++ b/src/hooks/useOfflineNote.hook.ts @@ -32,9 +32,9 @@ export const useOfflineNote = () => { file.path ) - const isNoteCached = (await getCachedNote()) !== null + const { from } = await getCachedNote() - if (isNoteCached) { + if (from === 'sha') { continue } diff --git a/src/modules/note/cache/prepareNoteCache.ts b/src/modules/note/cache/prepareNoteCache.ts index c655773..4ab4fc5 100644 --- a/src/modules/note/cache/prepareNoteCache.ts +++ b/src/modules/note/cache/prepareNoteCache.ts @@ -3,23 +3,41 @@ import { DataType } from '@/data/DataType.enum' import { Note } from '@/modules/note/models/Note' import { useUserRepoStore } from '@/modules/repo/store/userRepo.store' +type NoteCacheResult = + | { + note: Note + from: 'sha' + } + | { note: Note; from: 'path' } + | { note: null; from: null } + export const prepareNoteCache = (sha: string, path?: string) => { const store = useUserRepoStore() const noteId = data.generateId(DataType.Note, sha) const notePath = path ? data.generateId(DataType.Note, path) : null - const getCachedNote = async () => { + const getCachedNote = async (): Promise => { const note = await data.get(noteId) if (note) { - return note + return { note, from: 'sha' } } if (notePath) { - return data.get(notePath) + const note = await data.get(notePath) + if (!note) { + return { + note: null, + from: null + } + } + return { + note, + from: 'path' + } } - return null + return { note: null, from: null } } const saveCacheNote = async ( diff --git a/src/modules/repo/services/repo.ts b/src/modules/repo/services/repo.ts index 87fb766..5381045 100644 --- a/src/modules/repo/services/repo.ts +++ b/src/modules/repo/services/repo.ts @@ -44,7 +44,7 @@ export const getCachedMainReadme = async (owner: string, repo: string) => { const { render } = useMarkdown() const { getCachedNote } = prepareNoteCache(`${owner}-${repo}-README`) - const cachedReadme = await getCachedNote() + const { note: cachedReadme } = await getCachedNote() if (!cachedReadme) { return null @@ -77,7 +77,7 @@ export const getMainReadme = async (owner: string, repo: string) => { } } catch (error) { console.warn(error) - const cachedReadme = await getCachedNote() + const { note: cachedReadme } = await getCachedNote() if (cachedReadme) { return render(cachedReadme.content)