dzkcc-mflow 0.0.34 → 0.0.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,48 @@
1
+ /**
2
+ * ModelNames 接口(由业务层扩展以提供代码补全和类型推断)
3
+ * @example
4
+ * ```typescript
5
+ * // 在 .d.ts 文件中扩展
6
+ * interface ModelNamesType {
7
+ * readonly User: 'User';
8
+ * }
9
+ * ```
10
+ */
11
+ export interface ModelNamesType extends Record<string, string> {
12
+ }
13
+ /**
14
+ * ManagerNames 接口(由业务层扩展以提供代码补全和类型推断)
15
+ * @example
16
+ * ```typescript
17
+ * // 在 .d.ts 文件中扩展
18
+ * interface ManagerNamesType {
19
+ * readonly Home: 'Home';
20
+ * }
21
+ * ```
22
+ */
23
+ export interface ManagerNamesType extends Record<string, string> {
24
+ }
25
+ /**
26
+ * ViewNames 接口(由业务层扩展以提供代码补全和类型推断)
27
+ */
28
+ export interface ViewNamesType extends Record<string, string> {
29
+ }
1
30
  /**
2
31
  * 核心接口 - 管理 Model 和 Manager 的生命周期
3
32
  *
4
33
  * 类型推断由业务层的 .d.ts 文件通过函数重载提供
5
34
  */
6
35
  export interface ICore {
7
- /** 注册 Model - 通过 Key 自动实例化 */
8
- regModel(modelKey: string): void;
36
+ /**
37
+ * 注册 Model - 通过 Key 自动实例化
38
+ * @param modelKey Model 的 Key,使用 ModelNames.XXX
39
+ * @example
40
+ * ```typescript
41
+ * // 手动注册(通常不需要,推荐使用 autoRegister)
42
+ * core.regModel(ModelNames.User);
43
+ * ```
44
+ */
45
+ regModel(modelKey: keyof ModelNamesType): void;
9
46
  /**
10
47
  * 获取 Model 实例
11
48
  * @param modelKey Model 的 Key,使用 ModelNames.XXX
@@ -16,9 +53,17 @@ export interface ICore {
16
53
  * const userModel = core.getModel(ModelNames.User);
17
54
  * ```
18
55
  */
19
- getModel(modelKey: string): any;
20
- /** 注册 Manager - 通过 Key 自动实例化 */
21
- regManager(managerKey: string): void;
56
+ getModel(modelKey: keyof ModelNamesType): any;
57
+ /**
58
+ * 注册 Manager - 通过 Key 自动实例化
59
+ * @param managerKey Manager 的 Key,使用 ManagerNames.XXX
60
+ * @example
61
+ * ```typescript
62
+ * // 手动注册(通常不需要,推荐使用 autoRegister)
63
+ * core.regManager(ManagerNames.Game);
64
+ * ```
65
+ */
66
+ regManager(managerKey: keyof ManagerNamesType): void;
22
67
  /**
23
68
  * 获取 Manager 实例
24
69
  * @param managerKey Manager 的 Key,使用 ManagerNames.XXX
@@ -29,7 +74,19 @@ export interface ICore {
29
74
  * const gameManager = core.getManager(ManagerNames.Game);
30
75
  * ```
31
76
  */
32
- getManager(managerKey: string): any;
77
+ getManager(managerKey: keyof ManagerNamesType): any;
78
+ /**
79
+ * 检查 Model 是否已注册
80
+ * @param modelKey Model 的 Key
81
+ * @returns 是否已注册
82
+ */
83
+ hasModel(modelKey: keyof ModelNamesType): boolean;
84
+ /**
85
+ * 检查 Manager 是否已注册
86
+ * @param managerKey Manager 的 Key
87
+ * @returns 是否已注册
88
+ */
89
+ hasManager(managerKey: keyof ManagerNamesType): boolean;
33
90
  }
34
91
  /**
35
92
  * Model 基接口 - 数据模型
@@ -67,11 +124,11 @@ export interface IView {
67
124
  */
68
125
  export interface IUIManager {
69
126
  /** 打开视图 */
70
- open(viewKey: string, args?: any): Promise<IView>;
127
+ open(viewKey: keyof ViewNamesType, args?: any): Promise<IView>;
71
128
  /** 关闭视图 */
72
- close(viewKey: string | IView, destory?: boolean): void;
129
+ close(viewKey: keyof ViewNamesType | IView, destory?: boolean): void;
73
130
  /** 打开视图并入栈 */
74
- openAndPush(viewKey: string, group: string, args?: any): Promise<IView>;
131
+ openAndPush(viewKey: keyof ViewNamesType, group: string, args?: any): Promise<IView>;
75
132
  /** 关闭栈顶视图并弹出 */
76
133
  closeAndPop(group: string, destroy?: boolean): void;
77
134
  /** 获取栈顶视图 */
@@ -1,9 +1,9 @@
1
- import { ICore, IManager } from "./Api";
1
+ import { ICore, IManager, ModelNamesType, ManagerNamesType } from "./Api";
2
2
  export declare abstract class AbstractCore<T extends AbstractCore<T>> implements ICore {
3
3
  private readonly container;
4
4
  constructor();
5
5
  protected abstract initialize(): void;
6
- regModel(modelKey: string): void;
6
+ regModel(modelKey: keyof ModelNamesType): void;
7
7
  /**
8
8
  * 获取 Model 实例
9
9
  * @param modelKey Model 的 Key,使用 ModelNames.XXX
@@ -14,8 +14,8 @@ export declare abstract class AbstractCore<T extends AbstractCore<T>> implements
14
14
  * const userModel = core.getModel(ModelNames.User);
15
15
  * ```
16
16
  */
17
- getModel(modelKey: string): any;
18
- regManager(managerKey: string): void;
17
+ getModel(modelKey: keyof ModelNamesType): any;
18
+ regManager(managerKey: keyof ManagerNamesType): void;
19
19
  /**
20
20
  * 获取 Manager 实例
21
21
  * @param managerKey Manager 的 Key,使用 ManagerNames.XXX
@@ -26,7 +26,19 @@ export declare abstract class AbstractCore<T extends AbstractCore<T>> implements
26
26
  * const gameManager = core.getManager(ManagerNames.Game);
27
27
  * ```
28
28
  */
29
- getManager(managerKey: string): any;
29
+ getManager(managerKey: keyof ManagerNamesType): any;
30
+ /**
31
+ * 检查 Model 是否已注册
32
+ * @param modelKey Model 的 Key
33
+ * @returns 是否已注册
34
+ */
35
+ hasModel(modelKey: keyof ModelNamesType): boolean;
36
+ /**
37
+ * 检查 Manager 是否已注册
38
+ * @param managerKey Manager 的 Key
39
+ * @returns 是否已注册
40
+ */
41
+ hasManager(managerKey: keyof ManagerNamesType): boolean;
30
42
  }
31
43
  export declare abstract class AbstractManager implements IManager {
32
44
  abstract initialize(): void;
@@ -36,11 +48,11 @@ export declare abstract class AbstractManager implements IManager {
36
48
  * @param modelKey Model 的 Key,使用 ModelNames.XXX
37
49
  * @returns Model 实例(具体类型由 .d.ts 文件的函数重载推断)
38
50
  */
39
- protected getModel(modelKey: string): any;
51
+ protected getModel(modelKey: keyof ModelNamesType): any;
40
52
  /**
41
53
  * 获取 Manager 实例
42
54
  * @param managerKey Manager 的 Key,使用 ManagerNames.XXX
43
55
  * @returns Manager 实例(具体类型由 .d.ts 文件的函数重载推断)
44
56
  */
45
- protected getManager(managerKey: string): any;
57
+ protected getManager(managerKey: keyof ManagerNamesType): any;
46
58
  }
package/dist/core/Core.js CHANGED
@@ -14,6 +14,9 @@ class Container {
14
14
  throw new Error(`${key} not registered!`);
15
15
  return ins;
16
16
  }
17
+ has(key) {
18
+ return this.key2ins.has(key);
19
+ }
17
20
  }
18
21
  class AbstractCore {
19
22
  constructor() {
@@ -22,9 +25,10 @@ class AbstractCore {
22
25
  }
23
26
  // 注册与获取模型
24
27
  regModel(modelKey) {
25
- const ModelClass = getModelClass(modelKey);
28
+ const modelKeyStr = modelKey;
29
+ const ModelClass = getModelClass(modelKeyStr);
26
30
  const model = new ModelClass();
27
- this.container.reg(modelKey, model);
31
+ this.container.reg(modelKeyStr, model);
28
32
  model.initialize();
29
33
  }
30
34
  /**
@@ -42,9 +46,10 @@ class AbstractCore {
42
46
  }
43
47
  // 注册与获取管理器
44
48
  regManager(managerKey) {
45
- const ManagerClass = getManagerClass(managerKey);
49
+ const managerKeyStr = managerKey;
50
+ const ManagerClass = getManagerClass(managerKeyStr);
46
51
  const manager = new ManagerClass();
47
- this.container.reg(managerKey, manager);
52
+ this.container.reg(managerKeyStr, manager);
48
53
  manager.initialize();
49
54
  }
50
55
  /**
@@ -60,6 +65,22 @@ class AbstractCore {
60
65
  getManager(managerKey) {
61
66
  return this.container.get(managerKey);
62
67
  }
68
+ /**
69
+ * 检查 Model 是否已注册
70
+ * @param modelKey Model 的 Key
71
+ * @returns 是否已注册
72
+ */
73
+ hasModel(modelKey) {
74
+ return this.container.has(modelKey);
75
+ }
76
+ /**
77
+ * 检查 Manager 是否已注册
78
+ * @param managerKey Manager 的 Key
79
+ * @returns 是否已注册
80
+ */
81
+ hasManager(managerKey) {
82
+ return this.container.has(managerKey);
83
+ }
63
84
  }
64
85
  class AbstractManager {
65
86
  dispose() {
@@ -1,33 +1,4 @@
1
- import { ICore } from "./Api";
2
- /**
3
- * ModelNames 接口(由业务层扩展以提供代码补全和类型推断)
4
- * @example
5
- * ```typescript
6
- * // 在 .d.ts 文件中扩展
7
- * interface ModelNamesType {
8
- * readonly User: 'User';
9
- * }
10
- * ```
11
- */
12
- export interface ModelNamesType extends Record<string, string> {
13
- }
14
- /**
15
- * ManagerNames 接口(由业务层扩展以提供代码补全和类型推断)
16
- * @example
17
- * ```typescript
18
- * // 在 .d.ts 文件中扩展
19
- * interface ManagerNamesType {
20
- * readonly Home: 'Home';
21
- * }
22
- * ```
23
- */
24
- export interface ManagerNamesType extends Record<string, string> {
25
- }
26
- /**
27
- * ViewNames 接口(由业务层扩展以提供代码补全和类型推断)
28
- */
29
- export interface ViewNamesType extends Record<string, string> {
30
- }
1
+ import { ICore, ModelNamesType, ManagerNamesType, ViewNamesType } from "./Api";
31
2
  /** Model 名称常量对象,用于代码补全和类型推断 */
32
3
  export declare const ModelNames: ModelNamesType;
33
4
  /** Manager 名称常量对象,用于代码补全和类型推断 */
@@ -132,6 +103,7 @@ export declare function getViewClass<T>(viewKey: string): new () => T;
132
103
  /**
133
104
  * 自动注册所有使用装饰器标记的 Model 和 Manager
134
105
  * @param core Core 实例
106
+ * @param options 注册选项
135
107
  * @example
136
108
  * ```typescript
137
109
  * // 导入所有 Model 和 Manager
@@ -140,6 +112,15 @@ export declare function getViewClass<T>(viewKey: string): new () => T;
140
112
  *
141
113
  * // 自动注册
142
114
  * autoRegister(mf.core);
115
+ *
116
+ * // 带选项的自动注册
117
+ * autoRegister(mf.core, {
118
+ * skipExisting: true, // 跳过已注册的
119
+ * verbose: false // 静默模式
120
+ * });
143
121
  * ```
144
122
  */
145
- export declare function autoRegister(core: ICore): void;
123
+ export declare function autoRegister(core: ICore, options?: {
124
+ skipExisting?: boolean;
125
+ verbose?: boolean;
126
+ }): void;
@@ -182,6 +182,7 @@ function getViewClass(viewKey) {
182
182
  /**
183
183
  * 自动注册所有使用装饰器标记的 Model 和 Manager
184
184
  * @param core Core 实例
185
+ * @param options 注册选项
185
186
  * @example
186
187
  * ```typescript
187
188
  * // 导入所有 Model 和 Manager
@@ -190,17 +191,36 @@ function getViewClass(viewKey) {
190
191
  *
191
192
  * // 自动注册
192
193
  * autoRegister(mf.core);
194
+ *
195
+ * // 带选项的自动注册
196
+ * autoRegister(mf.core, {
197
+ * skipExisting: true, // 跳过已注册的
198
+ * verbose: false // 静默模式
199
+ * });
193
200
  * ```
194
201
  */
195
- function autoRegister(core) {
202
+ function autoRegister(core, options = {}) {
203
+ const { skipExisting = false, verbose = true } = options;
196
204
  // 注册所有 Model
197
205
  ctorToModelKey.forEach((modelKey, ctor) => {
198
- console.log(`${ctor.name} initialize`);
206
+ if (skipExisting && core.hasModel(modelKey)) {
207
+ if (verbose)
208
+ console.log(`${ctor.name} already registered, skipping`);
209
+ return;
210
+ }
211
+ if (verbose)
212
+ console.log(`${ctor.name} initialize`);
199
213
  core.regModel(modelKey);
200
214
  });
201
215
  // 注册所有 Manager
202
216
  ctorToManagerKey.forEach((managerKey, ctor) => {
203
- console.log(`${ctor.name} initialize`);
217
+ if (skipExisting && core.hasManager(managerKey)) {
218
+ if (verbose)
219
+ console.log(`${ctor.name} already registered, skipping`);
220
+ return;
221
+ }
222
+ if (verbose)
223
+ console.log(`${ctor.name} initialize`);
204
224
  core.regManager(managerKey);
205
225
  });
206
226
  }
@@ -1,5 +1,6 @@
1
1
  import { Component } from 'cc';
2
- import { IView, IManager, IModel, IEventManager, ICocosResManager } from '../core';
2
+ import { IView, IEventManager, ICocosResManager } from '../core';
3
+ import { ModelNamesType, ManagerNamesType } from '../core';
3
4
  export declare abstract class BaseView extends Component implements IView {
4
5
  /** @internal */
5
6
  private readonly __isIView__;
@@ -16,6 +17,26 @@ export declare abstract class BaseView extends Component implements IView {
16
17
  abstract onEnter(args?: any): void;
17
18
  onExit(): void;
18
19
  protected onDestroy(): void;
19
- protected getManager<T extends IManager>(managerSymbol: symbol): T;
20
- protected getModel<T extends IModel>(modelSymbol: symbol): T;
20
+ /**
21
+ * 获取 Model 实例
22
+ * @param modelKey Model 的 Key,使用 ModelNames.XXX
23
+ * @returns Model 实例(具体类型由 .d.ts 文件的函数重载推断)
24
+ * @example
25
+ * ```typescript
26
+ * // 类型由 .d.ts 文件的重载自动推断
27
+ * const userModel = this.getModel(ModelNames.User);
28
+ * ```
29
+ */
30
+ protected getModel<T extends keyof ModelNamesType>(modelKey: T): any;
31
+ /**
32
+ * 获取 Manager 实例
33
+ * @param managerKey Manager 的 Key,使用 ManagerNames.XXX
34
+ * @returns Manager 实例(具体类型由 .d.ts 文件的函数重载推断)
35
+ * @example
36
+ * ```typescript
37
+ * // 类型由 .d.ts 文件的重载自动推断
38
+ * const gameManager = this.getManager(ManagerNames.Game);
39
+ * ```
40
+ */
41
+ protected getManager<T extends keyof ManagerNamesType>(managerKey: T): any;
21
42
  }
@@ -69,13 +69,31 @@ class BaseView extends Component {
69
69
  });
70
70
  this._loaderHandlers = [];
71
71
  }
72
- getManager(managerSymbol) {
73
- // 业务组件避免直接依赖底层服务定位器,所以使用app.core统一对外接口,方便后续架构演进
74
- return mf.core.getManager(managerSymbol);
72
+ /**
73
+ * 获取 Model 实例
74
+ * @param modelKey Model 的 Key,使用 ModelNames.XXX
75
+ * @returns Model 实例(具体类型由 .d.ts 文件的函数重载推断)
76
+ * @example
77
+ * ```typescript
78
+ * // 类型由 .d.ts 文件的重载自动推断
79
+ * const userModel = this.getModel(ModelNames.User);
80
+ * ```
81
+ */
82
+ getModel(modelKey) {
83
+ return mf.core.getModel(modelKey);
75
84
  }
76
- getModel(modelSymbol) {
77
- // 业务组件避免直接依赖底层服务定位器,所以使用app.core统一对外接口,方便后续架构演进
78
- return mf.core.getModel(modelSymbol);
85
+ /**
86
+ * 获取 Manager 实例
87
+ * @param managerKey Manager 的 Key,使用 ManagerNames.XXX
88
+ * @returns Manager 实例(具体类型由 .d.ts 文件的函数重载推断)
89
+ * @example
90
+ * ```typescript
91
+ * // 类型由 .d.ts 文件的重载自动推断
92
+ * const gameManager = this.getManager(ManagerNames.Game);
93
+ * ```
94
+ */
95
+ getManager(managerKey) {
96
+ return mf.core.getManager(managerKey);
79
97
  }
80
98
  }
81
99
 
@@ -1,11 +1,12 @@
1
1
  import { Component } from "cc";
2
2
  import { IUIManager, IView } from "../core";
3
+ import { ViewNamesType } from "../core";
3
4
  type ICocosView = IView & Component;
4
5
  declare abstract class CcocosUIManager implements IUIManager {
5
6
  getTopView(): IView | undefined;
6
- open(viewKey: string, args?: any): Promise<IView>;
7
- close(viewKey: string | IView, destory?: boolean): void;
8
- openAndPush(viewKey: string, group: string, args?: any): Promise<IView>;
7
+ open(viewKey: keyof ViewNamesType, args?: any): Promise<IView>;
8
+ close(viewKey: keyof ViewNamesType | IView, destory?: boolean): void;
9
+ openAndPush(viewKey: keyof ViewNamesType, group: string, args?: any): Promise<IView>;
9
10
  closeAndPop(group: string, destroy?: boolean): void;
10
11
  clearStack(group: string, destroy?: boolean): void;
11
12
  protected abstract internalOpen(viewKey: string, args?: any): Promise<ICocosView>;
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dzkcc-mflow",
3
- "version": "0.0.34",
3
+ "version": "0.0.36",
4
4
  "description": "A modular design and process management framework developed for the cocos engine, suitable for decoupling and dependency injection.",
5
5
  "author": "duanzhk",
6
6
  "license": "MIT",