fix(storage): getStore() 统一到资料目录底座,并一次性迁移旧 localStorage 的 iwb* 键
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 51s

同上一条视口 bug 是同一类:桌面端有两套存储入口——lsGet/lsSet 走资料目录(IWBStorageFile.runtime),
而 IWBStorageLocal.getStore() 固定返回 localStorage 门面。用后者的模块(插件数据、插件安装记录、
停靠栏布局、设置、崩溃日志、自动备份)实际写在 WebView localStorage 里:重启不丢但不进资料目录,
多机共享时不同步,清 WebView 数据就全没。

改动(src/storage-local.js):
- getStore():有文件底座就用文件底座(friendly({storage: IWBStorageFile.storage()})),否则退回 localStorage
- 首次绑定文件底座时做一次性迁移:把 localStorage 里 iwb* 开头的键搬到文件侧
  (只搬文件侧还没有的键,不覆盖已有数据;写 iwb_local_migrated_v1 标记只跑一次)
- 两套实例分别缓存(sharedStore / sharedFileStore),避免早期调用把 localStorage 实例固化
This commit is contained in:
qiqi committed 2026-11-28 01:12:31 +08:00
1 parent 4610779c6e
commit 412f329d97
2 files changed
+37 -2

No files matched your search

+36 -1
View File
@@ -64,7 +64,42 @@
}
// 共享实例(懒创建一次):app.js 与各 src 模块都走它,配额/异常处理集中在此
let sharedStore = null
const getStore = () => { if (!sharedStore) sharedStore = friendly({}); return sharedStore }
let sharedFileStore = null // 桌面端:绑到资料目录底座的共享实例(与 lsGet/lsSet 同源)
// v3.26.109:getStore() 必须和 lsGet/lsSet 用同一个底座。
// 桌面端 lsGet 走的是资料目录(IWBStorageFile.runtime),而这里原来固定返回 localStorage 门面 ——
// 于是「视口」「插件安装状态」「停靠栏布局」等写进 WebView localStorage、读却读资料目录(或反之),
// 表现为重启后设置丢失、多机不同步。这里改成:有文件底座就用文件底座,并把旧 localStorage 里的
// iwb* 键一次性搬过去(只在文件侧没有该键时搬,不覆盖已有数据)。
const fileStorageOf = () => {
const fs = global.IWBStorageFile
if (!fs || typeof fs.enabled !== 'function' || !fs.enabled() || typeof fs.storage !== 'function') return null
try { return fs.storage() || null } catch (e) { return null }
}
const migrateLocalOnce = (dst) => {
try {
if (!dst || typeof dst.setItem !== 'function') return
if (dst.getItem('iwb_local_migrated_v1') !== null) return
const src = global.localStorage
if (src && src.length) {
for (let i = 0; i < src.length; i++) {
const k = src.key(i)
if (!k || k.indexOf('iwb') !== 0) continue
if (dst.getItem(k) !== null) continue
try { dst.setItem(k, src.getItem(k)) } catch (e) { /* 单条失败继续 */ }
}
}
try { dst.setItem('iwb_local_migrated_v1', '1') } catch (e) { /* ignore */ }
} catch (e) { /* ignore */ }
}
const getStore = () => {
const fsStore = fileStorageOf()
if (fsStore) {
if (!sharedFileStore) { sharedFileStore = friendly({ storage: fsStore }); migrateLocalOnce(fsStore) }
return sharedFileStore
}
if (!sharedStore) sharedStore = friendly({})
return sharedStore
}
global.IWBStorageLocal = { create, friendly, getStore }
})(typeof window !== 'undefined' ? window : globalThis)