主题数据
This commit is contained in:
@@ -8,6 +8,7 @@ import { PlayerData } from "./PlayerData";
|
||||
import { AccountData } from "./AccountData";
|
||||
import { MetaData } from "./MetaData";
|
||||
import { NetTimeData } from "./NetTimeData";
|
||||
import { ThemeData } from "./ThemeData";
|
||||
|
||||
/**
|
||||
* 集中定义数据ID,避免写错字符串
|
||||
@@ -18,6 +19,7 @@ export enum DataId {
|
||||
Account = "account",
|
||||
Meta = "meta",
|
||||
NetTime = "netTime",
|
||||
Theme = "theme",
|
||||
}
|
||||
|
||||
/** 构造器类型 */
|
||||
@@ -43,13 +45,12 @@ export class DataManager {
|
||||
*/
|
||||
public init(): void {
|
||||
console.log("[DataManager] 数据管理器初始化");
|
||||
|
||||
this.register(DataId.Login, LoginData);
|
||||
this.register(DataId.Player, PlayerData);
|
||||
this.register(DataId.Account, AccountData);
|
||||
this.register(DataId.Meta, MetaData);
|
||||
this.register(DataId.NetTime, NetTimeData);
|
||||
|
||||
this.register(DataId.Theme, ThemeData);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,18 +67,18 @@ export class DataManager {
|
||||
* 获取数据(已存在则直接返回;否则按配置自动实例化并缓存)
|
||||
*/
|
||||
public getDataById<T extends BaseData = BaseData>(dataId: string): T | null {
|
||||
// 1) 先从缓存取
|
||||
// 先从缓存取
|
||||
const cached = this._dataMap.get(dataId) as T | undefined;
|
||||
if (cached) return cached;
|
||||
|
||||
// 2) 缓存没有→ 查找构造器
|
||||
// 缓存没有→ 查找构造器
|
||||
const Ctor = this._config.get(dataId) as DataCtor<T> | undefined;
|
||||
if (!Ctor) {
|
||||
console.warn(`[DataManager] 未找到 dataId 的构造器配置: ${dataId}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
// 3) 动态创建 & init & 缓存
|
||||
// 动态创建 & init & 缓存
|
||||
const instance = new Ctor();
|
||||
// 若你的 BaseData.init 接受 dataId,可传入
|
||||
instance.init?.(dataId);
|
||||
|
||||
Reference in New Issue
Block a user