@ives_xxz/framework 1.5.1 → 1.5.3

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.
package/FW.d.ts CHANGED
@@ -296,6 +296,8 @@ declare namespace FW {
296
296
 
297
297
  send(msg);
298
298
 
299
+ socket: WebSocket;
300
+
299
301
  createSocket(
300
302
  address: string,
301
303
  tag: string,
package/Framework.ts CHANGED
@@ -2,7 +2,6 @@ import 'reflect-metadata';
2
2
  import { Container, interfaces } from 'inversify';
3
3
  import { FWSystemDefine } from './define/FWSystemDefine';
4
4
  import FWLog from './log/FWLog';
5
- import { FWEventDefine } from './define/FWEventDefine';
6
5
 
7
6
  class Framework {
8
7
  private container: Container;
package/FrameworkBase.ts CHANGED
@@ -1,23 +1,87 @@
1
1
  import { injectable } from 'inversify';
2
2
  import { FWEventDefine } from './define/FWEventDefine';
3
3
  import FWLog from './log/FWLog';
4
+ import { FWSystemDefine } from './define/FWSystemDefine';
5
+ import Framework from './Framework';
6
+ import FWLogic from './logic/FWLogic';
7
+ import FWData from './data/FWData';
4
8
  @injectable()
5
9
  export abstract class FrameworkBase {
6
10
  public readonly entry: FW.Entry = FW.Entry;
11
+ public logic?: FW.Logic;
12
+ public data?: FW.Data;
13
+ public config?: FW.Config;
14
+ public sender?: FW.SocketSender;
15
+ public handle?: FW.SocketHandle;
16
+ protected abstract initialize?(): void;
17
+ protected abstract onDestroy?(): void;
18
+ protected onRestart?(): void;
7
19
 
8
20
  constructor() {
9
21
  this.initialize?.();
10
-
11
22
  this.entry.evtMgr.register(FWEventDefine.SystemEvent.SYSTEM_RESTART, this.onRestart, this);
12
23
  }
13
24
 
14
- public abstract initialize?(): void;
15
- public abstract onDestroy?(): void;
16
-
17
25
  protected get moduleName(): string {
18
26
  return this.constructor.name;
19
27
  }
20
- protected onRestart?(): void;
28
+
29
+ public getLogic<T extends FW.Logic = FWLogic>() {
30
+ return this.logic as T;
31
+ }
32
+
33
+ public getData<T extends FW.Data = FWData>() {
34
+ return this.data as T;
35
+ }
36
+
37
+ public getSender<T extends FW.SocketSender>() {
38
+ return this.sender as T;
39
+ }
40
+
41
+ public getHandle<T extends FW.SocketHandle>() {
42
+ return this.handle as T;
43
+ }
44
+
45
+ public getConfig<T extends FW.Config>() {
46
+ return this.config as T;
47
+ }
48
+
49
+ protected dependencies(): void {
50
+ const bundleName = this.findBundleNameByClassName();
51
+ if (!bundleName) return;
52
+
53
+ const tag = this.getClassTag();
54
+
55
+ if (tag !== FWSystemDefine.FWBindTag.LOGIC) {
56
+ this.logic = Framework.getComponent<FW.Logic>(
57
+ `${bundleName}${FWSystemDefine.FWBindTag.LOGIC}`,
58
+ );
59
+ }
60
+
61
+ if (tag !== FWSystemDefine.FWBindTag.DATA) {
62
+ this.data = Framework.getComponent<FW.Data>(`${bundleName}${FWSystemDefine.FWBindTag.DATA}`);
63
+ }
64
+
65
+ if (tag !== FWSystemDefine.FWBindTag.CONFIG) {
66
+ this.config = Framework.getComponent<FW.Config>(
67
+ `${bundleName}${FWSystemDefine.FWBindTag.CONFIG}`,
68
+ );
69
+ }
70
+
71
+ if (tag !== FWSystemDefine.FWBindTag.SENDER) {
72
+ this.sender = Framework.getComponent<FW.SocketSender>(
73
+ `${bundleName}${FWSystemDefine.FWBindTag.SENDER}`,
74
+ );
75
+ }
76
+
77
+ if (tag !== FWSystemDefine.FWBindTag.HANDLE) {
78
+ this.handle = Framework.getComponent<FW.SocketHandle>(
79
+ `${bundleName}${FWSystemDefine.FWBindTag.HANDLE}`,
80
+ );
81
+ }
82
+
83
+ FWLog.debug(`[${this.moduleName}] Dependencies setup for bundle: ${bundleName}`);
84
+ }
21
85
 
22
86
  protected async invoke<T>(operation: Promise<T>, operationName: string = 'unknown'): Promise<T> {
23
87
  const startTime = this.getCurrentTime();
@@ -60,4 +124,37 @@ export abstract class FrameworkBase {
60
124
  protected getCurrentTime(): number {
61
125
  return Date.now();
62
126
  }
127
+
128
+ private findBundleNameByClassName(): string | undefined {
129
+ const registeredComponents = Framework.getRegisteredComponents();
130
+
131
+ const bundleNames = Array.from(registeredComponents.keys());
132
+
133
+ for (const bundleName of bundleNames) {
134
+ if (this.doesClassNameContainBundleName(this.moduleName, bundleName)) {
135
+ return bundleName;
136
+ }
137
+ }
138
+
139
+ return undefined;
140
+ }
141
+
142
+ private doesClassNameContainBundleName(className: string, bundleName: string): boolean {
143
+ const bundleNameLower = bundleName.toLowerCase();
144
+ const classNameLower = className.toLowerCase();
145
+ return classNameLower.includes(bundleNameLower);
146
+ }
147
+
148
+ private getClassTag(): FWSystemDefine.FWBindTag {
149
+ const className = this.moduleName;
150
+ if (className.endsWith('Logic')) return FWSystemDefine.FWBindTag.LOGIC;
151
+ if (className.endsWith('Data')) return FWSystemDefine.FWBindTag.DATA;
152
+ if (className.endsWith('Config') || className.endsWith('AssetConfig'))
153
+ return FWSystemDefine.FWBindTag.CONFIG;
154
+ if (className.endsWith('Sender') || className.endsWith('SocketSender'))
155
+ return FWSystemDefine.FWBindTag.SENDER;
156
+ if (className.endsWith('Handle') || className.endsWith('SocketHandle'))
157
+ return FWSystemDefine.FWBindTag.HANDLE;
158
+ return;
159
+ }
63
160
  }
@@ -34,7 +34,9 @@ export abstract class FWLayerController extends FrameworkBase implements FW.Laye
34
34
  onEnable?();
35
35
  onDisable?();
36
36
  onDestroy?();
37
- initialize() {}
37
+ initialize() {
38
+ this.dependencies();
39
+ }
38
40
  async onClose?();
39
41
 
40
42
  async destroy() {
package/data/FWData.ts CHANGED
@@ -2,6 +2,8 @@ import { injectable } from 'inversify';
2
2
  import { FrameworkBase } from '../FrameworkBase';
3
3
  @injectable()
4
4
  export default class FWData extends FrameworkBase implements FW.Data {
5
- public initialize?(): void;
5
+ public initialize() {
6
+ this.dependencies();
7
+ }
6
8
  public onDestroy?(): void;
7
9
  }
package/logic/FWLogic.ts CHANGED
@@ -1,7 +1,10 @@
1
1
  import { injectable } from 'inversify';
2
2
  import { FrameworkBase } from '../FrameworkBase';
3
+ import { FWSystemDefine } from '../define/FWSystemDefine';
3
4
  @injectable()
4
5
  export default class FWLogic extends FrameworkBase implements FW.Logic {
5
- initialize?();
6
- onDestroy?();
6
+ public initialize() {
7
+ this.dependencies();
8
+ }
9
+ public onDestroy?(): void;
7
10
  }
@@ -18,6 +18,7 @@ export default class FWEngineManager extends FWManager implements FW.EngineManag
18
18
 
19
19
  restart() {
20
20
  FW.Entry.evtMgr.dispatch(FWEventDefine.SystemEvent.SYSTEM_RESTART);
21
+ FW.Entry.initialize();
21
22
  Framework.restart();
22
23
  cc.game.restart();
23
24
  }
@@ -42,7 +42,7 @@ class FWObserver implements FW.Observer {
42
42
  args10?: FW.EventManagerArgs,
43
43
  ) {
44
44
  if (this.pause) return;
45
- this.cb.call(
45
+ this.cb?.call(
46
46
  this.target,
47
47
  args1,
48
48
  args2,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ives_xxz/framework",
3
- "version": "1.5.1",
3
+ "version": "1.5.3",
4
4
  "description": "cocoscreator 2.x mvc framework",
5
5
  "main": "index.js",
6
6
  "keywords": ["123456"],
@@ -1,8 +1,11 @@
1
1
  import { FrameworkBase } from '../FrameworkBase';
2
2
 
3
3
  export default abstract class FWService extends FrameworkBase {
4
- public abstract initialize(): void;
4
+ public initialize() {
5
+ this.dependencies();
6
+ }
5
7
  public abstract onDestroy(): void;
8
+
6
9
  protected onRestart() {
7
10
  this.onDestroy();
8
11
  this.initialize();
@@ -4,8 +4,8 @@ import FWLog from '../../log/FWLog';
4
4
  import FWService from '../FWService';
5
5
 
6
6
  export default class FWHttp extends FWService {
7
- public initialize(): void {}
8
7
  public onDestroy(): void {}
8
+ protected onRestart(): void {}
9
9
 
10
10
  protected async getMessage(
11
11
  url: string,
@@ -15,9 +15,9 @@ export default class FWSocket extends FWService implements FW.Socket {
15
15
  /** socket */
16
16
  socket: WebSocket = null;
17
17
  /** 消息发送者 */
18
- protected sender: FWSocketSender;
18
+ protected socketSender: FWSocketSender;
19
19
  /** 消息处理者 */
20
- protected handle: FWSocketHandle;
20
+ protected socketHandle: FWSocketHandle;
21
21
  /** 重连次数 */
22
22
  protected reconnectTimes: number;
23
23
  /** 心跳间隔时间 */
@@ -56,6 +56,7 @@ export default class FWSocket extends FWService implements FW.Socket {
56
56
  protected messageEvents: { [key: string]: (msg: any) => void };
57
57
 
58
58
  public initialize(): void {
59
+ super.initialize();
59
60
  this.messageEvents = cc.js.createMap();
60
61
  this.protocolContainer = new Map<Symbol, FW.ProtocolPolling>();
61
62
  this.protocolRegistry = new Map<string, Set<Symbol>>();
@@ -65,8 +66,8 @@ export default class FWSocket extends FWService implements FW.Socket {
65
66
  createSocket(
66
67
  address: string,
67
68
  tag: string,
68
- sender: FW.SocketSender,
69
- handle: FW.SocketHandle,
69
+ socketSender: FW.SocketSender,
70
+ socketHandle: FW.SocketHandle,
70
71
  config: FW.SocketConfig,
71
72
  ): FW.SocketPromiseProxy {
72
73
  try {
@@ -104,8 +105,8 @@ export default class FWSocket extends FWService implements FW.Socket {
104
105
  this.socket.onerror = this.onSocketError.bind(this);
105
106
  this.socket.onmessage = this.onSocketMessage.bind(this);
106
107
 
107
- this.sender = sender;
108
- this.handle = handle;
108
+ this.socketSender = socketSender;
109
+ this.socketHandle = socketHandle;
109
110
 
110
111
  const defaultConfig = FWSystemConfig.SocketDefaultConfig;
111
112
  this.heartInternal = config?.heartInternal || defaultConfig.heartInternal;
@@ -197,7 +198,7 @@ export default class FWSocket extends FWService implements FW.Socket {
197
198
  `socket reconnect : reconnectTimes->${this.reconnectTimes},maxReconnectTimes->${this.maxReconnectTimes}`,
198
199
  );
199
200
  if (++this.reconnectTimes >= this.maxReconnectTimes) {
200
- this.handle?.onTimeout?.();
201
+ this.socketHandle?.onTimeout?.();
201
202
  return;
202
203
  }
203
204
  this.clear();
@@ -205,7 +206,7 @@ export default class FWSocket extends FWService implements FW.Socket {
205
206
  this.socket?.close();
206
207
  this.socket = null;
207
208
 
208
- this.createSocket(this.address, this.tag, this.sender, this.handle, {
209
+ this.createSocket(this.address, this.tag, this.socketSender, this.socketHandle, {
209
210
  heartInternal: this.heartInternal,
210
211
  heartTimeout: this.heartTimeout,
211
212
  heartWeakTime: this.heartWeakTime,
@@ -220,8 +221,8 @@ export default class FWSocket extends FWService implements FW.Socket {
220
221
  async send(msg: string) {
221
222
  if (this.getReadyState() != WebSocket.OPEN) return;
222
223
  if (this.isPausedMessageHandle) return;
223
- if (!(await this.sender.onHeart?.(msg))) {
224
- const protocolKey = this.sender.getProtocolKey?.(msg);
224
+ if (!(await this.socketSender.onHeart?.(msg))) {
225
+ const protocolKey = this.socketSender.getProtocolKey?.(msg);
225
226
  if (protocolKey) {
226
227
  const symbol = Symbol(protocolKey);
227
228
  const registry = this.protocolRegistry.get(protocolKey) || new Set<Symbol>();
@@ -250,7 +251,7 @@ export default class FWSocket extends FWService implements FW.Socket {
250
251
  this.reconnectTimes = 0;
251
252
  this.sendHeartTimestamp = 0;
252
253
  this.receiveTimeStamp = new Date().getTime();
253
- this.handle?.onOpen?.();
254
+ this.socketHandle?.onOpen?.();
254
255
  this.heartHandle();
255
256
  this.promiseProxy?.resolve(this);
256
257
  }
@@ -260,7 +261,7 @@ export default class FWSocket extends FWService implements FW.Socket {
260
261
  () => {
261
262
  const interval = this.sendHeartTimestamp - this.receiveTimeStamp;
262
263
  if (interval >= this.heartWeakTime) {
263
- this.handle.onWeakNetWork?.();
264
+ this.socketHandle.onWeakNetWork?.();
264
265
  if (interval >= this.heartTimeout) {
265
266
  this.reconnect();
266
267
  return;
@@ -276,12 +277,12 @@ export default class FWSocket extends FWService implements FW.Socket {
276
277
 
277
278
  private sendHeart() {
278
279
  this.sendHeartTimestamp = new Date().getTime();
279
- this.sender?.sendHeart?.();
280
+ this.socketSender?.sendHeart?.();
280
281
  }
281
282
 
282
283
  private sendMessage(msg: any) {
283
284
  try {
284
- this.socket?.send(this.sender.onBeforeSendingMessage?.(msg) || msg);
285
+ this.socket?.send(this.socketSender.onBeforeSendingMessage?.(msg) || msg);
285
286
  } catch (e) {
286
287
  this.socket?.send(msg);
287
288
  }
@@ -290,7 +291,7 @@ export default class FWSocket extends FWService implements FW.Socket {
290
291
  /** socket关闭 */
291
292
  private onSocketClose() {
292
293
  FWLog.debug('on close!');
293
- this.handle?.onClose?.();
294
+ this.socketHandle?.onClose?.();
294
295
  FW.Entry.timeMgr.scheduleOnce(
295
296
  () => {
296
297
  if (this.getReadyState() == WebSocket.CLOSING || this.getReadyState() == WebSocket.CLOSED) {
@@ -305,14 +306,14 @@ export default class FWSocket extends FWService implements FW.Socket {
305
306
 
306
307
  /** 消息处理 */
307
308
  private async onSocketMessage(originalMsg: any) {
308
- const msg = await this.handle.onBeforeReceivingMessage(originalMsg);
309
+ const msg = await this.socketHandle.onBeforeReceivingMessage(originalMsg);
309
310
 
310
311
  if (msg.error) {
311
312
  this.onSocketError(msg);
312
313
  return;
313
314
  }
314
315
 
315
- if (await this.handle?.onHeart?.(msg)) {
316
+ if (await this.socketHandle?.onHeart?.(msg)) {
316
317
  this.receiveTimeStamp = new Date().getTime();
317
318
  return;
318
319
  }
@@ -321,16 +322,16 @@ export default class FWSocket extends FWService implements FW.Socket {
321
322
 
322
323
  if (this.isPausedMessageHandle) return;
323
324
 
324
- this.handle?.onMessage?.(msg);
325
+ this.socketHandle?.onMessage?.(msg);
325
326
  }
326
327
  /** 错误处理 */
327
328
  private async onSocketError(msg: any) {
328
329
  await this.remoteProtocol(msg);
329
- this.handle?.onError?.(msg);
330
+ this.socketHandle?.onError?.(msg);
330
331
  }
331
332
 
332
333
  private async remoteProtocol(msg: any) {
333
- const protocolKey = await this.handle.getProtocolKey?.(msg);
334
+ const protocolKey = await this.socketHandle.getProtocolKey?.(msg);
334
335
  if (protocolKey && this.protocolRegistry.has(protocolKey)) {
335
336
  const symbolSet = this.protocolRegistry.get(protocolKey);
336
337