feat: implement lazy singleton pattern for repository instances and update related imports
This commit is contained in:
@@ -8,18 +8,14 @@
|
||||
*
|
||||
* 设计:
|
||||
* - 依赖 `user-machine.helpers.ts`(toView, readInitData, userStorage)
|
||||
* - 仓库以接口类型注入(调用面只看接口,运行时仍是同一单例)
|
||||
* - 仓库使用懒单例 getter,actor 执行时才创建实例
|
||||
* - 命名约定:`[user-machine]` 前缀日志(如未来加)—— 与 chat 模块对齐
|
||||
*/
|
||||
|
||||
import { fromPromise } from "xstate";
|
||||
|
||||
import { userRepository } from "@/data/repositories/user_repository";
|
||||
import { authRepository } from "@/data/repositories/auth_repository";
|
||||
import type {
|
||||
IAuthRepository,
|
||||
IUserRepository,
|
||||
} from "@/data/repositories/interfaces";
|
||||
import { getUserRepository } from "@/data/repositories/user_repository";
|
||||
import { getAuthRepository } from "@/data/repositories/auth_repository";
|
||||
import { Result } from "@/utils";
|
||||
|
||||
import {
|
||||
@@ -32,13 +28,6 @@ import {
|
||||
userStorage,
|
||||
} from "./user-machine.helpers";
|
||||
|
||||
// ============================================================
|
||||
// 仓库注入
|
||||
// ============================================================
|
||||
// 仓库以接口类型注入:调用面只看接口,运行时仍是同一单例
|
||||
const userRepo: IUserRepository = userRepository;
|
||||
const authRepo: IAuthRepository = authRepository;
|
||||
|
||||
// ============================================================
|
||||
// Init actor:从 UserStorage 读 user + avatarUrl
|
||||
// ============================================================
|
||||
@@ -59,6 +48,7 @@ export const userInitActor = fromPromise<InitData>(async () => readInitData());
|
||||
* - 任一失败都不阻断另一方,machine 层负责保留已有 context
|
||||
*/
|
||||
export const userFetchActor = fromPromise<UserFetchData>(async () => {
|
||||
const userRepo = getUserRepository();
|
||||
const [userResult, entitlementsResult] = await Promise.all([
|
||||
userRepo.getCurrentUser(),
|
||||
userRepo.getEntitlements(),
|
||||
@@ -97,5 +87,5 @@ export const userFetchActor = fromPromise<UserFetchData>(async () => {
|
||||
* - onDone / onError 都跑 `clearUser` action(machine 层)—— 失败也清本地
|
||||
*/
|
||||
export const userLogoutActor = fromPromise<void>(async () => {
|
||||
await authRepo.logout();
|
||||
await getAuthRepository().logout();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user