fix(ci): 质量检查 - 同步格式、前端断言与存储降级
This commit is contained in:
@@ -8,19 +8,26 @@ localforage.config({
|
||||
storeName: "app_state",
|
||||
});
|
||||
|
||||
// localforage 需要 localStorage / indexedDB 等浏览器存储驱动;测试进程等环境可能只有 window 而无存储 API,
|
||||
// 此时直接跳过读写,避免 localforage 内部回退并输出 "No available storage method found"。
|
||||
function browserStorageAvailable(): boolean {
|
||||
if (typeof window === "undefined") return false;
|
||||
return typeof window.localStorage !== "undefined" || typeof window.indexedDB !== "undefined";
|
||||
}
|
||||
|
||||
export function localForageStorageForScope(scope?: string): StateStorage {
|
||||
const keyFor = (name: string) => scopedStorageKey(name, scope);
|
||||
return {
|
||||
getItem: async (name) => {
|
||||
if (typeof window === "undefined") return null;
|
||||
if (!browserStorageAvailable()) return null;
|
||||
return (await localforage.getItem<string>(keyFor(name))) || null;
|
||||
},
|
||||
setItem: async (name, value) => {
|
||||
if (typeof window === "undefined") return;
|
||||
if (!browserStorageAvailable()) return;
|
||||
await localforage.setItem(keyFor(name), value);
|
||||
},
|
||||
removeItem: async (name) => {
|
||||
if (typeof window === "undefined") return;
|
||||
if (!browserStorageAvailable()) return;
|
||||
await localforage.removeItem(keyFor(name));
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user