@ives_xxz/framework 1.4.7 → 1.4.8

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
@@ -50,7 +50,7 @@ declare namespace FW {
50
50
  };
51
51
 
52
52
  type PromiseManager = {
53
- public execute<T = any>(
53
+ execute<T = any>(
54
54
  executor: (
55
55
  resolve: (value: T | PromiseLike<T>) => void,
56
56
  reject: (reason?: any) => void,
@@ -58,6 +58,10 @@ declare namespace FW {
58
58
  ) => void = PromiseExcutor,
59
59
  options: PromiseExecuteOptions = {},
60
60
  ): PromiseProxy<T>;
61
+ all<T = any>(
62
+ promises: FW.PromiseProxy<T>[],
63
+ options: FW.PromiseExecuteOptions = {},
64
+ ): FW.PromiseProxy<FW.PromiseResult<T>>;
61
65
  };
62
66
 
63
67
  type Registry = {
@@ -1950,7 +1954,7 @@ declare namespace FW {
1950
1954
  type Promise = (resolve: (value: any) => void, reject: (reason?: any) => void) => void;
1951
1955
 
1952
1956
  type PromiseResult<T = any> = {
1953
- success: T[];
1957
+ success: PromiseProxy<T>[];
1954
1958
  failed: { id: number; reason: any }[];
1955
1959
  cancelled: number[];
1956
1960
  };
@@ -179,7 +179,6 @@ export class FWAssetManager extends FWManager implements FW.AssetManager {
179
179
  FWLog.error(`加载资源失败,请检查参数列表:${assetProperty}!`);
180
180
  return undefined;
181
181
  }
182
-
183
182
  const bundleName = assetProperty.bundle || FW.Entry.bundleName;
184
183
  const type = assetProperty.type;
185
184
  const path = assetProperty.path;
@@ -27,11 +27,6 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
27
27
  let retryCount = 0;
28
28
  const maxRetryTimes = options.retryCount || 0;
29
29
  const retryInterval = options.retryInterval || 0;
30
- const promiseProxy: FW.PromiseProxy<T> = {
31
- id,
32
- status: FWSystemDefine.FWPromiseStatus.PENDING,
33
- abortController,
34
- };
35
30
 
36
31
  const createPromise = (): Promise<T> => {
37
32
  return new Promise<T>((resolve, reject) => {
@@ -114,19 +109,180 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
114
109
  });
115
110
  };
116
111
 
117
- promiseProxy.promise = createPromise();
118
- promiseProxy.abort = (reason?: any) => {
119
- if (promiseProxy.status === FWSystemDefine.FWPromiseStatus.PENDING) {
120
- FWLog.debug(reason || 'promise cancelled');
121
- abortController.abort(reason);
122
- }
112
+ const promise = createPromise();
113
+ const promiseProxy: FW.PromiseProxy<T> = {
114
+ id,
115
+ promise,
116
+ status: FWSystemDefine.FWPromiseStatus.PENDING,
117
+ abortController,
118
+ abort: (reason?: any) => {
119
+ if (promiseProxy.status === FWSystemDefine.FWPromiseStatus.PENDING) {
120
+ FWLog.debug(reason || 'promise cancelled');
121
+ abortController.abort(reason);
122
+ }
123
+ },
124
+ addAbortEventListener: (
125
+ listener: (this: AbortSignal, ev: Event) => any,
126
+ options?: boolean | AddEventListenerOptions,
127
+ ) => {
128
+ abortController.signal.addEventListener('abort', listener, options);
129
+ },
123
130
  };
124
- promiseProxy.addAbortEventListener = (
125
- listener: (this: AbortSignal, ev: Event) => any,
126
- options?: boolean | AddEventListenerOptions,
127
- ) => {
128
- abortController.signal.addEventListener('abort', listener, options);
131
+
132
+ this.promiseRegistry.set(id, promiseProxy);
133
+ return promiseProxy;
134
+ }
135
+
136
+ /** 批量执行Promise并等待所有完成 */
137
+ public all<T = any>(
138
+ promises: FW.PromiseProxy<T>[],
139
+ options: FW.PromiseExecuteOptions = {},
140
+ ): FW.PromiseProxy<FW.PromiseResult<T>> {
141
+ const id = this.uniqueId++;
142
+ const abortController = new AbortController();
143
+ let retryCount = 0;
144
+ const maxRetryTimes = options.retryCount || 0;
145
+ const retryInterval = options.retryInterval || 0;
146
+
147
+ const createPromise = (): Promise<FW.PromiseResult<T>> => {
148
+ return new Promise<FW.PromiseResult<T>>((resolve, reject) => {
149
+ if (options.timeout && options.timeout > 0) {
150
+ this.timerSchedule?.unSchedule();
151
+ this.timerSchedule = FW.Entry.timeMgr.scheduleOnce(() => {
152
+ const timeoutError = new Error(`All Promise ${id} timeout after ${options.timeout} s`);
153
+ if (
154
+ retryCount < maxRetryTimes &&
155
+ (!options.retryCondition || options.retryCondition(timeoutError, retryCount))
156
+ ) {
157
+ retryCount++;
158
+ FWLog.debug(`All Promise ${id} timeout, retrying (${retryCount}/${maxRetryTimes})`);
159
+ if (retryInterval > 0) {
160
+ FW.Entry.timeMgr.scheduleOnce(() => {
161
+ createPromise().then(resolve, reject);
162
+ }, retryInterval);
163
+ } else {
164
+ createPromise().then(resolve, reject);
165
+ }
166
+ } else {
167
+ abortController.abort(timeoutError.message);
168
+ this.timerSchedule?.unSchedule();
169
+ }
170
+ }, options.timeout);
171
+ }
172
+
173
+ const onAbort = () => {
174
+ this.timerSchedule?.unSchedule();
175
+ if (promiseProxy.status === FWSystemDefine.FWPromiseStatus.PENDING) {
176
+ promiseProxy.status = FWSystemDefine.FWPromiseStatus.CANCELLED;
177
+ this.removePromise(id);
178
+ }
179
+ };
180
+
181
+ if (abortController.signal.aborted) {
182
+ onAbort();
183
+ return;
184
+ }
185
+
186
+ abortController.signal.addEventListener('abort', onAbort);
187
+
188
+ const processAll = async () => {
189
+ const result: FW.PromiseResult<T> = {
190
+ success: [],
191
+ failed: [],
192
+ cancelled: [],
193
+ };
194
+
195
+ for (const promiseProxy of promises) {
196
+ // 检查是否已取消
197
+ if (abortController.signal.aborted) {
198
+ result.cancelled.push(
199
+ ...promises
200
+ .map((p) => p.id)
201
+ .filter(
202
+ (id) =>
203
+ !result.success.some((s) => s.id === id) &&
204
+ !result.failed.some((f) => f.id === id),
205
+ ),
206
+ );
207
+ break;
208
+ }
209
+
210
+ try {
211
+ const value = await promiseProxy.promise;
212
+ result.success.push(value);
213
+ } catch (error) {
214
+ if (promiseProxy.status === FWSystemDefine.FWPromiseStatus.CANCELLED) {
215
+ result.cancelled.push(promiseProxy.id);
216
+ } else {
217
+ result.failed.push({
218
+ id: promiseProxy.id,
219
+ reason: error,
220
+ });
221
+ }
222
+ }
223
+ }
224
+
225
+ return result;
226
+ };
227
+
228
+ processAll()
229
+ .then((result) => {
230
+ promiseProxy.status = FWSystemDefine.FWPromiseStatus.FULFILLED;
231
+ abortController.signal.removeEventListener('abort', onAbort);
232
+ this.removePromise(id);
233
+ this.timerSchedule?.unSchedule();
234
+ resolve(result);
235
+ })
236
+ .catch((error) => {
237
+ this.timerSchedule?.unSchedule();
238
+ if (
239
+ retryCount < maxRetryTimes &&
240
+ (!options.retryCondition || options.retryCondition(error, retryCount))
241
+ ) {
242
+ retryCount++;
243
+ FWLog.debug(
244
+ `All Promise ${id} failed, retrying (${retryCount}/${maxRetryTimes}):`,
245
+ error,
246
+ );
247
+ if (retryInterval > 0) {
248
+ FW.Entry.timeMgr.scheduleOnce(() => {
249
+ createPromise().then(resolve, reject);
250
+ }, retryInterval);
251
+ } else {
252
+ createPromise().then(resolve, reject);
253
+ }
254
+ } else {
255
+ if (promiseProxy.status === FWSystemDefine.FWPromiseStatus.PENDING) {
256
+ promiseProxy.status = FWSystemDefine.FWPromiseStatus.REJECTED;
257
+ abortController.signal.removeEventListener('abort', onAbort);
258
+ this.removePromise(id);
259
+ reject(error);
260
+ }
261
+ }
262
+ });
263
+ });
264
+ };
265
+
266
+ const promise = createPromise();
267
+ const promiseProxy: FW.PromiseProxy<FW.PromiseResult<T>> = {
268
+ id,
269
+ promise,
270
+ status: FWSystemDefine.FWPromiseStatus.PENDING,
271
+ abortController,
272
+ abort: (reason?: any) => {
273
+ if (promiseProxy.status === FWSystemDefine.FWPromiseStatus.PENDING) {
274
+ FWLog.debug(reason || 'all promise cancelled');
275
+ abortController.abort(reason);
276
+ }
277
+ },
278
+ addAbortEventListener: (
279
+ listener: (this: AbortSignal, ev: Event) => any,
280
+ options?: boolean | AddEventListenerOptions,
281
+ ) => {
282
+ abortController.signal.addEventListener('abort', listener, options);
283
+ },
129
284
  };
285
+
130
286
  this.promiseRegistry.set(id, promiseProxy);
131
287
  return promiseProxy;
132
288
  }
@@ -164,36 +320,6 @@ export default class FWPromiseManager extends FWManager implements FW.PromiseMan
164
320
  return cancelled;
165
321
  }
166
322
 
167
- /** 批量执行Promise并等待所有完成 */
168
- public async all<T = any>(
169
- promises: FW.PromiseProxy<T>[],
170
- options: FW.PromiseExecuteOptions = {},
171
- ): Promise<FW.PromiseResult<T>> {
172
- const result: FW.PromiseResult<T> = {
173
- success: [],
174
- failed: [],
175
- cancelled: [],
176
- };
177
-
178
- for (const promiseProxy of promises) {
179
- try {
180
- const value = await promiseProxy.promise;
181
- result.success.push(value);
182
- } catch (error) {
183
- if (promiseProxy.status === FWSystemDefine.FWPromiseStatus.CANCELLED) {
184
- result.cancelled.push(promiseProxy.id);
185
- } else {
186
- result.failed.push({
187
- id: promiseProxy.id,
188
- reason: error,
189
- });
190
- }
191
- }
192
- }
193
-
194
- return result;
195
- }
196
-
197
323
  /** 获取Promise状态 */
198
324
  public getStatus(id: number): FWSystemDefine.FWPromiseStatus | null {
199
325
  const promiseProxy = this.promiseRegistry.get(id);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ives_xxz/framework",
3
- "version": "1.4.7",
3
+ "version": "1.4.8",
4
4
  "description": "cocoscreator 2.x mvc framework",
5
5
  "main": "index.js",
6
6
  "keywords": ["123456"],