keytops-game-framework 1.0.24 → 1.0.26

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.
@@ -43,10 +43,11 @@ type HttpSendParams = {
43
43
  jsonReviver?: JsonReviver | JsonReviver[];
44
44
  };
45
45
  /**
46
- * Http节点,实现接口简化,request复用池
46
+ * Http节点,实现接口简化。
47
+ * Cocos Creator Android Native 的 XMLHttpRequest 复用存在旧回调串包风险,
48
+ * 因此这里每次请求和每次 retry 都创建新的 HttpRequest。
47
49
  */
48
50
  export declare class HttpNode {
49
- private _pool;
50
51
  /**
51
52
  * 发送 Get 请求。
52
53
  * @param url 请求的地址。大多数浏览器实施了一个同源安全策略,并且要求这个 URL 与包含脚本的文本具有相同的主机名和端口。
@@ -36,6 +36,10 @@ export declare class HttpRequest extends EventDispatcher implements IRecoverable
36
36
  /**@private */
37
37
  protected _url: string;
38
38
  protected _jsonReviver: JsonReviver | JsonReviver[];
39
+ /**@private */
40
+ protected _requestId: number;
41
+ /**@private */
42
+ protected _activeRequestId: number;
39
43
  /**
40
44
  * 发送 HTTP 请求。
41
45
  * @param url 请求的地址。大多数浏览器实施了一个同源安全策略,并且要求这个 URL 与包含脚本的文本具有相同的主机名和端口。
@@ -46,6 +50,7 @@ export declare class HttpRequest extends EventDispatcher implements IRecoverable
46
50
  * @param jsonRever (default = null) 请求结果JSON转化特殊处理,参考Json.xxxReviver
47
51
  */
48
52
  send(url: string, data?: any, method?: string, responseType?: "text" | "json" | "xml" | "arraybuffer", headers?: any[] | null, jsonRever?: JsonReviver | JsonReviver[]): void;
53
+ protected isCurrentRequest(requestId: number, http: XMLHttpRequest): boolean;
49
54
  /**
50
55
  * @private
51
56
  * 请求进度的侦听处理函数。
@@ -82,7 +87,7 @@ export declare class HttpRequest extends EventDispatcher implements IRecoverable
82
87
  * 请求成功完成的处理函数。
83
88
  */
84
89
  protected complete(): void;
85
- protected parseJson(): boolean;
90
+ protected parseJson(responseText?: string, response?: any): boolean;
86
91
  reset(): void;
87
92
  destroy(): void;
88
93
  /**
package/dist/index.js CHANGED
@@ -4125,6 +4125,10 @@ class HttpRequest extends EventDispatcher {
4125
4125
  super(...arguments);
4126
4126
  /**@private */
4127
4127
  this._http = new XMLHttpRequest();
4128
+ /**@private */
4129
+ this._requestId = 0;
4130
+ /**@private */
4131
+ this._activeRequestId = 0;
4128
4132
  }
4129
4133
  /**
4130
4134
  * 发送 HTTP 请求。
@@ -4136,6 +4140,12 @@ class HttpRequest extends EventDispatcher {
4136
4140
  * @param jsonRever (default = null) 请求结果JSON转化特殊处理,参考Json.xxxReviver
4137
4141
  */
4138
4142
  send(url, data = null, method = "get", responseType = "text", headers = null, jsonRever = null) {
4143
+ const timeout = this._http ? this._http.timeout : 0;
4144
+ this.clear();
4145
+ const requestId = ++this._requestId;
4146
+ this._activeRequestId = requestId;
4147
+ this._http = new XMLHttpRequest();
4148
+ this._http.timeout = timeout;
4139
4149
  this._responseType = responseType;
4140
4150
  this._data = null;
4141
4151
  this._jsonReviver = jsonRever;
@@ -4177,23 +4187,31 @@ class HttpRequest extends EventDispatcher {
4177
4187
  http.dataType = restype;
4178
4188
  }
4179
4189
  http.onerror = function (e) {
4180
- _this._onError(e);
4190
+ if (_this.isCurrentRequest(requestId, http))
4191
+ _this._onError(e);
4181
4192
  };
4182
4193
  http.onabort = function (e) {
4183
- _this._onAbort(e);
4194
+ if (_this.isCurrentRequest(requestId, http))
4195
+ _this._onAbort(e);
4184
4196
  };
4185
4197
  http.onprogress = function (e) {
4186
- _this._onProgress(e);
4198
+ if (_this.isCurrentRequest(requestId, http))
4199
+ _this._onProgress(e);
4187
4200
  };
4188
4201
  http.onload = function (e) {
4189
- _this._onLoad(e);
4202
+ if (_this.isCurrentRequest(requestId, http))
4203
+ _this._onLoad(e);
4190
4204
  };
4191
4205
  http.ontimeout = function (e) {
4192
- _this._onTimeout(e);
4206
+ if (_this.isCurrentRequest(requestId, http))
4207
+ _this._onTimeout(e);
4193
4208
  };
4194
4209
  // if(Browser.onBLMiniGame&&Browser.onAndroid&&!data)data={};
4195
4210
  http.send(isJson ? JSON.stringify(data) : data);
4196
4211
  }
4212
+ isCurrentRequest(requestId, http) {
4213
+ return this._activeRequestId === requestId && this._http === http;
4214
+ }
4197
4215
  /**
4198
4216
  * @private
4199
4217
  * 请求进度的侦听处理函数。
@@ -4240,11 +4258,13 @@ class HttpRequest extends EventDispatcher {
4240
4258
  * @param message 错误信息。
4241
4259
  */
4242
4260
  error(message) {
4261
+ this._activeRequestId = 0;
4243
4262
  this.clear();
4244
4263
  console.error(`[网络请求错误] 地址:${this.url} 错误: ${message}`);
4245
4264
  this.event(HttpRequest.Event.ERROR, message);
4246
4265
  }
4247
4266
  _onTimeout(e) {
4267
+ this._activeRequestId = 0;
4248
4268
  this.clear();
4249
4269
  console.error(`[网络请求错误] 地址:${this.url} 超时`);
4250
4270
  this.event(HttpRequest.Event.TIMEOUT);
@@ -4254,14 +4274,20 @@ class HttpRequest extends EventDispatcher {
4254
4274
  * 请求成功完成的处理函数。
4255
4275
  */
4256
4276
  complete() {
4277
+ var http = this._http;
4278
+ var responseText = http.responseText;
4279
+ var response = http.response;
4280
+ var flag = this.parseJson(responseText, response);
4281
+ var data = this._data;
4282
+ this._activeRequestId = 0;
4257
4283
  this.clear();
4258
- var flag = this.parseJson();
4259
- flag && this.event(HttpRequest.Event.COMPLETE, this._data instanceof Array ? [this._data] : this._data);
4284
+ this._data = data;
4285
+ flag && this.event(HttpRequest.Event.COMPLETE, data instanceof Array ? [data] : data);
4260
4286
  }
4261
- parseJson() {
4287
+ parseJson(responseText = this._http.responseText, response = this._http.response) {
4262
4288
  try {
4263
4289
  if (this._responseType === "json") {
4264
- this._data = JSON.parse(this._http.responseText, (key, value) => {
4290
+ this._data = JSON.parse(responseText, (key, value) => {
4265
4291
  if (this._jsonReviver == null) {
4266
4292
  return value;
4267
4293
  }
@@ -4278,7 +4304,7 @@ class HttpRequest extends EventDispatcher {
4278
4304
  });
4279
4305
  }
4280
4306
  else {
4281
- this._data = this._http.response || this._http.responseText;
4307
+ this._data = response || responseText;
4282
4308
  }
4283
4309
  return true;
4284
4310
  }
@@ -4288,11 +4314,14 @@ class HttpRequest extends EventDispatcher {
4288
4314
  }
4289
4315
  }
4290
4316
  reset() {
4291
- this.offAllEvent();
4317
+ this.offAll();
4318
+ this._activeRequestId = 0;
4292
4319
  this.clear();
4320
+ this._http = new XMLHttpRequest();
4293
4321
  }
4294
4322
  destroy() {
4295
- this.offAllEvent();
4323
+ this.offAll();
4324
+ this._activeRequestId = 0;
4296
4325
  this.clear();
4297
4326
  }
4298
4327
  /**
@@ -4330,12 +4359,11 @@ HttpRequest.Event = Event$3;
4330
4359
  HttpRequest._urlEncode = encodeURI;
4331
4360
 
4332
4361
  /**
4333
- * Http节点,实现接口简化,request复用池
4362
+ * Http节点,实现接口简化。
4363
+ * Cocos Creator Android Native 的 XMLHttpRequest 复用存在旧回调串包风险,
4364
+ * 因此这里每次请求和每次 retry 都创建新的 HttpRequest。
4334
4365
  */
4335
4366
  class HttpNode {
4336
- constructor() {
4337
- this._pool = new Pool(HttpRequest);
4338
- }
4339
4367
  /**
4340
4368
  * 发送 Get 请求。
4341
4369
  * @param url 请求的地址。大多数浏览器实施了一个同源安全策略,并且要求这个 URL 与包含脚本的文本具有相同的主机名和端口。
@@ -4376,35 +4404,52 @@ class HttpNode {
4376
4404
  * @param onError (default = null) 请求发生错误时的响应Handler
4377
4405
  */
4378
4406
  send(url, { data = null, method = "get", responseType = "text", headers = null, timeout = 10000, retryTimes = 0, onComplete = null, onProgress = null, onError = null, jsonReviver = null, } = {}) {
4379
- let sender = this._pool.allocate();
4380
- sender.timeout = timeout;
4381
- sender.once(HttpRequest.Event.COMPLETE, this, (e) => {
4382
- this._pool.recover(sender);
4383
- if (onComplete) {
4384
- onComplete.runWith(e);
4385
- onComplete.clear();
4386
- }
4387
- });
4388
- let _onError = (e) => {
4389
- if (retryTimes-- > 0) {
4390
- sender.send(url, data, method, responseType, headers);
4391
- return;
4392
- }
4393
- this._pool.recover(sender);
4394
- if (onError) {
4395
- onError.runWith(e);
4396
- onError.clear();
4407
+ let remainingRetries = retryTimes;
4408
+ const progressCaller = onProgress && onProgress.caller;
4409
+ const progressMethod = onProgress && onProgress.method;
4410
+ const progressArgs = onProgress && onProgress.args;
4411
+ onProgress && onProgress.clear();
4412
+ const sendAttempt = () => {
4413
+ const sender = new HttpRequest();
4414
+ sender.timeout = timeout;
4415
+ sender.once(HttpRequest.Event.COMPLETE, this, (e) => {
4416
+ try {
4417
+ if (onComplete) {
4418
+ onComplete.runWith(e);
4419
+ }
4420
+ }
4421
+ finally {
4422
+ onComplete && onComplete.clear();
4423
+ sender.destroy();
4424
+ }
4425
+ });
4426
+ let _onError = (e) => {
4427
+ try {
4428
+ sender.destroy();
4429
+ if (remainingRetries-- > 0) {
4430
+ sendAttempt();
4431
+ return;
4432
+ }
4433
+ if (onError) {
4434
+ onError.runWith(e);
4435
+ }
4436
+ }
4437
+ finally {
4438
+ if (remainingRetries < 0) {
4439
+ onError && onError.clear();
4440
+ }
4441
+ }
4442
+ };
4443
+ sender.once(HttpRequest.Event.ERROR, this, _onError);
4444
+ sender.once(HttpRequest.Event.TIMEOUT, this, (e) => {
4445
+ _onError(HttpRequest.Event.TIMEOUT);
4446
+ });
4447
+ if (progressMethod) {
4448
+ sender.on(HttpRequest.Event.PROGRESS, progressCaller, progressMethod, progressArgs);
4397
4449
  }
4450
+ sender.send(url, data, method, responseType, headers, jsonReviver);
4398
4451
  };
4399
- sender.on(HttpRequest.Event.ERROR, this, _onError);
4400
- sender.on(HttpRequest.Event.TIMEOUT, this, (e) => {
4401
- _onError(HttpRequest.Event.TIMEOUT);
4402
- });
4403
- if (onProgress) {
4404
- sender.on(HttpRequest.Event.PROGRESS, onProgress.caller, onProgress.method, onProgress.args);
4405
- onProgress.clear();
4406
- }
4407
- sender.send(url, data, method, responseType, headers, jsonReviver);
4452
+ sendAttempt();
4408
4453
  }
4409
4454
  /**
4410
4455
  * 发送 Post 请求。
@@ -7598,11 +7643,33 @@ let View = View_1 = class View extends cc.Component {
7598
7643
  addView(view, index) {
7599
7644
  view.setControl(this._control);
7600
7645
  view.node.parent = this.node;
7601
- (view.node.getComponent(cc.UITransform) || view.node.addComponent(cc.UITransform)).priority = index;
7646
+ (view.node.getComponent(cc.UITransform) || view.node.addComponent(cc.UITransform)).priority = index !== null && index !== void 0 ? index : 0;
7647
+ this._sortChildrenByViewPriority();
7602
7648
  viewManager.event("open", view);
7603
7649
  viewManager.reportViewAnalytics("open", view);
7604
7650
  analytics.session.updateSessionDuration();
7605
7651
  }
7652
+ /**
7653
+ * 兼容 UITransform.priority 的旧层级语义,避免不同平台延迟排序导致同级 View 渲染顺序不稳定。
7654
+ */
7655
+ _sortChildrenByViewPriority() {
7656
+ const sortedChildren = this.node.children.map((child, originalSiblingIndex) => {
7657
+ var _a, _b;
7658
+ return {
7659
+ child,
7660
+ originalSiblingIndex,
7661
+ priority: (_b = (_a = child.getComponent(cc.UITransform)) === null || _a === void 0 ? void 0 : _a.priority) !== null && _b !== void 0 ? _b : 0,
7662
+ };
7663
+ }).sort((a, b) => {
7664
+ if (a.priority !== b.priority) {
7665
+ return a.priority - b.priority;
7666
+ }
7667
+ return a.originalSiblingIndex - b.originalSiblingIndex;
7668
+ });
7669
+ sortedChildren.forEach(({ child }, sortedIndex) => {
7670
+ child.setSiblingIndex(sortedIndex);
7671
+ });
7672
+ }
7606
7673
  /**
7607
7674
  * 移除子视图
7608
7675
  * @param view 子视图
@@ -62,6 +62,10 @@ export declare class View extends cc.Component implements IView {
62
62
  * @param index 视图索引
63
63
  */
64
64
  addView(view: View, index?: number): void;
65
+ /**
66
+ * 兼容 UITransform.priority 的旧层级语义,避免不同平台延迟排序导致同级 View 渲染顺序不稳定。
67
+ */
68
+ private _sortChildrenByViewPriority;
65
69
  /**
66
70
  * 移除子视图
67
71
  * @param view 子视图
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "keytops-game-framework",
3
- "version": "1.0.24",
3
+ "version": "1.0.26",
4
4
  "description": "cocos creator game framework library",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",