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 { useNoteCache } from '@/modules/note/hooks/useNoteCache'
import { useUserRepoStore } from '@/modules/repo/store/userRepo.store'
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) => {
const { render } = useMarkdown(sha)
@@ -47,6 +47,7 @@ export const useFile = (sha: string, retrieveContent = true) => {
if (!contentFile) {
return null
}
saveCacheNote(contentFile)
return contentFile
}

View File

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

View File

@@ -37,6 +37,22 @@ export const getFiles = async (
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) => {
if (!owner || !repo) {
return null
@@ -47,8 +63,6 @@ export const getMainReadme = async (owner: string, repo: string) => {
`${owner}-${repo}-README`
)
const cachedReadme = await getCachedNote()
try {
const octokit = await getOctokit()
@@ -63,6 +77,8 @@ export const getMainReadme = async (owner: string, repo: string) => {
}
} catch (error) {
console.warn(error)
const cachedReadme = await getCachedNote()
if (cachedReadme) {
return render(cachedReadme.content)
}

View File

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