Files
image-workbench-desktop/src/storage-fallback-indexeddb.js
T

66 lines
5.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* IndexedDB 兜底实现(自 app.js 收编,P2-10「伪模块收编」)
*
* 生产路径:app.js 的 IDB 直接用 window.IWBStorageRuntime.indexedDb(src/storage-runtime.js 的实例),
* 本模块只在「已加载 src 但运行时缺 indexedDb」的兜底分支被调用,以及单文件模式下的显式替代。
*
* 与 src/storage-runtime.js 的 createIndexedDb **有意差异**(由单测固化,勿盲目合并):
* - put/get/del 在事务失败时会「重置连接并重试一次」(retried 标志);
* - 返回原始值(布尔 / 记录 / 数组),而非 storage-indexeddb.js 的 Result 信封。
* 依赖仅 `indexedDB` 浏览器全局;契约经 create({ contract }) 注入,不引用 app.js 的任何全局。
*/
;(function (global) {
'use strict'
const create = (options) => {
const contract = (options && options.contract) || { name: 'iwb_web', version: 3, stores: ['images', 'logs', 'kv'] }
let db = null
const resetDb = () => { if (db) { try { db.close() } catch (e) { /* ignore */ } db = null } }
const open = () => new Promise((resolve) => {
if (db) return resolve(db)
if (typeof indexedDB === 'undefined') return resolve(null)
try {
const req = indexedDB.open(contract.name, contract.version)
req.onupgradeneeded = () => {
const d = req.result
for (const store of contract.stores) if (!d.objectStoreNames.contains(store)) d.createObjectStore(store)
}
req.onsuccess = () => { db = req.result; db.onversionchange = () => { try { try { db.close() } catch (e) { /* ignore */ } } catch (e2) { /* ignore */ } db = null }; resolve(db) }
req.onerror = () => resolve(null)
req.onblocked = () => resolve(null)
} catch (e) { resolve(null) }
})
// put 写入:失败时重置连接并重试一次(处理会话内连接进入坏状态、刷新才恢复的浏览器差异)
const put = (key, value, retried) => open().then((d) => d ? new Promise((res) => { const t = d.transaction('images', 'readwrite'); t.objectStore('images').put(value, key); t.oncomplete = () => res(true); t.onerror = () => { if (!retried) put(key, value, true).then(res); else res(false) } }) : Promise.resolve(false))
const get = (key, retried) => open().then((d) => d ? new Promise((res) => { const t = d.transaction('images', 'readonly'); const r = t.objectStore('images').get(key); r.onsuccess = () => res(r.result); r.onerror = () => { if (!retried) get(key, true).then(res); else res(null) } }) : Promise.resolve(null))
const del = (key, retried) => open().then((d) => d ? new Promise((res) => { const t = d.transaction('images', 'readwrite'); t.objectStore('images').delete(key); t.oncomplete = () => res(true); t.onerror = () => { if (!retried) del(key, true).then(res); else res(false) } }) : Promise.resolve(true))
const tx = (store, mode) => open().then((d) => d ? new Promise((res) => { const t = d.transaction(store, mode); t.oncomplete = () => res(true); t.onerror = () => res(false); t.onabort = () => res(false); /** @type {any} */ (res)._t = t }) : Promise.resolve(null))
return {
put(key, value) { return put(key, value, false) },
get(key) { return get(key, false) },
del(key) { return del(key, false) },
keys() { return open().then((d) => d ? new Promise((res) => { const t = d.transaction('images', 'readonly'); const r = t.objectStore('images').getAllKeys(); r.onsuccess = () => res(r.result || []); r.onerror = () => res([]) }) : Promise.resolve([])) },
// 日志(logs store,自动保留 7 天)
logPut(entry) { return open().then((d) => d ? new Promise((res) => { const t = d.transaction('logs', 'readwrite'); t.objectStore('logs').put(entry, entry.id); t.oncomplete = () => res(true); t.onerror = () => res(false) }) : Promise.resolve(false)) },
logAll() { return open().then((d) => d ? new Promise((res) => { const t = d.transaction('logs', 'readonly'); const r = t.objectStore('logs').getAll(); r.onsuccess = () => res(r.result || []); r.onerror = () => res([]) }) : Promise.resolve([])) },
logClear() { return open().then((d) => d ? new Promise((res) => { const t = d.transaction('logs', 'readwrite'); t.objectStore('logs').clear(); t.oncomplete = () => res(true); t.onerror = () => res(false) }) : Promise.resolve(true)) },
// 遍历 images store(备份导出用,游标分批)
imagesAll(batch = 40) { return open().then((d) => {
if (!d) return Promise.resolve([])
return new Promise((res) => {
const out = []
const t = d.transaction('images', 'readonly')
const store = t.objectStore('images')
const r = store.openCursor()
r.onsuccess = () => { const c = r.result; if (c) { out.push({ key: c.key, value: c.value }); c.continue() } else res(out) }
r.onerror = () => res(out)
})
}) },
// 通用键值(kv store):节点组等可能超 localStorage 容量的数据放这里(容量大,整表写入不丢)
kvPut(key, value) { return open().then((d) => d ? new Promise((res) => { const t = d.transaction('kv', 'readwrite'); t.objectStore('kv').put(value, key); t.oncomplete = () => res(true); t.onerror = () => res(false) }) : Promise.resolve(false)) },
kvGet(key) { return open().then((d) => d ? new Promise((res) => { const t = d.transaction('kv', 'readonly'); const r = t.objectStore('kv').get(key); r.onsuccess = () => res(r.result); r.onerror = () => res(null) }) : Promise.resolve(null)) },
}
}
global.IWBStorageFallbackIndexeddb = Object.freeze({ create })
})(typeof window !== 'undefined' ? window : globalThis)