@ives_xxz/framework 1.5.13 → 1.5.15

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,10 +1,10 @@
1
- import 'reflect-metadata';
2
- import FWLog from '../log/FWLog';
1
+ import "reflect-metadata";
2
+ import FWLog from "../log/FWLog";
3
3
 
4
4
  type PropertyDecorator = (
5
5
  $class: Record<string, any>,
6
6
  $propertyKey: string | symbol,
7
- $descriptorOrInitializer?: any,
7
+ $descriptorOrInitializer?: any
8
8
  ) => void;
9
9
 
10
10
  const searchChild = function (node: cc.Node, name: string) {
@@ -21,7 +21,7 @@ const searchChild = function (node: cc.Node, name: string) {
21
21
 
22
22
  const CookDecoratorKey = ($desc: string) => `__ccc_decorator_${$desc}__`;
23
23
 
24
- const KeyChild = CookDecoratorKey('child_cache');
24
+ const KeyChild = CookDecoratorKey("child_cache");
25
25
 
26
26
  type ParamType = {
27
27
  path?: string;
@@ -30,26 +30,33 @@ type ParamType = {
30
30
  export function FWPropertyNode($opt?: ParamType): PropertyDecorator {
31
31
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
32
32
  return ($target, $propertyKey: string, $descriptorOrInitializer) => {
33
- const cache: { propertyKey: string; childName: string }[] = ($target[KeyChild] ??= []);
33
+ const cache: { propertyKey: string; childName: string }[] = ($target[
34
+ KeyChild
35
+ ] ??= []);
34
36
  if (!cache.some(($vo) => $vo.propertyKey === $propertyKey)) {
35
37
  cache.push({
36
38
  propertyKey: $propertyKey,
37
39
  childName: $opt?.path || $propertyKey,
38
40
  });
39
41
  } else {
40
- throw new Error(`child 装饰器重复绑定属性:${$propertyKey},class:${$target?.name}`);
42
+ throw new Error(
43
+ `child 装饰器重复绑定属性:${$propertyKey},class:${$target?.name}`
44
+ );
41
45
  }
42
46
  if (cache.length === 1) {
43
47
  const oldOnLoad: () => void = $target.onLoad || undefined;
44
48
  $target.onLoad = function () {
45
- cache.forEach(($vo) => (this[$vo.propertyKey] = searchChild(this.node, $vo.childName)));
49
+ cache.forEach(
50
+ ($vo) =>
51
+ (this[$vo.propertyKey] = searchChild(this.node, $vo.childName))
52
+ );
46
53
  oldOnLoad && oldOnLoad.apply(this);
47
54
  };
48
55
  }
49
56
  };
50
57
  }
51
58
 
52
- const KeyChildMulti = CookDecoratorKey('child_cache_multi'); // 重新定义 Key,避免冲突
59
+ const KeyChildMulti = CookDecoratorKey("child_cache_multi"); // 重新定义 Key,避免冲突
53
60
 
54
61
  /** 查找多个节点,并存储为数组 */
55
62
  export function FWPropertyNodes(...paths: string[]): PropertyDecorator {
@@ -59,7 +66,8 @@ export function FWPropertyNodes(...paths: string[]): PropertyDecorator {
59
66
  $target[KeyChildMulti] = [];
60
67
  }
61
68
 
62
- const cache: { propertyKey: string; childNames: string[] }[] = $target[KeyChildMulti];
69
+ const cache: { propertyKey: string; childNames: string[] }[] =
70
+ $target[KeyChildMulti];
63
71
 
64
72
  // 查找是否已经存在该属性的绑定
65
73
  const existingEntry = cache.find(($vo) => $vo.propertyKey === $propertyKey);
@@ -72,7 +80,7 @@ export function FWPropertyNodes(...paths: string[]): PropertyDecorator {
72
80
  });
73
81
  } else {
74
82
  throw new Error(
75
- `child 装饰器重复绑定属性:${$propertyKey}, class:${$target.constructor.name}`,
83
+ `child 装饰器重复绑定属性:${$propertyKey}, class:${$target.constructor.name}`
76
84
  );
77
85
  }
78
86
 
@@ -115,12 +123,12 @@ interface INewable<T = any> extends Function {
115
123
  new (...args: any[]): T;
116
124
  }
117
125
 
118
- const KeyComp = CookDecoratorKey('comp_cache');
126
+ const KeyComp = CookDecoratorKey("comp_cache");
119
127
 
120
128
  export function FWPropertyComponent(
121
129
  $componentClass: INewable<cc.Component>,
122
130
  $childName?: string,
123
- $mute = false,
131
+ $mute = false
124
132
  ): PropertyDecorator {
125
133
  return ($target, $propertyKey: string, $descriptorOrInitializer) => {
126
134
  const cache: {
@@ -136,7 +144,9 @@ export function FWPropertyComponent(
136
144
  });
137
145
  } else {
138
146
  if (!$mute) {
139
- throw new Error(`component装饰器重复绑定属性:${$propertyKey},class:${$target.name}`);
147
+ throw new Error(
148
+ `component装饰器重复绑定属性:${$propertyKey},class:${$target.name}`
149
+ );
140
150
  }
141
151
  return;
142
152
  }
@@ -144,18 +154,21 @@ export function FWPropertyComponent(
144
154
  const oldOnLoad: () => void = $target.onLoad || undefined; //$target.onLoad也可以拿到父类的实现
145
155
  $target.onLoad = function () {
146
156
  cache.forEach(($vo) => {
147
- const node = $vo.childName ? searchChild(this.node, $vo.childName) : this.node;
157
+ const node = $vo.childName
158
+ ? searchChild(this.node, $vo.childName)
159
+ : this.node;
148
160
  if (!node) {
149
161
  if (!$mute) {
150
162
  throw new Error(
151
- `component装饰器没有找到适合的node节点:class:${$target?.name},组件:${$componentClass?.name},childName:${$childName}`,
163
+ `component装饰器没有找到适合的node节点:class:${$target?.name},组件:${$componentClass?.name},childName:${$childName}`
152
164
  );
153
165
  } else {
154
166
  return;
155
167
  }
156
168
  }
157
169
  this[$vo.propertyKey] =
158
- node.getComponent($vo.compClass) || node.addComponent($vo.compClass);
170
+ node.getComponent($vo.compClass) ||
171
+ node.addComponent($vo.compClass);
159
172
  });
160
173
  oldOnLoad && oldOnLoad.apply(this);
161
174
  };
@@ -165,7 +178,7 @@ export function FWPropertyComponent(
165
178
 
166
179
  export function FWPropertyComponents(
167
180
  $componentClass: INewable<cc.Component>,
168
- $childName?: string,
181
+ $childName?: string
169
182
  ): PropertyDecorator {
170
183
  return ($target, $propertyKey: string, $descriptorOrInitializer) => {
171
184
  const componentName = $childName || $propertyKey;
@@ -210,7 +223,11 @@ export function isNull(message?: string) {
210
223
  }
211
224
 
212
225
  export function FWSocketAutoProcessPause() {
213
- return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
226
+ return function (
227
+ target: any,
228
+ propertyKey: string,
229
+ descriptor: PropertyDescriptor
230
+ ) {
214
231
  const originalMethod = descriptor.value;
215
232
 
216
233
  descriptor.value = async function (...args: any[]) {
@@ -218,7 +235,7 @@ export function FWSocketAutoProcessPause() {
218
235
  const result = await originalMethod.apply(this, args);
219
236
  return result;
220
237
  } finally {
221
- FWLog.debug('暂停socket消息处理');
238
+ FWLog.debug("暂停socket消息处理");
222
239
  FW.Entry.socketMgr.resumeMessageHandle();
223
240
  }
224
241
  };
@@ -228,17 +245,25 @@ export function FWSocketAutoProcessPause() {
228
245
  }
229
246
 
230
247
  export function FWDeprecated(description?: string) {
231
- return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
248
+ return function (
249
+ target: any,
250
+ propertyKey: string,
251
+ descriptor: PropertyDescriptor
252
+ ) {
232
253
  const originalMethod = descriptor.value;
233
254
  descriptor.value = async function (...args: any[]) {
234
- FWLog.warn(`${propertyKey}方法已弃用!${description || ''}`);
255
+ FWLog.warn(`${propertyKey}方法已弃用!${description || ""}`);
235
256
  originalMethod.apply(this, args);
236
257
  };
237
258
  return descriptor;
238
259
  };
239
260
  }
240
261
  export function FWSocketAutoProcessResume() {
241
- return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
262
+ return function (
263
+ target: any,
264
+ propertyKey: string,
265
+ descriptor: PropertyDescriptor
266
+ ) {
242
267
  const originalMethod = descriptor.value;
243
268
 
244
269
  descriptor.value = async function (...args: any[]) {
@@ -246,7 +271,7 @@ export function FWSocketAutoProcessResume() {
246
271
  const result = await originalMethod.apply(this, args);
247
272
  return result;
248
273
  } finally {
249
- FWLog.debug('恢复socket消息处理');
274
+ FWLog.debug("恢复socket消息处理");
250
275
  FW.Entry.socketMgr.resumeMessageHandle();
251
276
  }
252
277
  };
@@ -254,3 +279,140 @@ export function FWSocketAutoProcessResume() {
254
279
  return descriptor;
255
280
  };
256
281
  }
282
+
283
+ export function RegisterEvents(events: FW.RegisterEventArgs[]) {
284
+ return function (
285
+ target: any,
286
+ propertyKey: string,
287
+ descriptor: PropertyDescriptor
288
+ ) {
289
+ const originalMethod = descriptor.value;
290
+
291
+ descriptor.value = function (...args: any[]) {
292
+ // 在初始化时自动注册事件
293
+ if (this.registerEvent) {
294
+ this.registerEvent(events);
295
+ }
296
+ return originalMethod.apply(this, args);
297
+ };
298
+
299
+ return descriptor;
300
+ };
301
+ }
302
+
303
+ export function AutoRegisterCCEvent(nodePath: string, eventName?: string) {
304
+ return function (
305
+ target: any,
306
+ propertyKey: string,
307
+ descriptor: PropertyDescriptor
308
+ ) {
309
+ const originalMethod = descriptor.value;
310
+ const originalOnInit = target.onInit;
311
+ const evt = eventName || cc.Node.EventType.TOUCH_END;
312
+
313
+ target.onInit = function () {
314
+ originalOnInit?.call(this);
315
+
316
+ let targetNode: cc.Node | null = null;
317
+
318
+ const cachedNodes = this[KeyChild] || [];
319
+ const cachedNode = cachedNodes.find(
320
+ (vo: any) => vo.childName === nodePath
321
+ );
322
+
323
+ if (cachedNode && this[cachedNode.propertyKey]) {
324
+ targetNode = this[cachedNode.propertyKey];
325
+ } else {
326
+ targetNode = searchChild(this.layer?.node || this.node, nodePath);
327
+
328
+ if (targetNode && !cachedNode) {
329
+ const cacheKey = `_cached_${nodePath.replace(/\//g, "_")}`;
330
+ this[cacheKey] = targetNode;
331
+ if (!this[KeyChild]) this[KeyChild] = [];
332
+ this[KeyChild].push({
333
+ propertyKey: cacheKey,
334
+ childName: nodePath,
335
+ });
336
+ }
337
+ }
338
+
339
+ if (targetNode) {
340
+ this.registerCCEvent(evt, {
341
+ cb: originalMethod.bind(this),
342
+ target: targetNode,
343
+ });
344
+ FWLog.debug(`自动注册事件: ${nodePath} -> ${propertyKey}`);
345
+ } else {
346
+ FWLog.warn(`自动注册事件失败: 未找到节点 ${nodePath}`);
347
+ }
348
+ };
349
+ };
350
+ }
351
+
352
+ export function AutoRegisterFWEvent(eventName: any) {
353
+ return function (
354
+ target: any,
355
+ propertyKey: string,
356
+ descriptor: PropertyDescriptor
357
+ ) {
358
+ const originalMethod = descriptor.value;
359
+ const originalOnInit = target.onInit;
360
+
361
+ target.onInit = function (...args) {
362
+ originalOnInit?.call(this, args);
363
+ this.registerCCEvent(eventName, {
364
+ cb: originalMethod.bind(this),
365
+ target: this,
366
+ });
367
+ };
368
+ };
369
+ }
370
+
371
+ export function PerformanceMonitor(operationName?: string) {
372
+ return function (
373
+ target: any,
374
+ propertyKey: string,
375
+ descriptor: PropertyDescriptor
376
+ ) {
377
+ const originalMethod = descriptor.value;
378
+
379
+ descriptor.value = async function (...args: any[]) {
380
+ const startTime = Date.now();
381
+ const moduleName = target.constructor.name;
382
+ let methodName = propertyKey;
383
+
384
+ if (args && args[0].type) {
385
+ methodName = `${methodName}->${args[0]?.type?.name}`;
386
+ } else {
387
+ methodName = operationName || propertyKey;
388
+ }
389
+
390
+ try {
391
+ const result = await originalMethod.apply(this, args);
392
+ const duration = Date.now() - startTime;
393
+
394
+ FW.Entry.performanceMgr?.recordOperationMetric(
395
+ moduleName,
396
+ methodName,
397
+ duration
398
+ );
399
+
400
+ if (duration > 100) {
401
+ FWLog.warn(`[${moduleName}] ${methodName} took ${duration}ms`);
402
+ }
403
+
404
+ return result;
405
+ } catch (error) {
406
+ const duration = Date.now() - startTime;
407
+ FW.Entry.performanceMgr?.recordOperationMetric(
408
+ moduleName,
409
+ `${methodName}_error`,
410
+ duration
411
+ );
412
+ throw error;
413
+ }
414
+ };
415
+
416
+ return descriptor;
417
+ };
418
+ }
@@ -11,6 +11,7 @@ import FWSocketSender from "../service/socket/FWSocketSender";
11
11
  import FWSocketHandle from "../service/socket/FWSocketHandle";
12
12
  import Framework from "../Framework";
13
13
  import { FrameworkBase } from "../FrameworkBase";
14
+ import { PerformanceMonitor } from "../expand/FWDecorator";
14
15
 
15
16
  const ADD_EXTERNAL_REFERENCE: string = "addExternalReference";
16
17
 
@@ -43,6 +44,7 @@ export class FWLayerManager extends FWManager implements FW.LayerManager {
43
44
  /**
44
45
  * 异步打开Layer
45
46
  */
47
+ @PerformanceMonitor("openAsync")
46
48
  async openAsync<Ctr extends FW.LayerController = FW.LayerController>(
47
49
  data: FW.LayerOpenArgs
48
50
  ): Promise<Ctr> {
@@ -52,6 +54,7 @@ export class FWLayerManager extends FWManager implements FW.LayerManager {
52
54
  /**
53
55
  * 同步打开
54
56
  */
57
+ @PerformanceMonitor("openSync")
55
58
  openSync<Ctr extends FW.LayerController = FW.LayerController>(
56
59
  data: FW.LayerOpenArgs
57
60
  ): Ctr {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ives_xxz/framework",
3
- "version": "1.5.13",
3
+ "version": "1.5.15",
4
4
  "description": "cocoscreator 2.x mvc framework",
5
5
  "main": "index.js",
6
6
  "keywords": [