Skip to content

Commit

Permalink
fix: add db index
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Aug 9, 2024
1 parent bad5869 commit a888a78
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/renderer/src/store/image/db.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
import { createStore, get, set } from "idb-keyval"
import type { UseStore } from "idb-keyval"
import { get, promisifyRequest, set } from "idb-keyval"

import type { StoreImageType } from "."

const db = createStore("FOLLOW_IMAGE_DIMENSIONS", "image-dimensions")
function createStore(dbName: string, storeName: string): UseStore {
const request = indexedDB.open(dbName)
request.onupgradeneeded = () => {
const objectStore = request.result.createObjectStore(storeName)

objectStore.createIndex("src", "src", { unique: true })
return objectStore
}
const dbp = promisifyRequest(request)

export const getImageDimensionsFromDb = async (url: string) => await get(url, db)
return (txMode, callback) =>
dbp.then((db) =>
callback(db.transaction(storeName, txMode).objectStore(storeName)),
)
}

const db = createStore("FOLLOW_IMAGE_DIMENSIONS", "image-dimensions")
export const getImageDimensionsFromDb = async (url: string) =>
await get(url, db)

export const saveImageDimensionsToDb = async (
url: string,
Expand Down

0 comments on commit a888a78

Please sign in to comment.