- Cache queries and songs across mobile and web - Show cached content and skeletons during loading - Move shared playback and cache utilities into Convex client
117 lines
2.9 KiB
TypeScript
117 lines
2.9 KiB
TypeScript
import {
|
|
configurePersistentCache,
|
|
type PersistentCacheBackend,
|
|
type PersistentCacheEntry,
|
|
} from "@otunes/convex/client/persistent-cache"
|
|
|
|
export * from "@otunes/convex/client/persistent-cache"
|
|
|
|
const DB_NAME = "otunes-cache"
|
|
const DB_VERSION = 1
|
|
const STORE_NAME = "entries"
|
|
|
|
let dbPromise: Promise<IDBDatabase | null> | null = null
|
|
|
|
function requestToPromise<T>(request: IDBRequest<T>) {
|
|
return new Promise<T>((resolve, reject) => {
|
|
request.onsuccess = () => resolve(request.result)
|
|
request.onerror = () => reject(request.error)
|
|
})
|
|
}
|
|
|
|
function transactionDone(transaction: IDBTransaction) {
|
|
return new Promise<void>((resolve, reject) => {
|
|
transaction.oncomplete = () => resolve()
|
|
transaction.onabort = () => reject(transaction.error)
|
|
transaction.onerror = () => reject(transaction.error)
|
|
})
|
|
}
|
|
|
|
function openDb() {
|
|
if (dbPromise) {
|
|
return dbPromise
|
|
}
|
|
|
|
dbPromise = new Promise<IDBDatabase | null>((resolve) => {
|
|
if (typeof indexedDB === "undefined") {
|
|
resolve(null)
|
|
return
|
|
}
|
|
|
|
try {
|
|
const request = indexedDB.open(DB_NAME, DB_VERSION)
|
|
request.onupgradeneeded = () => {
|
|
if (!request.result.objectStoreNames.contains(STORE_NAME)) {
|
|
request.result.createObjectStore(STORE_NAME)
|
|
}
|
|
}
|
|
request.onsuccess = () => resolve(request.result)
|
|
request.onerror = () => resolve(null)
|
|
request.onblocked = () => resolve(null)
|
|
} catch {
|
|
resolve(null)
|
|
}
|
|
})
|
|
|
|
return dbPromise
|
|
}
|
|
|
|
const indexedDbBackend: PersistentCacheBackend = {
|
|
async loadAll() {
|
|
const db = await openDb()
|
|
if (!db) {
|
|
return []
|
|
}
|
|
|
|
const transaction = db.transaction(STORE_NAME, "readonly")
|
|
const store = transaction.objectStore(STORE_NAME)
|
|
const [keys, values] = await Promise.all([
|
|
requestToPromise(store.getAllKeys()),
|
|
requestToPromise(store.getAll() as IDBRequest<PersistentCacheEntry[]>),
|
|
])
|
|
const entries: Array<[string, PersistentCacheEntry]> = []
|
|
keys.forEach((key, index) => {
|
|
const entry = values[index]
|
|
if (typeof key === "string" && entry) {
|
|
entries.push([key, entry])
|
|
}
|
|
})
|
|
return entries
|
|
},
|
|
|
|
async write(writes) {
|
|
const db = await openDb()
|
|
if (!db) {
|
|
return
|
|
}
|
|
|
|
const transaction = db.transaction(STORE_NAME, "readwrite")
|
|
const store = transaction.objectStore(STORE_NAME)
|
|
for (const [key, entry] of writes) {
|
|
if (entry === null) {
|
|
store.delete(key)
|
|
} else {
|
|
store.put(entry, key)
|
|
}
|
|
}
|
|
await transactionDone(transaction)
|
|
},
|
|
|
|
async clear() {
|
|
const db = await openDb()
|
|
if (!db) {
|
|
return
|
|
}
|
|
|
|
const transaction = db.transaction(STORE_NAME, "readwrite")
|
|
transaction.objectStore(STORE_NAME).clear()
|
|
await transactionDone(transaction)
|
|
},
|
|
}
|
|
|
|
if (typeof window !== "undefined") {
|
|
configurePersistentCache(indexedDbBackend, {
|
|
maxEntriesByPrefix: { q: 400, song: 5000 },
|
|
})
|
|
}
|