@ives_xxz/framework 1.4.14 → 1.4.16
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 +12 -2
- package/Framework.ts +95 -3
- package/define/FWEventDefine.ts +4 -0
- package/entry/FWEntry.ts +7 -16
- package/manager/FWBundleManager.ts +1 -1
- package/manager/FWEngineManager.ts +8 -0
- package/manager/FWLayerManager.ts +3 -3
- package/manager/FWManager.ts +7 -0
- package/manager/FWPerformanceManager.ts +1 -6
- package/manager/FWPromiseManager.ts +21 -23
- package/manager/FWTaskManager.ts +3 -1
- package/package.json +1 -1
- package/service/FWService.ts +4 -1
- package/service/http/FWHttp.ts +36 -31
- package/service/socket/FWSocket.ts +8 -11
package/FW.d.ts
CHANGED
|
@@ -47,7 +47,7 @@ declare namespace FW {
|
|
|
47
47
|
getDepend(bundleName: string): string[];
|
|
48
48
|
update(dt: number): void;
|
|
49
49
|
initialize(): void;
|
|
50
|
-
|
|
50
|
+
restart(): void;
|
|
51
51
|
};
|
|
52
52
|
|
|
53
53
|
type Manager = {};
|
|
@@ -57,7 +57,8 @@ declare namespace FW {
|
|
|
57
57
|
executor: (
|
|
58
58
|
resolve: (value: T | PromiseLike<T>) => void,
|
|
59
59
|
reject: (reason?: any) => void,
|
|
60
|
-
signal
|
|
60
|
+
signal?: AbortSignal,
|
|
61
|
+
reason?: any,
|
|
61
62
|
) => void = PromiseExcutor,
|
|
62
63
|
options: PromiseExecuteOptions = {},
|
|
63
64
|
): PromiseProxy<T>;
|
|
@@ -972,6 +973,7 @@ declare namespace FW {
|
|
|
972
973
|
type EngineManager = {
|
|
973
974
|
debug: boolean;
|
|
974
975
|
getMemory(): void;
|
|
976
|
+
restart(): void;
|
|
975
977
|
};
|
|
976
978
|
|
|
977
979
|
type Layer = {
|
|
@@ -1162,6 +1164,8 @@ declare namespace FW {
|
|
|
1162
1164
|
* 获取所有任务
|
|
1163
1165
|
*/
|
|
1164
1166
|
getAllTasks(): FW.Task[];
|
|
1167
|
+
|
|
1168
|
+
onDestroy();
|
|
1165
1169
|
};
|
|
1166
1170
|
|
|
1167
1171
|
type StateManager = {
|
|
@@ -1839,6 +1843,10 @@ declare namespace FW {
|
|
|
1839
1843
|
* 重置
|
|
1840
1844
|
*/
|
|
1841
1845
|
reset(): void;
|
|
1846
|
+
/**
|
|
1847
|
+
* 停止任务
|
|
1848
|
+
*/
|
|
1849
|
+
stop(): void;
|
|
1842
1850
|
/**
|
|
1843
1851
|
* 获取任务性能报告
|
|
1844
1852
|
*/
|
|
@@ -1918,9 +1926,11 @@ declare namespace FW {
|
|
|
1918
1926
|
resolve: (value: T | PromiseLike<T>) => void,
|
|
1919
1927
|
reject: (reason?: any) => void,
|
|
1920
1928
|
signal: AbortSignal,
|
|
1929
|
+
reason?: any,
|
|
1921
1930
|
) => void;
|
|
1922
1931
|
|
|
1923
1932
|
type PromiseExecuteOptions = {
|
|
1933
|
+
reason?: any;
|
|
1924
1934
|
timeout?: number;
|
|
1925
1935
|
retryCount?: number;
|
|
1926
1936
|
retryInterval?: number;
|
package/Framework.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import 'reflect-metadata';
|
|
2
2
|
import { Container, interfaces } from 'inversify';
|
|
3
3
|
import { FWSystemDefine } from './define/FWSystemDefine';
|
|
4
|
+
import FWLog from './log/FWLog';
|
|
5
|
+
import { FWEventDefine } from './define/FWEventDefine';
|
|
4
6
|
|
|
5
7
|
class Framework {
|
|
6
8
|
private container: Container;
|
|
7
9
|
private registry: Map<string, new () => FW.Registry>;
|
|
10
|
+
private registeredComponents: Map<string, FW.RegisterFramework[]> = new Map();
|
|
8
11
|
private static instance: Framework;
|
|
9
12
|
private constructor() {}
|
|
10
13
|
|
|
@@ -14,6 +17,7 @@ class Framework {
|
|
|
14
17
|
autoBindInjectable: true,
|
|
15
18
|
});
|
|
16
19
|
this.registry = new Map<string, new () => FW.Registry>();
|
|
20
|
+
this.registeredComponents.clear();
|
|
17
21
|
}
|
|
18
22
|
|
|
19
23
|
public static getInstance() {
|
|
@@ -63,6 +67,7 @@ class Framework {
|
|
|
63
67
|
return this.getComponents(serviceIdentifier)[0];
|
|
64
68
|
}
|
|
65
69
|
}
|
|
70
|
+
|
|
66
71
|
/** 获取所有组件 */
|
|
67
72
|
public getComponents<T>(serviceIdentifier: FW.ServiceIdentifier<T>): T[] {
|
|
68
73
|
const binder = (this.container as any)._bindingDictionary;
|
|
@@ -112,7 +117,10 @@ class Framework {
|
|
|
112
117
|
* @param bundleName
|
|
113
118
|
*/
|
|
114
119
|
getRegistry(bundleName: string): FW.Registry {
|
|
115
|
-
|
|
120
|
+
if (this.registry.has(bundleName)) {
|
|
121
|
+
return this.container.get(this.registry.get(bundleName));
|
|
122
|
+
}
|
|
123
|
+
return undefined;
|
|
116
124
|
}
|
|
117
125
|
|
|
118
126
|
/**
|
|
@@ -145,7 +153,10 @@ class Framework {
|
|
|
145
153
|
this.container.bind(this.getKey(data.bundleName, index)).toService(cls);
|
|
146
154
|
}
|
|
147
155
|
});
|
|
156
|
+
|
|
157
|
+
this.recordRegistration(data);
|
|
148
158
|
}
|
|
159
|
+
|
|
149
160
|
/**
|
|
150
161
|
* 注销数据
|
|
151
162
|
* @param data
|
|
@@ -156,14 +167,95 @@ class Framework {
|
|
|
156
167
|
classes.forEach((cls, index) => {
|
|
157
168
|
const key = this.getKey(data.bundleName, index);
|
|
158
169
|
if (cls && this.container.isBound(cls)) {
|
|
159
|
-
this.container.get(key)['onDestroy']?.();
|
|
170
|
+
this.container.get(key)?.['onDestroy']?.();
|
|
160
171
|
this.container.unbind(cls);
|
|
161
172
|
}
|
|
162
173
|
if (this.container.isBound(key)) {
|
|
163
|
-
this.container.get(key)['onDestroy']?.();
|
|
174
|
+
this.container.get(key)?.['onDestroy']?.();
|
|
164
175
|
this.container.unbind(this.getKey(data.bundleName, index));
|
|
165
176
|
}
|
|
166
177
|
});
|
|
178
|
+
|
|
179
|
+
this.removeRegistration(data);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
private recordRegistration(data: FW.RegisterFramework) {
|
|
183
|
+
const bundleName = data.bundleName;
|
|
184
|
+
if (!this.registeredComponents.has(bundleName)) {
|
|
185
|
+
this.registeredComponents.set(bundleName, []);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const registrations = this.registeredComponents.get(bundleName)!;
|
|
189
|
+
const exists = registrations.some(
|
|
190
|
+
(reg) => reg.logic === data.logic && reg.data === data.data && reg.config === data.config,
|
|
191
|
+
);
|
|
192
|
+
|
|
193
|
+
if (!exists) {
|
|
194
|
+
registrations.push({ ...data });
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
private removeRegistration(data: FW.RegisterFramework) {
|
|
199
|
+
const bundleName = data.bundleName;
|
|
200
|
+
if (this.registeredComponents.has(bundleName)) {
|
|
201
|
+
const registrations = this.registeredComponents.get(bundleName)!;
|
|
202
|
+
const index = registrations.findIndex(
|
|
203
|
+
(reg) => reg.logic === data.logic && reg.data === data.data && reg.config === data.config,
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
if (index !== -1) {
|
|
207
|
+
registrations.splice(index, 1);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (registrations.length === 0) {
|
|
211
|
+
this.registeredComponents.delete(bundleName);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
public restart(): void {
|
|
217
|
+
this.destroyAllComponents();
|
|
218
|
+
this.container.unbindAll();
|
|
219
|
+
this.container = new Container({
|
|
220
|
+
defaultScope: 'Singleton',
|
|
221
|
+
autoBindInjectable: true,
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
this.reregisterAllComponents();
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private destroyAllComponents(): void {
|
|
228
|
+
const binder = (this.container as any)._bindingDictionary;
|
|
229
|
+
const map = binder._map;
|
|
230
|
+
|
|
231
|
+
for (const [key, bindings] of map.entries()) {
|
|
232
|
+
for (const binding of bindings) {
|
|
233
|
+
try {
|
|
234
|
+
if (this.container.isBound(binding.serviceIdentifier)) {
|
|
235
|
+
const instance = this.container.get(binding.serviceIdentifier);
|
|
236
|
+
instance?.['onDestroy']?.();
|
|
237
|
+
}
|
|
238
|
+
} catch (e) {
|
|
239
|
+
FWLog.warn(`Error destroying component ${key}:`, e);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
private reregisterAllComponents(): void {
|
|
246
|
+
for (const [bundleName, registrations] of this.registeredComponents.entries()) {
|
|
247
|
+
for (const data of registrations) {
|
|
248
|
+
try {
|
|
249
|
+
this.register(data);
|
|
250
|
+
} catch (e) {
|
|
251
|
+
console.error(`Error re-registering component for bundle ${bundleName}:`, e);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
public getRegisteredComponents(): Map<string, FW.RegisterFramework[]> {
|
|
258
|
+
return new Map(this.registeredComponents);
|
|
167
259
|
}
|
|
168
260
|
|
|
169
261
|
private getKey(bundleName: string, tag: FWSystemDefine.FWBindTag) {
|
package/define/FWEventDefine.ts
CHANGED
package/entry/FWEntry.ts
CHANGED
|
@@ -108,9 +108,9 @@ export default class FWEntry implements FW.Entry {
|
|
|
108
108
|
* */
|
|
109
109
|
initialize() {
|
|
110
110
|
this.map = new Map<string, FW.RegisterBundle>();
|
|
111
|
+
this.evtMgr = new FWEventManager();
|
|
111
112
|
this.engineMgr = new FWEngineManager();
|
|
112
113
|
this.taskMgr = new FWTaskManager();
|
|
113
|
-
this.evtMgr = new FWEventManager();
|
|
114
114
|
this.resMgr = new FWResManager();
|
|
115
115
|
this.layerMgr = new FWLayerManager();
|
|
116
116
|
this.timeMgr = new FWTimeManager();
|
|
@@ -140,6 +140,10 @@ export default class FWEntry implements FW.Entry {
|
|
|
140
140
|
@FWSocketAutoProcessPause()
|
|
141
141
|
launchScene(name: string): void {
|
|
142
142
|
FW.Entry.layerMgr.clear();
|
|
143
|
+
if (this.bundleAutoRelease(this.bundleName)) {
|
|
144
|
+
this.releaseBundle(this.bundleName);
|
|
145
|
+
}
|
|
146
|
+
|
|
143
147
|
if (!this.hasSceneName(name)) {
|
|
144
148
|
try {
|
|
145
149
|
cc.director.loadScene(name);
|
|
@@ -148,9 +152,6 @@ export default class FWEntry implements FW.Entry {
|
|
|
148
152
|
FWLog.error('launchScene failed:', name);
|
|
149
153
|
}
|
|
150
154
|
}
|
|
151
|
-
if (this.bundleAutoRelease(this.bundleName)) {
|
|
152
|
-
this.releaseBundle(this.bundleName);
|
|
153
|
-
}
|
|
154
155
|
this.bundleName = name;
|
|
155
156
|
cc.director.loadScene(this.getSceneName());
|
|
156
157
|
}
|
|
@@ -207,18 +208,8 @@ export default class FWEntry implements FW.Entry {
|
|
|
207
208
|
getComponents: <T>(serviceIdentifier?: FW.ServiceIdentifier<T>) => T[] = (serviceIdentifier) =>
|
|
208
209
|
Framework.getComponents(serviceIdentifier);
|
|
209
210
|
|
|
210
|
-
|
|
211
|
-
this.
|
|
212
|
-
this.layerMgr.onDestroy();
|
|
213
|
-
this.timeMgr.onDestroy();
|
|
214
|
-
this.animationMgr.onDestroy();
|
|
215
|
-
this.stateMgr.onDestroy();
|
|
216
|
-
|
|
217
|
-
this.resMgr = null;
|
|
218
|
-
this.layerMgr = null;
|
|
219
|
-
this.timeMgr = null;
|
|
220
|
-
this.animationMgr = null;
|
|
221
|
-
this.stateMgr = null;
|
|
211
|
+
restart() {
|
|
212
|
+
this.engineMgr?.restart();
|
|
222
213
|
}
|
|
223
214
|
|
|
224
215
|
update(dt: number) {
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { FWEventDefine } from '../define/FWEventDefine';
|
|
1
2
|
import { FWSystemDefine } from '../define/FWSystemDefine';
|
|
3
|
+
import Framework from '../Framework';
|
|
2
4
|
import FWLog from '../log/FWLog';
|
|
3
5
|
import { FWManager } from './FWManager';
|
|
4
6
|
|
|
@@ -14,6 +16,12 @@ export default class FWEngineManager extends FWManager implements FW.EngineManag
|
|
|
14
16
|
cc.game.off(cc.game.EVENT_HIDE, this.onAppHide, this);
|
|
15
17
|
}
|
|
16
18
|
|
|
19
|
+
restart() {
|
|
20
|
+
FW.Entry.evtMgr.dispatch(FWEventDefine.SystemEvent.SYSTEM_RESTART);
|
|
21
|
+
Framework.restart();
|
|
22
|
+
cc.game.restart();
|
|
23
|
+
}
|
|
24
|
+
|
|
17
25
|
getMemory() {
|
|
18
26
|
if (!this.debug) return;
|
|
19
27
|
if (window.performance && window.performance['memory']) {
|
|
@@ -168,8 +168,8 @@ export class FWLayerManager extends FWManager implements FW.LayerManager {
|
|
|
168
168
|
this.dataManager.notifyExternalRefUpdates(ctr.layerData);
|
|
169
169
|
}
|
|
170
170
|
|
|
171
|
-
this.dataManager.removeFromMap(layerData
|
|
172
|
-
this.dataManager.removeFromRegistry(ctr.layerData
|
|
171
|
+
this.dataManager.removeFromMap(layerData?.controllerConstructor);
|
|
172
|
+
this.dataManager.removeFromRegistry(ctr.layerData?.controllerConstructor);
|
|
173
173
|
|
|
174
174
|
const index = this.stackManager.getStack().findIndex((v) => {
|
|
175
175
|
v.controller == ctr;
|
|
@@ -207,11 +207,11 @@ export class FWLayerManager extends FWManager implements FW.LayerManager {
|
|
|
207
207
|
}
|
|
208
208
|
|
|
209
209
|
public clear() {
|
|
210
|
+
this.dataManager.getLayerMap().forEach((v) => this.close(v.controller));
|
|
210
211
|
this.queueManager.clear();
|
|
211
212
|
this.stackManager.clear();
|
|
212
213
|
this.stateManager.clear();
|
|
213
214
|
this.dataManager.clear();
|
|
214
|
-
this.dataManager.getLayerMap().forEach((v) => this.close(v.controller));
|
|
215
215
|
}
|
|
216
216
|
|
|
217
217
|
public onDestroy(): void {
|
package/manager/FWManager.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { FWEventDefine } from '../define/FWEventDefine';
|
|
1
2
|
import FWLog from '../log/FWLog';
|
|
2
3
|
|
|
3
4
|
export abstract class FWManager implements FW.Manager {
|
|
@@ -9,6 +10,12 @@ export abstract class FWManager implements FW.Manager {
|
|
|
9
10
|
|
|
10
11
|
constructor() {
|
|
11
12
|
this.initialize();
|
|
13
|
+
this.entry.evtMgr.register(FWEventDefine.SystemEvent.SYSTEM_RESTART, this.onRestart, this);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
protected onRestart() {
|
|
17
|
+
this.onDestroy();
|
|
18
|
+
this.initialize();
|
|
12
19
|
}
|
|
13
20
|
|
|
14
21
|
protected async invoke<T>(operation: Promise<T>, operationName: string = 'unknown'): Promise<T> {
|
|
@@ -20,18 +20,13 @@ export class FWPerformanceManager extends FWManager implements FW.PerformanceMan
|
|
|
20
20
|
|
|
21
21
|
private performanceOptions: FW.PerformanceManagerOptions;
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
super();
|
|
25
|
-
|
|
23
|
+
public async initialize(): Promise<void> {
|
|
26
24
|
this.performanceOptions = {
|
|
27
25
|
autoCollect: true,
|
|
28
26
|
dataRetentionTime: 5 * 60 * 1000,
|
|
29
27
|
samplingRate: 1.0,
|
|
30
28
|
warningThreshold: 1000,
|
|
31
29
|
};
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
public async initialize(): Promise<void> {
|
|
35
30
|
if (this.performanceOptions.autoCollect) {
|
|
36
31
|
this.startAutoCleanup();
|
|
37
32
|
}
|
|
@@ -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(
|
|
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
|
-
|
|
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
|
-
|
|
49
|
+
timerSchedule?.unSchedule();
|
|
53
50
|
}
|
|
54
51
|
}, options.timeout);
|
|
55
52
|
}
|
|
56
53
|
|
|
57
54
|
const onAbort = () => {
|
|
58
|
-
|
|
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
|
-
|
|
73
|
+
timerSchedule?.unSchedule();
|
|
77
74
|
resolve(value);
|
|
78
75
|
};
|
|
79
76
|
|
|
80
77
|
const wrappedReject = (reason?: any) => {
|
|
81
|
-
|
|
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
|
-
|
|
151
|
-
|
|
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
|
-
|
|
167
|
+
timerSchedule?.unSchedule();
|
|
169
168
|
}
|
|
170
169
|
}, options.timeout);
|
|
171
170
|
}
|
|
172
171
|
|
|
173
172
|
const onAbort = () => {
|
|
174
|
-
|
|
173
|
+
timerSchedule?.unSchedule();
|
|
175
174
|
if (promiseProxy.status === FWSystemDefine.FWPromiseStatus.PENDING) {
|
|
176
175
|
promiseProxy.status = FWSystemDefine.FWPromiseStatus.CANCELLED;
|
|
177
176
|
this.removePromise(id);
|
|
@@ -230,11 +229,11 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
230
229
|
promiseProxy.status = FWSystemDefine.FWPromiseStatus.FULFILLED;
|
|
231
230
|
abortController.signal.removeEventListener('abort', onAbort);
|
|
232
231
|
this.removePromise(id);
|
|
233
|
-
|
|
232
|
+
timerSchedule?.unSchedule();
|
|
234
233
|
resolve(result);
|
|
235
234
|
})
|
|
236
235
|
.catch((error) => {
|
|
237
|
-
|
|
236
|
+
timerSchedule?.unSchedule();
|
|
238
237
|
if (
|
|
239
238
|
retryCount < maxRetryTimes &&
|
|
240
239
|
(!options.retryCondition || options.retryCondition(error, retryCount))
|
|
@@ -312,11 +311,10 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
312
311
|
public cancelAll(reason?: any): number[] {
|
|
313
312
|
const cancelled: number[] = [];
|
|
314
313
|
this.promiseRegistry.forEach((promiseProxy, id) => {
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
cancelled.push(id);
|
|
318
|
-
}
|
|
314
|
+
promiseProxy.abort(reason);
|
|
315
|
+
cancelled.push(id);
|
|
319
316
|
});
|
|
317
|
+
FWLog.error(cancelled);
|
|
320
318
|
return cancelled;
|
|
321
319
|
}
|
|
322
320
|
|
package/manager/FWTaskManager.ts
CHANGED
|
@@ -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
|
*/
|
package/package.json
CHANGED
package/service/FWService.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
+
import { FWEventDefine } from '../define/FWEventDefine';
|
|
1
2
|
import FWLog from '../log/FWLog';
|
|
2
3
|
|
|
3
4
|
export default abstract class FWService {
|
|
4
5
|
constructor() {
|
|
5
6
|
this.initialize();
|
|
7
|
+
this.entry.evtMgr.register(FWEventDefine.SystemEvent.SYSTEM_RESTART, this.onDestroy, this);
|
|
6
8
|
}
|
|
9
|
+
public entry: FW.Entry = FW.Entry;
|
|
7
10
|
public abstract initialize(): void;
|
|
8
|
-
public abstract
|
|
11
|
+
public abstract onDestroy(): void;
|
|
9
12
|
public abstract readonly serviceName: string;
|
|
10
13
|
|
|
11
14
|
protected async invoke<T>(operation: Promise<T>, operationName: string = 'unknown'): Promise<T> {
|
package/service/http/FWHttp.ts
CHANGED
|
@@ -3,9 +3,9 @@ import FWLog from '../../log/FWLog';
|
|
|
3
3
|
import FWService from '../FWService';
|
|
4
4
|
|
|
5
5
|
export default class FWHttp extends FWService {
|
|
6
|
-
public serviceName: string = '
|
|
6
|
+
public serviceName: string = 'http';
|
|
7
7
|
public initialize(): void {}
|
|
8
|
-
public
|
|
8
|
+
public onDestroy(): void {}
|
|
9
9
|
|
|
10
10
|
protected async getMessage(
|
|
11
11
|
url: string,
|
|
@@ -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
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
+
retryCount: 3,
|
|
64
|
+
retryCondition(error, retryCount) {
|
|
65
|
+
return xhr.readyState != 4 || (xhr.status !== 200 && retryCount < 3);
|
|
66
66
|
},
|
|
67
|
-
|
|
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,14 @@
|
|
|
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 {
|
|
11
|
+
public serviceName: string = 'socket';
|
|
10
12
|
/** 发送心跳时间抽 */
|
|
11
13
|
private sendHeartTimestamp: number;
|
|
12
14
|
/** 接收心跳时间戳 */
|
|
@@ -54,10 +56,6 @@ export default class FWSocket implements FW.Socket {
|
|
|
54
56
|
*/
|
|
55
57
|
protected messageEvents: { [key: string]: (msg: any) => void };
|
|
56
58
|
|
|
57
|
-
constructor() {
|
|
58
|
-
this.initialize();
|
|
59
|
-
}
|
|
60
|
-
|
|
61
59
|
public initialize(): void {
|
|
62
60
|
this.messageEvents = cc.js.createMap();
|
|
63
61
|
this.protocolContainer = new Map<Symbol, FW.ProtocolPolling>();
|
|
@@ -102,8 +100,6 @@ export default class FWSocket implements FW.Socket {
|
|
|
102
100
|
}
|
|
103
101
|
}
|
|
104
102
|
|
|
105
|
-
|
|
106
|
-
|
|
107
103
|
this.socket.onopen = this.onSocketOpen.bind(this);
|
|
108
104
|
this.socket.onclose = this.onSocketClose.bind(this);
|
|
109
105
|
this.socket.onerror = this.onSocketError.bind(this);
|
|
@@ -147,6 +143,7 @@ export default class FWSocket implements FW.Socket {
|
|
|
147
143
|
|
|
148
144
|
public onDestroy(): void {
|
|
149
145
|
this.messageEvents = cc.js.createMap();
|
|
146
|
+
this.socket.close();
|
|
150
147
|
this.socket = null;
|
|
151
148
|
}
|
|
152
149
|
|
|
@@ -310,9 +307,9 @@ export default class FWSocket implements FW.Socket {
|
|
|
310
307
|
/** 消息处理 */
|
|
311
308
|
private async onSocketMessage(originalMsg: any) {
|
|
312
309
|
const msg = await this.handle.onBeforeReceivingMessage(originalMsg);
|
|
313
|
-
|
|
314
|
-
if(msg.error){
|
|
315
|
-
this.onSocketError(msg)
|
|
310
|
+
|
|
311
|
+
if (msg.error) {
|
|
312
|
+
this.onSocketError(msg);
|
|
316
313
|
return;
|
|
317
314
|
}
|
|
318
315
|
|
|
@@ -320,7 +317,7 @@ export default class FWSocket implements FW.Socket {
|
|
|
320
317
|
this.receiveTimeStamp = new Date().getTime();
|
|
321
318
|
return;
|
|
322
319
|
}
|
|
323
|
-
|
|
320
|
+
|
|
324
321
|
this.remoteProtocol(msg);
|
|
325
322
|
|
|
326
323
|
if (this.isPausedMessageHandle) return;
|