@ives_xxz/framework 1.4.6 → 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
  };
@@ -131,37 +131,43 @@ export class FWAssetManager extends FWManager implements FW.AssetManager {
131
131
 
132
132
  assetData.loaded = false;
133
133
 
134
- const proxy = FW.Entry.promiseMgr.execute((resolve, reject) => {
135
- const self = this;
136
- bundle.load(
137
- path,
138
- type,
139
- (finish: number, total: number, item: cc.AssetManager.RequestItem) => {
140
- progress?.(finish, total, item);
141
- },
142
- (err: Error, asset: cc.Asset) => {
143
- if (err || !asset) {
144
- reject(err);
145
- return;
146
- }
134
+ return FW.Entry.promiseMgr.execute(
135
+ (resolve, reject, signal) => {
136
+ const self = this;
137
+ bundle.load(
138
+ path,
139
+ type,
140
+ (finish: number, total: number, item: cc.AssetManager.RequestItem) => {
141
+ progress?.(finish, total, item);
142
+ },
143
+ (err: Error, asset: cc.Asset) => {
144
+ if (err || !asset) {
145
+ reject(err);
146
+ return;
147
+ }
147
148
 
148
- assetData.loaded = true;
149
- assetData.dependentBundle = bundleName;
150
- assetData.uuid = asset['_uuid'];
151
- assetData.autoRelease = autoRelease;
152
- assetData.asset = asset;
153
- assetData.user = assetProperty.user;
154
- assetData.refCount = 1;
155
- assetData.assetProperty = assetProperty;
156
-
157
- self.assetsMap.set(key, assetData);
158
-
159
- cb?.(assetData);
160
- resolve(assetData);
161
- },
162
- );
163
- });
164
- return proxy.promise;
149
+ assetData.loaded = true;
150
+ assetData.dependentBundle = bundleName;
151
+ assetData.uuid = asset['_uuid'];
152
+ assetData.autoRelease = autoRelease;
153
+ assetData.asset = asset;
154
+ assetData.user = assetProperty.user;
155
+ assetData.refCount = 1;
156
+ assetData.assetProperty = assetProperty;
157
+
158
+ self.assetsMap.set(key, assetData);
159
+
160
+ cb?.(assetData);
161
+ resolve(assetData);
162
+ },
163
+ );
164
+ },
165
+ {
166
+ retryCount: 3,
167
+ retryInterval: 1,
168
+ timeout: 10,
169
+ },
170
+ ).promise;
165
171
  }
166
172
  /**
167
173
  * 加载文件夹
@@ -173,7 +179,6 @@ export class FWAssetManager extends FWManager implements FW.AssetManager {
173
179
  FWLog.error(`加载资源失败,请检查参数列表:${assetProperty}!`);
174
180
  return undefined;
175
181
  }
176
-
177
182
  const bundleName = assetProperty.bundle || FW.Entry.bundleName;
178
183
  const type = assetProperty.type;
179
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.6",
3
+ "version": "1.4.8",
4
4
  "description": "cocoscreator 2.x mvc framework",
5
5
  "main": "index.js",
6
6
  "keywords": ["123456"],