()
| 9 | let chatCacheDbPromise = null; |
| 10 | |
| 11 | export function openChatCacheDb() { |
| 12 | if (chatCacheDbPromise) return chatCacheDbPromise; |
| 13 | chatCacheDbPromise = new Promise((resolve, reject) => { |
| 14 | if (typeof indexedDB === 'undefined') { |
| 15 | reject(new Error('IndexedDB unavailable')); |
| 16 | return; |
| 17 | } |
| 18 | const req = indexedDB.open(CHAT_CACHE_DB, 1); |
| 19 | req.onerror = () => reject(req.error || new Error('Failed to open chat cache')); |
| 20 | req.onupgradeneeded = () => { |
| 21 | const db = req.result; |
| 22 | if (!db.objectStoreNames.contains(CHAT_CACHE_STORE)) { |
| 23 | const store = db.createObjectStore(CHAT_CACHE_STORE, { keyPath: 'cacheKey' }); |
| 24 | store.createIndex('updatedAt', 'updatedAt', { unique: false }); |
| 25 | } |
| 26 | }; |
| 27 | req.onsuccess = () => resolve(req.result); |
| 28 | }).catch(err => { |
| 29 | console.warn('[chat-cache]', err); |
| 30 | chatCacheDbPromise = null; |
| 31 | throw err; |
| 32 | }); |
| 33 | return chatCacheDbPromise; |
| 34 | } |
| 35 | |
| 36 | export async function chatCacheRead(cacheKey) { |
| 37 | const db = await openChatCacheDb(); |
no outgoing calls
no test coverage detected