@ives_xxz/framework 1.4.15 → 1.5.0

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.
@@ -5,16 +5,14 @@ import { FWManager } from './FWManager';
5
5
  export default class FWPromiseManager extends FWManager implements FW.PromiseManager {
6
6
  private promiseRegistry: Map<number, FW.PromiseProxy>;
7
7
  private uniqueId: number = 0;
8
- private timerSchedule: FW.TimerSchedule;
9
8
 
10
9
  public initialize(): void {
11
10
  this.promiseRegistry = new Map<number, FW.PromiseProxy>();
12
11
  }
13
12
 
14
13
  public onDestroy(): void {
15
- this.cancelAll('Manager destroyed');
14
+ this.cancelAll();
16
15
  this.promiseRegistry.clear();
17
- this.promiseRegistry = null;
18
16
  }
19
17
 
20
18
  /** 创建Promise执行器 */
@@ -24,15 +22,14 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
24
22
  ): FW.PromiseProxy<T> {
25
23
  const id = this.uniqueId++;
26
24
  const abortController = new AbortController();
27
- let retryCount = 0;
28
25
  const maxRetryTimes = options.retryCount || 0;
29
26
  const retryInterval = options.retryInterval || 0;
30
-
27
+ let retryCount = 0;
28
+ let timerSchedule: FW.TimerSchedule;
31
29
  const createPromise = (): Promise<T> => {
32
30
  return new Promise<T>((resolve, reject) => {
33
31
  if (options.timeout && options.timeout > 0) {
34
- this.timerSchedule?.unSchedule();
35
- this.timerSchedule = FW.Entry.timeMgr.scheduleOnce(() => {
32
+ timerSchedule = FW.Entry.timeMgr.scheduleOnce(() => {
36
33
  const timeoutError = new Error(`Promise ${id} timeout after ${options.timeout} s`);
37
34
  if (
38
35
  retryCount < maxRetryTimes &&
@@ -49,13 +46,13 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
49
46
  }
50
47
  } else {
51
48
  abortController.abort(timeoutError.message);
52
- this.timerSchedule?.unSchedule();
49
+ timerSchedule?.unSchedule();
53
50
  }
54
51
  }, options.timeout);
55
52
  }
56
53
 
57
54
  const onAbort = () => {
58
- this.timerSchedule?.unSchedule();
55
+ timerSchedule?.unSchedule();
59
56
  if (promiseProxy.status === FWSystemDefine.FWPromiseStatus.PENDING) {
60
57
  promiseProxy.status = FWSystemDefine.FWPromiseStatus.CANCELLED;
61
58
  this.removePromise(id);
@@ -73,12 +70,12 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
73
70
  promiseProxy.status = FWSystemDefine.FWPromiseStatus.FULFILLED;
74
71
  abortController.signal.removeEventListener('abort', onAbort);
75
72
  this.removePromise(id);
76
- this.timerSchedule?.unSchedule();
73
+ timerSchedule?.unSchedule();
77
74
  resolve(value);
78
75
  };
79
76
 
80
77
  const wrappedReject = (reason?: any) => {
81
- this.timerSchedule?.unSchedule();
78
+ timerSchedule?.unSchedule();
82
79
  if (
83
80
  retryCount < maxRetryTimes &&
84
81
  (!options.retryCondition || options.retryCondition(reason, retryCount))
@@ -102,7 +99,7 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
102
99
  }
103
100
  };
104
101
  try {
105
- executor(wrappedResolve, wrappedReject, abortController.signal);
102
+ executor(wrappedResolve, wrappedReject, abortController.signal, options.reason);
106
103
  } catch (error) {
107
104
  wrappedReject(error);
108
105
  }
@@ -121,6 +118,7 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
121
118
  abortController.abort(reason);
122
119
  }
123
120
  },
121
+
124
122
  addAbortEventListener: (
125
123
  listener: (this: AbortSignal, ev: Event) => any,
126
124
  options?: boolean | AddEventListenerOptions,
@@ -140,15 +138,16 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
140
138
  ): FW.PromiseProxy<FW.PromiseResult<T>> {
141
139
  const id = this.uniqueId++;
142
140
  const abortController = new AbortController();
143
- let retryCount = 0;
144
141
  const maxRetryTimes = options.retryCount || 0;
145
142
  const retryInterval = options.retryInterval || 0;
143
+ let timerSchedule: FW.TimerSchedule;
144
+ let retryCount = 0;
146
145
 
147
146
  const createPromise = (): Promise<FW.PromiseResult<T>> => {
148
147
  return new Promise<FW.PromiseResult<T>>((resolve, reject) => {
149
148
  if (options.timeout && options.timeout > 0) {
150
- this.timerSchedule?.unSchedule();
151
- this.timerSchedule = FW.Entry.timeMgr.scheduleOnce(() => {
149
+ timerSchedule?.unSchedule();
150
+ timerSchedule = FW.Entry.timeMgr.scheduleOnce(() => {
152
151
  const timeoutError = new Error(`All Promise ${id} timeout after ${options.timeout} s`);
153
152
  if (
154
153
  retryCount < maxRetryTimes &&
@@ -165,13 +164,13 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
165
164
  }
166
165
  } else {
167
166
  abortController.abort(timeoutError.message);
168
- this.timerSchedule?.unSchedule();
167
+ timerSchedule?.unSchedule();
169
168
  }
170
169
  }, options.timeout);
171
170
  }
172
171
 
173
172
  const onAbort = () => {
174
- this.timerSchedule?.unSchedule();
173
+ timerSchedule?.unSchedule();
175
174
  if (promiseProxy.status === FWSystemDefine.FWPromiseStatus.PENDING) {
176
175
  promiseProxy.status = FWSystemDefine.FWPromiseStatus.CANCELLED;
177
176
  this.removePromise(id);
@@ -193,7 +192,6 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
193
192
  };
194
193
 
195
194
  for (const promiseProxy of promises) {
196
- // 检查是否已取消
197
195
  if (abortController.signal.aborted) {
198
196
  result.cancelled.push(
199
197
  ...promises
@@ -230,11 +228,11 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
230
228
  promiseProxy.status = FWSystemDefine.FWPromiseStatus.FULFILLED;
231
229
  abortController.signal.removeEventListener('abort', onAbort);
232
230
  this.removePromise(id);
233
- this.timerSchedule?.unSchedule();
231
+ timerSchedule?.unSchedule();
234
232
  resolve(result);
235
233
  })
236
234
  .catch((error) => {
237
- this.timerSchedule?.unSchedule();
235
+ timerSchedule?.unSchedule();
238
236
  if (
239
237
  retryCount < maxRetryTimes &&
240
238
  (!options.retryCondition || options.retryCondition(error, retryCount))
@@ -312,11 +310,10 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
312
310
  public cancelAll(reason?: any): number[] {
313
311
  const cancelled: number[] = [];
314
312
  this.promiseRegistry.forEach((promiseProxy, id) => {
315
- if (promiseProxy.status === FWSystemDefine.FWPromiseStatus.PENDING) {
316
- promiseProxy.abort(reason);
317
- cancelled.push(id);
318
- }
313
+ promiseProxy.abort(reason);
314
+ cancelled.push(id);
319
315
  });
316
+ FWLog.error(cancelled);
320
317
  return cancelled;
321
318
  }
322
319
 
@@ -75,7 +75,7 @@ export class FWResManager extends FWManager implements FW.ResManager {
75
75
  * @returns
76
76
  */
77
77
  async loadAssetData<T extends cc.Asset>(assetProperty: FW.AssetProperty) {
78
- return this.dataProcessing<T>(await this.assetMgr.load(assetProperty));
78
+ return this.process<T>(await this.assetMgr.load(assetProperty));
79
79
  }
80
80
  /**
81
81
  * 加载资源
@@ -84,7 +84,7 @@ export class FWResManager extends FWManager implements FW.ResManager {
84
84
  */
85
85
  async loadAsset<T extends cc.Asset>(assetProperty: FW.AssetProperty): Promise<T> {
86
86
  const res = await this.assetMgr.load(assetProperty);
87
- return this.dataProcessing<T>(res).asset as T;
87
+ return this.process<T>(res).asset as T;
88
88
  }
89
89
 
90
90
  /**
@@ -102,7 +102,7 @@ export class FWResManager extends FWManager implements FW.ResManager {
102
102
  * @returns
103
103
  */
104
104
  getAssetData<T extends cc.Asset>(assetProperty: FW.AssetProperty) {
105
- return this.dataProcessing<T>(this.assetMgr.get(assetProperty));
105
+ return this.process<T>(this.assetMgr.get(assetProperty));
106
106
  }
107
107
  /**
108
108
  * 获取资源
@@ -110,7 +110,7 @@ export class FWResManager extends FWManager implements FW.ResManager {
110
110
  * @returns
111
111
  */
112
112
  getAsset<T extends cc.Asset>(assetProperty: FW.AssetProperty): T {
113
- return this.dataProcessing<T>(this.assetMgr.get(assetProperty)).asset as T;
113
+ return this.process<T>(this.assetMgr.get(assetProperty)).asset as T;
114
114
  }
115
115
 
116
116
  /**
@@ -141,7 +141,7 @@ export class FWResManager extends FWManager implements FW.ResManager {
141
141
  * @param res
142
142
  * @returns
143
143
  */
144
- private dataProcessing<T extends cc.Asset>(res: FW.AssetData) {
144
+ private process<T extends cc.Asset>(res: FW.AssetData) {
145
145
  let asset: cc.Asset;
146
146
 
147
147
  if (res.asset instanceof cc.Texture2D) {
@@ -3,7 +3,9 @@ import { FWManager } from './FWManager';
3
3
 
4
4
  export default class FWTaskManager extends FWManager implements FW.TaskManager {
5
5
  public initialize(): void {}
6
- public onDestroy(): void {}
6
+ public onDestroy(): void {
7
+ this.tasks.forEach((task) => task.stop());
8
+ }
7
9
  /**
8
10
  * 任务合集
9
11
  */
@@ -133,46 +133,18 @@ export class FWTimeManager extends FWManager implements FW.TimeManager {
133
133
  let cb: () => void;
134
134
  let tag: string;
135
135
  let target: any;
136
- if (arguments.length == 1) {
137
- if (typeof arguments[0] === 'number') time = arguments[0];
138
- else if (typeof arguments[0] === 'function') cb = arguments[0];
139
- } else if (arguments.length == 2) {
140
- if (typeof arguments[0] === 'number') {
141
- time = arguments[0];
142
- if (typeof arguments[1] === 'string') {
143
- tag = arguments[1];
144
- } else if (typeof arguments[1] === 'object') {
145
- cb = arguments[1];
146
- } else {
147
- target = arguments[1];
148
- }
149
- } else if (typeof arguments[0] === 'function') {
150
- cb = arguments[0];
151
- if (typeof arguments[1] === 'string') {
152
- tag = arguments[1];
153
- } else if (typeof arguments[1] === 'number') {
154
- time = arguments[1];
155
- } else {
156
- target = arguments[1];
157
- }
158
- }
159
- } else if (arguments.length == 3) {
160
- if (typeof arguments[0] === 'number') {
161
- time = arguments[0];
162
- cb = arguments[1];
163
- if (typeof arguments[2] === 'string') {
164
- tag = arguments[2];
165
- } else {
166
- target = arguments[2];
167
- }
168
- } else if (typeof arguments[0] === 'function') {
169
- cb = arguments[0];
170
- time = arguments[1];
171
- if (typeof arguments[2] === 'string') {
172
- tag = arguments[2];
173
- } else {
174
- target = arguments[2];
175
- }
136
+
137
+ const config: any = {};
138
+
139
+ for (const arg of arguments) {
140
+ if (typeof arg === 'function') {
141
+ cb = arg;
142
+ } else if (typeof arg === 'number') {
143
+ time = arg;
144
+ } else if (typeof arg === 'string') {
145
+ tag = arg;
146
+ } else if (arg) {
147
+ target = arg;
176
148
  }
177
149
  }
178
150
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ives_xxz/framework",
3
- "version": "1.4.15",
3
+ "version": "1.5.0",
4
4
  "description": "cocoscreator 2.x mvc framework",
5
5
  "main": "index.js",
6
6
  "keywords": ["123456"],
@@ -1,54 +1,10 @@
1
- import FWLog from '../log/FWLog';
1
+ import { FrameworkBase } from '../FrameworkBase';
2
2
 
3
- export default abstract class FWService {
4
- constructor() {
5
- this.initialize();
6
- }
3
+ export default abstract class FWService extends FrameworkBase {
7
4
  public abstract initialize(): void;
8
- public abstract destroy(): void;
9
- public abstract readonly serviceName: string;
10
-
11
- protected async invoke<T>(operation: Promise<T>, operationName: string = 'unknown'): Promise<T> {
12
- const startTime = this.getCurrentTime();
13
-
14
- try {
15
- const result = await operation;
16
- const duration = this.getCurrentTime() - startTime;
17
-
18
- this.recordPerformanceMetric(operationName, duration);
19
- return result;
20
- } catch (error) {
21
- this.handleError(operationName, error);
22
- return undefined;
23
- }
24
- }
25
-
26
- private recordPerformanceMetric(operationName: string, duration: number): void {
27
- if (FW.Entry.performanceMgr) {
28
- FW.Entry.performanceMgr.recordOperationMetric(this.serviceName, operationName, duration);
29
- }
30
-
31
- if (FW.Entry.engineMgr.debug) {
32
- const log = duration > 1000 ? FWLog.warn : FWLog.debug;
33
- log(`Operation ${operationName} took ${duration}ms`);
34
- }
35
- }
36
-
37
- protected getCurrentTime(): number {
38
- return Date.now();
39
- }
40
-
41
- protected handleError(operation: string, error: any): void {
42
- const errorInfo = {
43
- manager: this.serviceName,
44
- operation,
45
- error: error?.message || error,
46
- stack: error?.stack,
47
- };
48
-
49
- FWLog.error(`Manager error in ${this.serviceName}.${operation}:`, errorInfo);
50
-
51
- if (FW.Entry.engineMgr.debug) {
52
- }
5
+ public abstract onDestroy(): void;
6
+ protected onRestart() {
7
+ this.onDestroy();
8
+ this.initialize();
53
9
  }
54
10
  }
@@ -1,11 +1,11 @@
1
+ import { FWSystemConfig } from '../../config/FWSystemConfig';
1
2
  import { FWSystemDefine } from '../../define/FWSystemDefine';
2
3
  import FWLog from '../../log/FWLog';
3
4
  import FWService from '../FWService';
4
5
 
5
6
  export default class FWHttp extends FWService {
6
- public serviceName: string = 'HTTP';
7
7
  public initialize(): void {}
8
- public destroy(): void {}
8
+ public onDestroy(): void {}
9
9
 
10
10
  protected async getMessage(
11
11
  url: string,
@@ -13,7 +13,7 @@ export default class FWHttp extends FWService {
13
13
  cb?: (response: string) => void,
14
14
  tag?: string,
15
15
  ): Promise<string> {
16
- return this.dataProcessing(url, params, cb, tag, FWSystemDefine.FWHttpRequestType.GET);
16
+ return this.process(url, params, cb, tag, FWSystemDefine.FWHttpRequestType.GET);
17
17
  }
18
18
 
19
19
  protected async postMessage(
@@ -22,10 +22,10 @@ export default class FWHttp extends FWService {
22
22
  cb?: (response: string) => void,
23
23
  tag?: string,
24
24
  ): Promise<string> {
25
- return this.dataProcessing(url, params, cb, tag, FWSystemDefine.FWHttpRequestType.POST);
25
+ return this.process(url, params, cb, tag, FWSystemDefine.FWHttpRequestType.POST);
26
26
  }
27
27
 
28
- private async dataProcessing(
28
+ private async process(
29
29
  url: string,
30
30
  params: string,
31
31
  cb: (response: any) => void,
@@ -33,40 +33,45 @@ export default class FWHttp extends FWService {
33
33
  type: FWSystemDefine.FWHttpRequestType,
34
34
  ): Promise<string> {
35
35
  let xhr: XMLHttpRequest;
36
- return await this.invoke(
37
- FW.Entry.promiseMgr.execute<string>(
38
- (resolve, reject) => {
39
- xhr = new XMLHttpRequest();
40
- xhr.onreadystatechange = () => {
41
- if (xhr.readyState == 4 && xhr.status === 200) {
42
- cb?.(xhr.response);
43
- resolve(xhr.response);
44
- }
45
- };
46
36
 
47
- xhr.onerror = (err: any) => {
48
- FWLog.error(err);
49
- this.onError(err);
50
- reject(`http request error:${err}`);
51
- };
37
+ const promiseProxy: FW.PromiseProxy<string> = FW.Entry.promiseMgr.execute<string>(
38
+ (resolve, reject, signal, reason) => {
39
+ xhr = new XMLHttpRequest();
52
40
 
53
- xhr.ontimeout = () => {
54
- this.onTimeout();
55
- reject(`http request timeout!`);
56
- };
41
+ xhr.onreadystatechange = () => {
42
+ if (xhr.readyState == 4 && xhr.status === 200) {
43
+ cb?.(xhr.response);
44
+ resolve(xhr.response);
45
+ }
46
+ };
57
47
 
58
- xhr.open(type, url, true);
59
- xhr.send(params);
60
- },
61
- {
62
- retryCount: 3,
63
- retryCondition(error, retryCount) {
64
- return xhr.readyState != 4 || (xhr.status !== 200 && retryCount < 3);
65
- },
48
+ xhr.onerror = (err: any) => {
49
+ FWLog.error(err);
50
+ this.onError?.(err);
51
+ reject(`http request error:${err}`);
52
+ };
53
+
54
+ xhr.ontimeout = () => {
55
+ this.onTimeout?.();
56
+ reject(`http request timeout!`);
57
+ };
58
+
59
+ xhr.open(type, url, true);
60
+ xhr.send(params);
61
+ },
62
+ {
63
+ ...FWSystemConfig.PromiseConfig.http,
64
+ retryCondition(error, retryCount) {
65
+ return xhr.readyState != 4 || (xhr.status !== 200 && retryCount < 3);
66
66
  },
67
- ).promise,
68
- tag ? `http request ->${tag}` : '',
67
+ },
69
68
  );
69
+
70
+ promiseProxy.addAbortEventListener(() => {
71
+ xhr.abort();
72
+ });
73
+
74
+ return await this.invoke(promiseProxy.promise, tag ? `http request ->${tag}` : '');
70
75
  }
71
76
 
72
77
  onError?(err: any);
@@ -1,12 +1,13 @@
1
1
  import { FWSystemConfig } from '../../config/FWSystemConfig';
2
2
  import FWLog from '../../log/FWLog';
3
+ import FWService from '../FWService';
3
4
  import FWSocketHandle from './FWSocketHandle';
4
5
  import FWSocketSender from './FWSocketSender';
5
6
 
6
7
  /**
7
8
  * TODO 犹豫socket没有改版暂时已老的cmd字符串映射方式做,后期优化
8
9
  */
9
- export default class FWSocket implements FW.Socket {
10
+ export default class FWSocket extends FWService implements FW.Socket {
10
11
  /** 发送心跳时间抽 */
11
12
  private sendHeartTimestamp: number;
12
13
  /** 接收心跳时间戳 */
@@ -54,10 +55,6 @@ export default class FWSocket implements FW.Socket {
54
55
  */
55
56
  protected messageEvents: { [key: string]: (msg: any) => void };
56
57
 
57
- constructor() {
58
- this.initialize();
59
- }
60
-
61
58
  public initialize(): void {
62
59
  this.messageEvents = cc.js.createMap();
63
60
  this.protocolContainer = new Map<Symbol, FW.ProtocolPolling>();
@@ -102,8 +99,6 @@ export default class FWSocket implements FW.Socket {
102
99
  }
103
100
  }
104
101
 
105
-
106
-
107
102
  this.socket.onopen = this.onSocketOpen.bind(this);
108
103
  this.socket.onclose = this.onSocketClose.bind(this);
109
104
  this.socket.onerror = this.onSocketError.bind(this);
@@ -147,6 +142,7 @@ export default class FWSocket implements FW.Socket {
147
142
 
148
143
  public onDestroy(): void {
149
144
  this.messageEvents = cc.js.createMap();
145
+ this.socket.close();
150
146
  this.socket = null;
151
147
  }
152
148
 
@@ -310,9 +306,9 @@ export default class FWSocket implements FW.Socket {
310
306
  /** 消息处理 */
311
307
  private async onSocketMessage(originalMsg: any) {
312
308
  const msg = await this.handle.onBeforeReceivingMessage(originalMsg);
313
-
314
- if(msg.error){
315
- this.onSocketError(msg)
309
+
310
+ if (msg.error) {
311
+ this.onSocketError(msg);
316
312
  return;
317
313
  }
318
314
 
@@ -320,7 +316,7 @@ export default class FWSocket implements FW.Socket {
320
316
  this.receiveTimeStamp = new Date().getTime();
321
317
  return;
322
318
  }
323
-
319
+
324
320
  this.remoteProtocol(msg);
325
321
 
326
322
  if (this.isPausedMessageHandle) return;