@xmov/avatar 2.4.0-beta.2 → 2.4.0-beta.4

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.
@@ -237,7 +237,9 @@ export default class ResourceManager {
237
237
  load(onDownloadProgress?: TDownloadProgress): Promise<ISessionResponse | null>;
238
238
  startSession(): Promise<any>;
239
239
  _startSession(): Promise<any> | null;
240
- stopSession(stop_reason: string): Promise<any>;
240
+ stopSession(stop_reason: string, options?: {
241
+ keepalive?: boolean;
242
+ }): Promise<any>;
241
243
  /**
242
244
  * 加载表情数据
243
245
  */
@@ -3,6 +3,7 @@ interface RequestOptions {
3
3
  method?: HttpMethod;
4
4
  data?: Record<string, any>;
5
5
  headers?: Record<string, string>;
6
+ keepalive?: boolean;
6
7
  }
7
8
  export default function request(url: string, options?: RequestOptions): Promise<Response>;
8
9
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xmov/avatar",
3
- "version": "2.4.0-beta.2",
3
+ "version": "2.4.0-beta.4",
4
4
  "description": "魔珐数字人 JS-SDK",
5
5
  "type": "module",
6
6
  "source": "src/index.ts",
package/src/index.ts CHANGED
@@ -43,6 +43,7 @@ export default class XmovAvatar {
43
43
 
44
44
  // 使用 ! 操作符告诉 TypeScript 这些属性会在构造函数中被初始化
45
45
  private resourceManager!: ResourceManager;
46
+ private beforeUnloadStopSessionPromise: ReturnType<ResourceManager["stopSession"]> | null = null;
46
47
  private renderScheduler!: RenderScheduler;
47
48
  private networkMonitor: NetworkMonitor | null = null;
48
49
  private ttsa: Ttsa | null = null;
@@ -642,6 +643,9 @@ export default class XmovAvatar {
642
643
  this.pendingInvisibleMode = true;
643
644
  }
644
645
 
646
+ // Session 请求和资源下载期间也需要处理页面刷新。
647
+ window.addEventListener('beforeunload', this.handleBeforeUnload);
648
+
645
649
  // 加载资源(sdk.resource_load:资源包下载/解析耗时)
646
650
  (window as any).performanceTracker.markStart(performanceConstant.load_resource);
647
651
  (window as any).performanceTracker.markStart(performanceConstant.first_avatar_render);
@@ -674,7 +678,6 @@ export default class XmovAvatar {
674
678
  // 设置上报使用ttsa
675
679
  (window as any).performanceTracker.setReportFunc(this.ttsa);
676
680
 
677
- window.addEventListener('beforeunload', this.handleBeforeUnload);
678
681
  document.addEventListener('visibilitychange', this.boundVisibilityChange);
679
682
  initSpan?.end();
680
683
  return sessionInfo;
@@ -1315,7 +1318,11 @@ export default class XmovAvatar {
1315
1318
  }
1316
1319
 
1317
1320
  private handleBeforeUnload = () => {
1318
- this.destroy("beforeunload");
1321
+ if (!this._isClientMode && (this.resourceManager?.session_id || this.resourceManager?.session_request_id)) {
1322
+ // Page dismissal cannot wait for E2E and TTSA shutdown before starting DELETE.
1323
+ this.beforeUnloadStopSessionPromise ??= this.resourceManager.stopSession("beforeunload", { keepalive: true });
1324
+ }
1325
+ void this.destroy("beforeunload");
1319
1326
  };
1320
1327
 
1321
1328
  public isDestroyed() {
@@ -1459,7 +1466,7 @@ export default class XmovAvatar {
1459
1466
  this.ttsa?.sendSdkPoint('close_session', {
1460
1467
  reason: stop_reason,
1461
1468
  });
1462
- const res = await this.resourceManager.stopSession(stop_reason);
1469
+ const res = await (this.beforeUnloadStopSessionPromise ?? this.resourceManager.stopSession(stop_reason));
1463
1470
  await this.destroyClient();
1464
1471
  if(res) {
1465
1472
  this.onStatusChange(AvatarStatus.close);
@@ -1483,7 +1490,7 @@ export default class XmovAvatar {
1483
1490
  } finally {
1484
1491
  try {
1485
1492
  if (!this._isClientMode) {
1486
- await this.resourceManager.stopSession(stopReason);
1493
+ await (this.beforeUnloadStopSessionPromise ?? this.resourceManager.stopSession(stopReason));
1487
1494
  }
1488
1495
  } finally {
1489
1496
  await this.destroyClient();
@@ -406,7 +406,7 @@ export default class ResourceManager {
406
406
  });
407
407
  }
408
408
 
409
- async stopSession(stop_reason: string) {
409
+ async stopSession(stop_reason: string, options: { keepalive?: boolean } = {}) {
410
410
  try {
411
411
  const url = `${this.options.gatewayServer}`;
412
412
  // session_id 尚未返回时(如 init 未完成就 destroy),只要有 request_id 仍发请求,
@@ -424,7 +424,7 @@ export default class ResourceManager {
424
424
  data.request_id = this.session_request_id;
425
425
  }
426
426
  const { headers, data: signedData } = headersNeedSign(this.options.appId, this.options.appSecret, "DELETE", url, data);
427
- const response = await request(url, { method: "DELETE", data: signedData, headers: { ...headers, ...this.options.headers } });
427
+ const response = await request(url, { method: "DELETE", data: signedData, headers: { ...headers, ...this.options.headers }, keepalive: options.keepalive });
428
428
  return await response.json();
429
429
  } catch (error) {
430
430
  this.sdk.onMessage({
@@ -4,6 +4,7 @@ interface RequestOptions {
4
4
  method?: HttpMethod
5
5
  data?: Record<string, any>
6
6
  headers?: Record<string, string>
7
+ keepalive?: boolean
7
8
  }
8
9
 
9
10
  interface ErrorResponse {
@@ -21,6 +22,7 @@ export default async function request(
21
22
 
22
23
  const config: RequestInit = {
23
24
  method,
25
+ keepalive: options.keepalive,
24
26
  // headers,
25
27
  headers: {
26
28
  'Content-Type': 'application/json',
@@ -104,4 +106,4 @@ export function XMLRequest(options: {
104
106
  };
105
107
  xhr.send();
106
108
  });
107
- }
109
+ }