store has a first copy of readme from cache is no waiting

This commit is contained in:
Julien Calixte
2023-08-13 21:07:17 +02:00
parent a741c7e1eb
commit 656e4b14a9
4 changed files with 33 additions and 17 deletions

View File

@@ -1,8 +1,8 @@
import { ref } from 'vue'
import { useMarkdown } from '@/hooks/useMarkdown.hook' import { useMarkdown } from '@/hooks/useMarkdown.hook'
import { useNoteCache } from '@/modules/note/hooks/useNoteCache' import { useNoteCache } from '@/modules/note/hooks/useNoteCache'
import { useUserRepoStore } from '@/modules/repo/store/userRepo.store'
import { getFileContent } from '@/modules/repo/services/repo' import { getFileContent } from '@/modules/repo/services/repo'
import { useUserRepoStore } from '@/modules/repo/store/userRepo.store'
import { ref } from 'vue'
export const useFile = (sha: string, retrieveContent = true) => { export const useFile = (sha: string, retrieveContent = true) => {
const { render } = useMarkdown(sha) const { render } = useMarkdown(sha)
@@ -47,6 +47,7 @@ export const useFile = (sha: string, retrieveContent = true) => {
if (!contentFile) { if (!contentFile) {
return null return null
} }
saveCacheNote(contentFile) saveCacheNote(contentFile)
return contentFile return contentFile
} }

View File

@@ -1,30 +1,22 @@
import { data } from '@/data/data' import { data } from '@/data/data'
import { DataType } from '@/data/DataType.enum' import { DataType } from '@/data/DataType.enum'
import { Note } from '@/modules/note/models/Note' import { Note } from '@/modules/note/models/Note'
import { useAsyncState } from '@vueuse/core'
import { computed } from 'vue'
export const useNoteCache = (sha: string) => { export const useNoteCache = (sha: string) => {
const noteId = computed(() => data.generateId(DataType.Note, sha)) const noteId = data.generateId(DataType.Note, sha)
const getCachedNote = async () => data.get<DataType.Note, Note>(noteId.value) const getCachedNote = async () => data.get<DataType.Note, Note>(noteId)
const cachedNote = useAsyncState(getCachedNote, null)
const saveCacheNote = async (content: string) => { const saveCacheNote = async (content: string) => {
const newNote: Note = { const newNote: Note = {
_id: noteId.value, _id: noteId,
$type: DataType.Note, $type: DataType.Note,
content content
} }
await data.update(newNote) await data.update(newNote)
await cachedNote.execute()
} }
return { return {
cachedNote: cachedNote.state,
isReady: cachedNote.isReady,
getCachedNote, getCachedNote,
saveCacheNote saveCacheNote
} }

View File

@@ -37,6 +37,22 @@ export const getFiles = async (
return treeResponse?.data.tree.filter((t) => t.type === 'blob') ?? [] return treeResponse?.data.tree.filter((t) => t.type === 'blob') ?? []
} }
export const getCachedMainReadme = async (owner: string, repo: string) => {
if (!owner || !repo) {
return null
}
const { render } = useMarkdown()
const { getCachedNote } = useNoteCache(`${owner}-${repo}-README`)
const cachedReadme = await getCachedNote()
if (!cachedReadme) {
return null
}
return render(cachedReadme.content)
}
export const getMainReadme = async (owner: string, repo: string) => { export const getMainReadme = async (owner: string, repo: string) => {
if (!owner || !repo) { if (!owner || !repo) {
return null return null
@@ -47,8 +63,6 @@ export const getMainReadme = async (owner: string, repo: string) => {
`${owner}-${repo}-README` `${owner}-${repo}-README`
) )
const cachedReadme = await getCachedNote()
try { try {
const octokit = await getOctokit() const octokit = await getOctokit()
@@ -63,6 +77,8 @@ export const getMainReadme = async (owner: string, repo: string) => {
} }
} catch (error) { } catch (error) {
console.warn(error) console.warn(error)
const cachedReadme = await getCachedNote()
if (cachedReadme) { if (cachedReadme) {
return render(cachedReadme.content) return render(cachedReadme.content)
} }

View File

@@ -1,6 +1,7 @@
import { RepoFile } from '@/modules/repo/interfaces/RepoFile' import { RepoFile } from '@/modules/repo/interfaces/RepoFile'
import { UserSettings } from '@/modules/repo/interfaces/UserSettings' import { UserSettings } from '@/modules/repo/interfaces/UserSettings'
import { import {
getCachedMainReadme,
getFiles, getFiles,
getMainReadme, getMainReadme,
getUserSettingsContent getUserSettingsContent
@@ -36,14 +37,20 @@ export const useUserRepoStore = defineStore({
} catch (error) { } catch (error) {
console.warn('impossible to refresh token') console.warn('impossible to refresh token')
} }
const [cachedReadme] = await Promise.all([
getCachedMainReadme(newUser, newRepo)
])
this.readme = cachedReadme
const [readme, files] = await Promise.all([ const [readme, files] = await Promise.all([
getMainReadme(newUser, newRepo), getMainReadme(newUser, newRepo),
getFiles(newUser, newRepo) getFiles(newUser, newRepo)
]) ])
this.userSettings = await getUserSettingsContent(newUser, newRepo, files)
this.readme = readme this.readme = readme
this.files = files this.files = files
this.userSettings = await getUserSettingsContent(newUser, newRepo, files)
}, },
resetUserRepo() { resetUserRepo() {
this.user = '' this.user = ''