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>
49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
import { Ref, toValue } from "vue"
|
|
|
|
import { data, generateId } from "@/data/data"
|
|
import { DataType } from "@/data/DataType.enum"
|
|
import { History } from "@/data/models/History"
|
|
|
|
const HISTORY_ID = generateId(DataType.History, "history")
|
|
const MAX_REPO_HISTORY = 10
|
|
|
|
export const useVisitRepo = (newRepo: {
|
|
user: Ref<string> | string
|
|
repo: Ref<string> | string
|
|
}) => {
|
|
const visitRepo = async () => {
|
|
const history = await data.get<DataType.History, History>(HISTORY_ID)
|
|
if (!history) {
|
|
const newHistory: History = {
|
|
_id: HISTORY_ID,
|
|
$type: DataType.History,
|
|
repos: [{ user: toValue(newRepo.user), repo: toValue(newRepo.repo) }]
|
|
}
|
|
await data.add<DataType.History>(newHistory)
|
|
return
|
|
}
|
|
|
|
const clearedRepos = history.repos.filter(
|
|
(repo) =>
|
|
repo.user !== toValue(newRepo.user) &&
|
|
repo.repo !== toValue(newRepo.repo)
|
|
)
|
|
|
|
const historyRepos = [
|
|
{ user: toValue(newRepo.user), repo: toValue(newRepo.repo) },
|
|
...clearedRepos
|
|
].slice(0, MAX_REPO_HISTORY - 1)
|
|
|
|
const newHistory: History = {
|
|
...history,
|
|
repos: historyRepos
|
|
}
|
|
|
|
await data.update(newHistory)
|
|
}
|
|
|
|
return {
|
|
visitRepo
|
|
}
|
|
}
|