@ives_xxz/framework 1.5.7 → 1.5.9
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/manager/FWLayerManager.ts +27 -19
- package/manager/FWPromiseManager.ts +70 -38
- package/package.json +1 -1
|
@@ -9,6 +9,7 @@ import FWLogic from "../logic/FWLogic";
|
|
|
9
9
|
import FWData from "../data/FWData";
|
|
10
10
|
import FWSocketSender from "../service/socket/FWSocketSender";
|
|
11
11
|
import FWSocketHandle from "../service/socket/FWSocketHandle";
|
|
12
|
+
import Framework from "../Framework";
|
|
12
13
|
|
|
13
14
|
const ADD_EXTERNAL_REFERENCE: string = "addExternalReference";
|
|
14
15
|
|
|
@@ -296,18 +297,39 @@ class FWLayerDataManager {
|
|
|
296
297
|
}
|
|
297
298
|
}
|
|
298
299
|
|
|
300
|
+
private findBundleNameByClassName(name: string): string | undefined {
|
|
301
|
+
const registeredComponents = Framework.getRegisteredComponents();
|
|
302
|
+
|
|
303
|
+
const bundleNames = Array.from(registeredComponents.keys());
|
|
304
|
+
|
|
305
|
+
for (const bundleName of bundleNames) {
|
|
306
|
+
if (this.doesClassNameContainBundleName(name, bundleName)) {
|
|
307
|
+
return bundleName;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return undefined;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
private doesClassNameContainBundleName(
|
|
314
|
+
className: string,
|
|
315
|
+
bundleName: string
|
|
316
|
+
): boolean {
|
|
317
|
+
const bundleNameLower = bundleName.toLowerCase();
|
|
318
|
+
const classNameLower = className.toLowerCase();
|
|
319
|
+
return classNameLower.includes(bundleNameLower);
|
|
320
|
+
}
|
|
321
|
+
|
|
299
322
|
/**
|
|
300
323
|
* 生成数据
|
|
301
324
|
*/
|
|
302
325
|
private generationData(ctr: FW.LayerController): FWLayerData {
|
|
303
326
|
const layerData = new FWLayerData();
|
|
304
327
|
const layerType = ctr.layerType;
|
|
328
|
+
const constructorName = ctr.constructor.name;
|
|
329
|
+
const bundleName = this.findBundleNameByClassName(constructorName);
|
|
330
|
+
const configTag = FWSystemDefine.FWBindTag.CONFIG;
|
|
305
331
|
|
|
306
|
-
ctr.config = FW.Entry.getComponent(
|
|
307
|
-
ctr.logic = FW.Entry.getComponent(FWLogic);
|
|
308
|
-
ctr.data = FW.Entry.getComponent(FWData);
|
|
309
|
-
ctr.sender = FW.Entry.getComponent(FWSocketSender);
|
|
310
|
-
ctr.handle = FW.Entry.getComponent(FWSocketHandle);
|
|
332
|
+
ctr.config = FW.Entry.getComponent(`${bundleName}${configTag}`);
|
|
311
333
|
|
|
312
334
|
layerData.layerRenderOrder = ctr.renderOrder;
|
|
313
335
|
layerData.layerAssetProperty = ctr.layerAssetProperty;
|
|
@@ -561,20 +583,6 @@ class FWLayerOpenManager {
|
|
|
561
583
|
return true;
|
|
562
584
|
}
|
|
563
585
|
|
|
564
|
-
/**
|
|
565
|
-
* 获取undefined结果
|
|
566
|
-
*/
|
|
567
|
-
private getUndefinedResult<Ctr>(isAsync: boolean): Promise<Ctr> | Ctr {
|
|
568
|
-
return isAsync ? Promise.resolve(undefined) : undefined;
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
/**
|
|
572
|
-
* 包装结果
|
|
573
|
-
*/
|
|
574
|
-
private wrapResult<Ctr>(result: Ctr, isAsync: boolean): Promise<Ctr> | Ctr {
|
|
575
|
-
return isAsync ? Promise.resolve(result) : result;
|
|
576
|
-
}
|
|
577
|
-
|
|
578
586
|
/**
|
|
579
587
|
* 异步生成Layer
|
|
580
588
|
*/
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import { FWSystemDefine } from
|
|
2
|
-
import FWLog from
|
|
3
|
-
import { FWManager } from
|
|
4
|
-
|
|
5
|
-
export default class FWPromiseManager
|
|
1
|
+
import { FWSystemDefine } from "../define/FWSystemDefine";
|
|
2
|
+
import FWLog from "../log/FWLog";
|
|
3
|
+
import { FWManager } from "./FWManager";
|
|
4
|
+
|
|
5
|
+
export default class FWPromiseManager
|
|
6
|
+
extends FWManager
|
|
7
|
+
implements FW.PromiseManager
|
|
8
|
+
{
|
|
6
9
|
private promiseRegistry: Map<number, FW.PromiseProxy>;
|
|
7
10
|
private uniqueId: number = 0;
|
|
8
11
|
|
|
@@ -18,7 +21,7 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
18
21
|
/** 创建Promise执行器 */
|
|
19
22
|
public execute<T = any>(
|
|
20
23
|
executor: FW.PromiseExcutor<T>,
|
|
21
|
-
options: FW.PromiseExecuteOptions = {}
|
|
24
|
+
options: FW.PromiseExecuteOptions = {}
|
|
22
25
|
): FW.PromiseProxy<T> {
|
|
23
26
|
const id = this.uniqueId++;
|
|
24
27
|
const abortController = new AbortController();
|
|
@@ -26,17 +29,23 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
26
29
|
const retryInterval = options.retryInterval || 0;
|
|
27
30
|
let retryCount = 0;
|
|
28
31
|
let timerSchedule: FW.TimerSchedule;
|
|
32
|
+
|
|
29
33
|
const createPromise = (): Promise<T> => {
|
|
30
34
|
return new Promise<T>((resolve, reject) => {
|
|
31
35
|
if (options.timeout && options.timeout > 0) {
|
|
32
36
|
timerSchedule = FW.Entry.timeMgr.scheduleOnce(() => {
|
|
33
|
-
const timeoutError = new Error(
|
|
37
|
+
const timeoutError = new Error(
|
|
38
|
+
`Promise ${id} timeout after ${options.timeout} s`
|
|
39
|
+
);
|
|
34
40
|
if (
|
|
35
41
|
retryCount < maxRetryTimes &&
|
|
36
|
-
(!options.retryCondition ||
|
|
42
|
+
(!options.retryCondition ||
|
|
43
|
+
options.retryCondition(timeoutError, retryCount))
|
|
37
44
|
) {
|
|
38
45
|
retryCount++;
|
|
39
|
-
FWLog.debug(
|
|
46
|
+
FWLog.debug(
|
|
47
|
+
`Promise ${id} timeout, retrying (${retryCount}/${maxRetryTimes})`
|
|
48
|
+
);
|
|
40
49
|
if (retryInterval > 0) {
|
|
41
50
|
FW.Entry.timeMgr.scheduleOnce(() => {
|
|
42
51
|
createPromise().then(resolve, reject);
|
|
@@ -64,11 +73,11 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
64
73
|
return;
|
|
65
74
|
}
|
|
66
75
|
|
|
67
|
-
abortController.signal.addEventListener(
|
|
76
|
+
abortController.signal.addEventListener("abort", onAbort);
|
|
68
77
|
|
|
69
78
|
const wrappedResolve = (value: T | PromiseLike<T>) => {
|
|
70
79
|
promiseProxy.status = FWSystemDefine.FWPromiseStatus.FULFILLED;
|
|
71
|
-
abortController.signal.removeEventListener(
|
|
80
|
+
abortController.signal.removeEventListener("abort", onAbort);
|
|
72
81
|
this.removePromise(id);
|
|
73
82
|
timerSchedule?.unSchedule();
|
|
74
83
|
resolve(value);
|
|
@@ -78,10 +87,14 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
78
87
|
timerSchedule?.unSchedule();
|
|
79
88
|
if (
|
|
80
89
|
retryCount < maxRetryTimes &&
|
|
81
|
-
(!options.retryCondition ||
|
|
90
|
+
(!options.retryCondition ||
|
|
91
|
+
options.retryCondition(reason, retryCount))
|
|
82
92
|
) {
|
|
83
93
|
retryCount++;
|
|
84
|
-
FWLog.debug(
|
|
94
|
+
FWLog.debug(
|
|
95
|
+
`Promise ${id} failed, retrying (${retryCount}/${maxRetryTimes}):`,
|
|
96
|
+
reason
|
|
97
|
+
);
|
|
85
98
|
if (retryInterval > 0) {
|
|
86
99
|
FW.Entry.timeMgr.scheduleOnce(() => {
|
|
87
100
|
createPromise().then(resolve, reject);
|
|
@@ -90,16 +103,23 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
90
103
|
createPromise().then(resolve, reject);
|
|
91
104
|
}
|
|
92
105
|
} else {
|
|
93
|
-
if (
|
|
106
|
+
if (
|
|
107
|
+
promiseProxy.status === FWSystemDefine.FWPromiseStatus.PENDING
|
|
108
|
+
) {
|
|
94
109
|
promiseProxy.status = FWSystemDefine.FWPromiseStatus.REJECTED;
|
|
95
|
-
abortController.signal.removeEventListener(
|
|
110
|
+
abortController.signal.removeEventListener("abort", onAbort);
|
|
96
111
|
this.removePromise(id);
|
|
97
112
|
reject(reason);
|
|
98
113
|
}
|
|
99
114
|
}
|
|
100
115
|
};
|
|
101
116
|
try {
|
|
102
|
-
executor(
|
|
117
|
+
executor(
|
|
118
|
+
wrappedResolve,
|
|
119
|
+
wrappedReject,
|
|
120
|
+
abortController.signal,
|
|
121
|
+
options.reason
|
|
122
|
+
);
|
|
103
123
|
} catch (error) {
|
|
104
124
|
wrappedReject(error);
|
|
105
125
|
}
|
|
@@ -114,16 +134,16 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
114
134
|
abortController,
|
|
115
135
|
abort: (reason?: any) => {
|
|
116
136
|
if (promiseProxy.status === FWSystemDefine.FWPromiseStatus.PENDING) {
|
|
117
|
-
FWLog.debug(reason ||
|
|
137
|
+
FWLog.debug(reason || "promise cancelled");
|
|
118
138
|
abortController.abort(reason);
|
|
119
139
|
}
|
|
120
140
|
},
|
|
121
141
|
|
|
122
142
|
addAbortEventListener: (
|
|
123
143
|
listener: (this: AbortSignal, ev: Event) => any,
|
|
124
|
-
options?: boolean | AddEventListenerOptions
|
|
144
|
+
options?: boolean | AddEventListenerOptions
|
|
125
145
|
) => {
|
|
126
|
-
abortController.signal.addEventListener(
|
|
146
|
+
abortController.signal.addEventListener("abort", listener, options);
|
|
127
147
|
},
|
|
128
148
|
};
|
|
129
149
|
|
|
@@ -134,7 +154,7 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
134
154
|
/** 批量执行Promise并等待所有完成 */
|
|
135
155
|
public all<T = any>(
|
|
136
156
|
promises: FW.PromiseProxy<T>[],
|
|
137
|
-
options: FW.PromiseExecuteOptions = {}
|
|
157
|
+
options: FW.PromiseExecuteOptions = {}
|
|
138
158
|
): FW.PromiseProxy<FW.PromiseResult<T>> {
|
|
139
159
|
const id = this.uniqueId++;
|
|
140
160
|
const abortController = new AbortController();
|
|
@@ -148,13 +168,18 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
148
168
|
if (options.timeout && options.timeout > 0) {
|
|
149
169
|
timerSchedule?.unSchedule();
|
|
150
170
|
timerSchedule = FW.Entry.timeMgr.scheduleOnce(() => {
|
|
151
|
-
const timeoutError = new Error(
|
|
171
|
+
const timeoutError = new Error(
|
|
172
|
+
`All Promise ${id} timeout after ${options.timeout} s`
|
|
173
|
+
);
|
|
152
174
|
if (
|
|
153
175
|
retryCount < maxRetryTimes &&
|
|
154
|
-
(!options.retryCondition ||
|
|
176
|
+
(!options.retryCondition ||
|
|
177
|
+
options.retryCondition(timeoutError, retryCount))
|
|
155
178
|
) {
|
|
156
179
|
retryCount++;
|
|
157
|
-
FWLog.debug(
|
|
180
|
+
FWLog.debug(
|
|
181
|
+
`All Promise ${id} timeout, retrying (${retryCount}/${maxRetryTimes})`
|
|
182
|
+
);
|
|
158
183
|
if (retryInterval > 0) {
|
|
159
184
|
FW.Entry.timeMgr.scheduleOnce(() => {
|
|
160
185
|
createPromise().then(resolve, reject);
|
|
@@ -182,7 +207,7 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
182
207
|
return;
|
|
183
208
|
}
|
|
184
209
|
|
|
185
|
-
abortController.signal.addEventListener(
|
|
210
|
+
abortController.signal.addEventListener("abort", onAbort);
|
|
186
211
|
|
|
187
212
|
const processAll = async () => {
|
|
188
213
|
const result: FW.PromiseResult<T> = {
|
|
@@ -199,8 +224,8 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
199
224
|
.filter(
|
|
200
225
|
(id) =>
|
|
201
226
|
!result.success.some((s) => s.id === id) &&
|
|
202
|
-
!result.failed.some((f) => f.id === id)
|
|
203
|
-
)
|
|
227
|
+
!result.failed.some((f) => f.id === id)
|
|
228
|
+
)
|
|
204
229
|
);
|
|
205
230
|
break;
|
|
206
231
|
}
|
|
@@ -209,7 +234,9 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
209
234
|
const value = await promiseProxy.promise;
|
|
210
235
|
result.success.push(value);
|
|
211
236
|
} catch (error) {
|
|
212
|
-
if (
|
|
237
|
+
if (
|
|
238
|
+
promiseProxy.status === FWSystemDefine.FWPromiseStatus.CANCELLED
|
|
239
|
+
) {
|
|
213
240
|
result.cancelled.push(promiseProxy.id);
|
|
214
241
|
} else {
|
|
215
242
|
result.failed.push({
|
|
@@ -226,7 +253,7 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
226
253
|
processAll()
|
|
227
254
|
.then((result) => {
|
|
228
255
|
promiseProxy.status = FWSystemDefine.FWPromiseStatus.FULFILLED;
|
|
229
|
-
abortController.signal.removeEventListener(
|
|
256
|
+
abortController.signal.removeEventListener("abort", onAbort);
|
|
230
257
|
this.removePromise(id);
|
|
231
258
|
timerSchedule?.unSchedule();
|
|
232
259
|
resolve(result);
|
|
@@ -235,12 +262,13 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
235
262
|
timerSchedule?.unSchedule();
|
|
236
263
|
if (
|
|
237
264
|
retryCount < maxRetryTimes &&
|
|
238
|
-
(!options.retryCondition ||
|
|
265
|
+
(!options.retryCondition ||
|
|
266
|
+
options.retryCondition(error, retryCount))
|
|
239
267
|
) {
|
|
240
268
|
retryCount++;
|
|
241
269
|
FWLog.debug(
|
|
242
270
|
`All Promise ${id} failed, retrying (${retryCount}/${maxRetryTimes}):`,
|
|
243
|
-
error
|
|
271
|
+
error
|
|
244
272
|
);
|
|
245
273
|
if (retryInterval > 0) {
|
|
246
274
|
FW.Entry.timeMgr.scheduleOnce(() => {
|
|
@@ -250,9 +278,11 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
250
278
|
createPromise().then(resolve, reject);
|
|
251
279
|
}
|
|
252
280
|
} else {
|
|
253
|
-
if (
|
|
281
|
+
if (
|
|
282
|
+
promiseProxy.status === FWSystemDefine.FWPromiseStatus.PENDING
|
|
283
|
+
) {
|
|
254
284
|
promiseProxy.status = FWSystemDefine.FWPromiseStatus.REJECTED;
|
|
255
|
-
abortController.signal.removeEventListener(
|
|
285
|
+
abortController.signal.removeEventListener("abort", onAbort);
|
|
256
286
|
this.removePromise(id);
|
|
257
287
|
reject(error);
|
|
258
288
|
}
|
|
@@ -269,15 +299,15 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
269
299
|
abortController,
|
|
270
300
|
abort: (reason?: any) => {
|
|
271
301
|
if (promiseProxy.status === FWSystemDefine.FWPromiseStatus.PENDING) {
|
|
272
|
-
FWLog.debug(reason ||
|
|
302
|
+
FWLog.debug(reason || "all promise cancelled");
|
|
273
303
|
abortController.abort(reason);
|
|
274
304
|
}
|
|
275
305
|
},
|
|
276
306
|
addAbortEventListener: (
|
|
277
307
|
listener: (this: AbortSignal, ev: Event) => any,
|
|
278
|
-
options?: boolean | AddEventListenerOptions
|
|
308
|
+
options?: boolean | AddEventListenerOptions
|
|
279
309
|
) => {
|
|
280
|
-
abortController.signal.addEventListener(
|
|
310
|
+
abortController.signal.addEventListener("abort", listener, options);
|
|
281
311
|
},
|
|
282
312
|
};
|
|
283
313
|
|
|
@@ -288,7 +318,10 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
288
318
|
/** 取消指定Promise */
|
|
289
319
|
public cancel(id: number, reason?: any): boolean {
|
|
290
320
|
const promiseProxy = this.promiseRegistry.get(id);
|
|
291
|
-
if (
|
|
321
|
+
if (
|
|
322
|
+
promiseProxy &&
|
|
323
|
+
promiseProxy.status === FWSystemDefine.FWPromiseStatus.PENDING
|
|
324
|
+
) {
|
|
292
325
|
promiseProxy.abort(reason);
|
|
293
326
|
return true;
|
|
294
327
|
}
|
|
@@ -313,7 +346,6 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
313
346
|
promiseProxy.abort(reason);
|
|
314
347
|
cancelled.push(id);
|
|
315
348
|
});
|
|
316
|
-
FWLog.error(cancelled);
|
|
317
349
|
return cancelled;
|
|
318
350
|
}
|
|
319
351
|
|
|
@@ -340,7 +372,7 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
|
|
|
340
372
|
/** 获取正在执行的Promise数量 */
|
|
341
373
|
public getActiveCount(): number {
|
|
342
374
|
return Array.from(this.promiseRegistry.values()).filter(
|
|
343
|
-
(p) => p.status === FWSystemDefine.FWPromiseStatus.PENDING
|
|
375
|
+
(p) => p.status === FWSystemDefine.FWPromiseStatus.PENDING
|
|
344
376
|
).length;
|
|
345
377
|
}
|
|
346
378
|
|