Files
remanso/src/hooks/useOfflineNotes.hook.ts
Julien Calixte b003a3e008 perf: move PouchDB/IndexedDB operations to a Web Worker
All database reads and writes now run off the main thread via a
dedicated worker, eliminating IndexedDB overhead from the frame budget.

- Create data.worker.ts exposing the Data class via Comlink
- Refactor data.ts to export a Comlink-wrapped proxy and a standalone
  generateId() pure function (workers can't expose sync methods cleanly)
- Update all 10 call sites to import generateId directly instead of
  calling data.generateId()

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-04 11:27:45 +02:00

70 lines
1.7 KiB
TypeScript

import { useAsyncState } from "@vueuse/core"
import { computed, ref } from "vue"
import { data, generateId } from "@/data/data"
import { DataType } from "@/data/DataType.enum"
import { prepareNoteCache } from "@/modules/note/cache/prepareNoteCache"
import { Note } from "@/modules/note/models/Note"
import { queryFileContent } from "@/modules/repo/services/repo"
import { useUserRepoStore } from "@/modules/repo/store/userRepo.store"
export const useOfflineNotes = () => {
const store = useUserRepoStore()
const totalOfNotes = computed(() => store.files.length)
const noteCompleted = ref(0)
const cacheAllNotes = async () => {
const isInitialized = store.user && store.repo && totalOfNotes.value > 0
if (!isInitialized) {
return
}
const cachedNotesFromSha = await data.getAll<DataType.Note, Note>({
prefix: DataType.Note,
keys: store.files.map((file) => file.sha).filter(Boolean) as string[],
includeDocs: false
})
const cachedNotesSet = new Set(cachedNotesFromSha.map((note) => note._id))
noteCompleted.value = 0
for (const file of store.files) {
noteCompleted.value++
if (
!file.sha ||
cachedNotesSet.has(generateId(DataType.Note, file.sha))
) {
continue
}
const { saveCacheNote } = prepareNoteCache(file.sha, file.path)
const contentFile = await queryFileContent(
store.user,
store.repo,
file.sha
)
if (!contentFile) {
return null
}
saveCacheNote(contentFile)
}
}
const { execute, isLoading } = useAsyncState(cacheAllNotes, null, {
immediate: false
})
return {
cacheAllNotes: execute,
isLoading,
totalOfNotes,
noteCompleted
}
}