chore(deps): add unstorage and migrate ChatStorage to use SpAsyncUtil
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
"use client";
|
||||
|
||||
/**
|
||||
* AppStorage 完整实现(静态类风格 / localStorage)
|
||||
* AppStorage 完整实现(基于 unstorage 抽象)
|
||||
*
|
||||
* 对齐 Dart 端 `AppStorage`(lib/data/services/storage/app_storage.dart):
|
||||
* - PWA 对话框每日展示 / PWA 事件每日上报 / app-info 每日上报
|
||||
* - 内部用 `yyyy-MM-dd` 字符串比对实现"每日一次"语义
|
||||
*
|
||||
* 注意:Dart 端是 `static class` 没有接口,本 TS 版本同样保持静态方法风格。
|
||||
* 通过 `LocalStorage` 单例 + 懒加载的私有 getter 访问底层存储。
|
||||
* 通过 SpAsyncUtil 静态类访问底层存储(浏览器 localStorage / SSR memory 自动切换)。
|
||||
*
|
||||
* 语义:
|
||||
* - `canXxx(today)` 返回 `true` 当且仅当"自上次记录以来日期变了"
|
||||
@@ -17,23 +17,10 @@
|
||||
*/
|
||||
|
||||
import { Result, type Result as ResultT } from "@/utils/result";
|
||||
import { SpAsyncUtil } from "@/utils/storage";
|
||||
import { StorageKeys } from "../storage_keys";
|
||||
import { LocalStorage } from "@/utils/local-storage";
|
||||
|
||||
export class AppStorage {
|
||||
private static _ls: LocalStorage | null = null;
|
||||
|
||||
/** 懒加载 LocalStorage 单例,避免模块加载期触发 SSR 副作用。 */
|
||||
private static get ls(): LocalStorage {
|
||||
if (!AppStorage._ls) AppStorage._ls = LocalStorage.getInstance();
|
||||
return AppStorage._ls;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
static _resetForTests(): void {
|
||||
AppStorage._ls = null;
|
||||
}
|
||||
|
||||
// ---- PWA install dialog ----
|
||||
|
||||
static async canShowPwaDialog(todayString: string): Promise<ResultT<boolean>> {
|
||||
@@ -41,7 +28,7 @@ export class AppStorage {
|
||||
}
|
||||
|
||||
static recordPwaDialogShown(todayString: string): Promise<ResultT<void>> {
|
||||
return AppStorage.ls.setString(StorageKeys.lastPwaDialogShown, todayString);
|
||||
return SpAsyncUtil.setString(StorageKeys.lastPwaDialogShown, todayString);
|
||||
}
|
||||
|
||||
// ---- PWA event reporting ----
|
||||
@@ -51,7 +38,7 @@ export class AppStorage {
|
||||
}
|
||||
|
||||
static recordPwaEventReported(todayString: string): Promise<ResultT<void>> {
|
||||
return AppStorage.ls.setString(StorageKeys.lastPwaEventReported, todayString);
|
||||
return SpAsyncUtil.setString(StorageKeys.lastPwaEventReported, todayString);
|
||||
}
|
||||
|
||||
// ---- app info reporting ----
|
||||
@@ -61,7 +48,7 @@ export class AppStorage {
|
||||
}
|
||||
|
||||
static recordAppInfoReported(todayString: string): Promise<ResultT<void>> {
|
||||
return AppStorage.ls.setString(StorageKeys.lastAppInfoReported, todayString);
|
||||
return SpAsyncUtil.setString(StorageKeys.lastAppInfoReported, todayString);
|
||||
}
|
||||
|
||||
// ---- internal helpers ----
|
||||
@@ -77,7 +64,7 @@ export class AppStorage {
|
||||
key: string,
|
||||
todayString: string,
|
||||
): Promise<ResultT<boolean>> {
|
||||
const r = await AppStorage.ls.getString(key);
|
||||
const r = await SpAsyncUtil.getString(key);
|
||||
if (!r.success) return r;
|
||||
return Result.ok(r.data === null || r.data !== todayString);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user