Tencent / Tencent/libpag

鸿蒙端监听使用弱引用,监听不确定时机会被回收

Open
#2,706 1 comment 0 reactions 1 assignee View on GitHub

@kevingpqi123 is already working on this.

Since Mar 4, 2025.

Dominant language
HTML
Stars
5.8k
Forks
531
Avg merge
8d 55m
Merged PRs (30d)
17

Description

【版本信息】

"@tencent/libpag": "^4.4.25"

【平台信息】

鸿蒙原生

【预期的表现】

PAGViewController 中添加监听可以正常准确的保证回调

【实际的情况】

PAGViewController中的注册监听均使用WeakRef弱引用形式:
addListener(u20: WeakRef): void;
removeListener(t20: WeakRef): void;
在实际操作中监听会被在任意情况下被回收,导致监听不准确,影响正常业务逻辑处理,请帮忙解答,或者帮忙出个dome给下使用规范

【Demo及附件】

在aboutToAppear中初始化PAG相关,在页面中点击触发this.mPAGController.play(),不确定时机listener会被回收。

  aboutToAppear(): void {
    let manager = getContext(this).resourceManager;
    let file = PAGFile.LoadFromAssets(manager, "pag/*****.pag");
    this.mPAGController.setComposition(file);
    let listener: PAGViewListener = {
      onAnimationEnd: () => {
        HCLog.e("PAG", "onAnimationEnd")
        // 业务代码
      }
    }
    HCLog.e("PAG", "addListener")
    this.weakRef = new WeakRef(listener)
    this.mPAGController.addListener(this.weakRef)

    // 创建FinalizationRegistry
    const registry: FinalizationRegistry<PAGViewListener> = new FinalizationRegistry((heldValue) => {
      HCLog.e("PAG", `对象已被回收,关联值: ${heldValue}`);
    });
    registry.register(listener, {}, listener)
  }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.