refactor: migrate state imports from contexts to stores directory
This commit is contained in:
+15
-3
@@ -11,15 +11,27 @@
|
||||
*/
|
||||
import { createStorage, type Storage } from "unstorage";
|
||||
import localStorageDriver from "unstorage/drivers/localstorage";
|
||||
import memoryDriver from "unstorage/drivers/memory";
|
||||
import type { ZodType } from "zod";
|
||||
|
||||
import { Result, type Result as ResultT } from "@/utils/result";
|
||||
|
||||
export class SpAsyncUtil {
|
||||
// ===== 私有静态状态 =====
|
||||
private static _storage: Storage = createStorage({
|
||||
driver: localStorageDriver({ base: "cozsweet:" }),
|
||||
});
|
||||
// 环境判断 + lazy 初始化:SSR 用 memory driver,浏览器用 localStorage driver。
|
||||
// unstorage 1.10+ 要求显式传入 localStorage,故这里同步注入 window.localStorage。
|
||||
private static _storage: Storage = (() => {
|
||||
if (typeof window === "undefined") {
|
||||
// SSR 环境:无 window,降级为内存驱动
|
||||
return createStorage({ driver: memoryDriver() });
|
||||
}
|
||||
// 浏览器环境:使用 localStorage 驱动
|
||||
return createStorage({
|
||||
driver: localStorageDriver({
|
||||
base: "cozsweet:",
|
||||
}),
|
||||
});
|
||||
})();
|
||||
|
||||
// 私有构造函数:禁止实例化(对齐 Dart `SpAsyncUtil._()`)
|
||||
private constructor() {}
|
||||
|
||||
Reference in New Issue
Block a user