add a method to cache all files when necessary

This commit is contained in:
Julien Calixte
2024-09-01 00:25:11 +02:00
parent b4a7571b8a
commit df25634b98
3 changed files with 43 additions and 5 deletions

View File

@@ -2,7 +2,7 @@ import { computed, Ref, ref, toValue } from 'vue'
import { useMarkdown } from '@/hooks/useMarkdown.hook' import { useMarkdown } from '@/hooks/useMarkdown.hook'
import { prepareNoteCache } from '@/modules/note/cache/prepareNoteCache' import { prepareNoteCache } from '@/modules/note/cache/prepareNoteCache'
import { getFileContent } from '@/modules/repo/services/repo' import { queryFileContent } from '@/modules/repo/services/repo'
import { useUserRepoStore } from '@/modules/repo/store/userRepo.store' import { useUserRepoStore } from '@/modules/repo/store/userRepo.store'
export const useFile = (sha: Ref<string> | string, retrieveContent = true) => { export const useFile = (sha: Ref<string> | string, retrieveContent = true) => {
@@ -48,7 +48,7 @@ export const useFile = (sha: Ref<string> | string, retrieveContent = true) => {
return cachedNote.content return cachedNote.content
} }
const contentFile = await getFileContent( const contentFile = await queryFileContent(
store.user, store.user,
store.repo, store.repo,
toValue(sha) toValue(sha)

View File

@@ -98,7 +98,7 @@ export const getUserSettingsContent = async (
return null return null
} }
const content = await getFileContent(user, repo, configFile.sha) const content = await queryFileContent(user, repo, configFile.sha)
if (!content) { if (!content) {
return null return null
@@ -107,7 +107,7 @@ export const getUserSettingsContent = async (
return JSON.parse(atob(content)) as UserSettings return JSON.parse(atob(content)) as UserSettings
} }
export const getFileContent = async ( export const queryFileContent = async (
user: string, user: string,
repo: string, repo: string,
sha: string sha: string

View File

@@ -2,6 +2,7 @@ import { defineStore } from 'pinia'
import { data } from '@/data/data' import { data } from '@/data/data'
import { DataType } from '@/data/DataType.enum' import { DataType } from '@/data/DataType.enum'
import { prepareNoteCache } from '@/modules/note/cache/prepareNoteCache'
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 { SavedRepo } from '@/modules/repo/models/SavedRepo' import { SavedRepo } from '@/modules/repo/models/SavedRepo'
@@ -9,7 +10,8 @@ import {
getCachedMainReadme, getCachedMainReadme,
getFiles, getFiles,
getMainReadme, getMainReadme,
getUserSettingsContent getUserSettingsContent,
queryFileContent
} from '@/modules/repo/services/repo' } from '@/modules/repo/services/repo'
import { refreshToken } from '@/modules/user/service/signIn' import { refreshToken } from '@/modules/user/service/signIn'
@@ -114,6 +116,42 @@ export const useUserRepoStore = defineStore({
this.files = [] this.files = []
this.readme = null this.readme = null
this.userSettings = undefined this.userSettings = undefined
},
async cacheAllFiles() {
const isInitialized = this.user && this.repo && this.files.length > 0
if (!isInitialized) {
return
}
for (const file of this.files) {
if (!file.sha) {
continue
}
const { getCachedNote, saveCacheNote } = prepareNoteCache(
file.sha,
file.path
)
const isNoteCached = (await getCachedNote()) !== null
if (isNoteCached) {
continue
}
const contentFile = await queryFileContent(
this.user,
this.repo,
file.sha
)
if (!contentFile) {
return null
}
saveCacheNote(contentFile)
}
} }
} }
}) })