Skip to content

fix(h5): 支持音频事件无参取消全部监听 - #19591

Merged
Single-Dancer merged 1 commit into
NervJS:mainfrom
Jarvis636431:fix/h5-audio-off-listeners
Nov 2, 2026
Merged

Single-Dancer merged 1 commit into
NervJS:mainfrom
Jarvis636431:fix/h5-audio-off-listeners

Conversation

@Jarvis636431

@Jarvis636431 Jarvis636431 commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

这个 PR 做了什么?

H5 InnerAudioContext 的 offXxx 把省略的 callback 替换为新的空函数,导致已注册的回调无法移除。十类音频事件均可复现,offError/offStop 还会绕过 CallbackManager 的无参清空路径。

为原生 Audio 事件登记用户回调:不传 callback 时移除该事件的全部用户监听,传 callback 时保留指定移除行为。DOM 事件继续使用原生 EventTarget,保留事件参数、this、重复注册去重及分发期间移除的行为。offError/offStop 直接把可选参数交给原有 CallbackManager。

首次播放处理单独注册,不受 offPlay() 清空用户监听影响,startTime 仍在第一次 play 时应用。destroy 同时解绑登记的 DOM 回调和内部 play 监听,清理新增的登记引用。

Closes #19590

这个 PR 是什么类型?

  • 错误修复 (Bugfix)

这个 PR 涉及以下平台:

  • Web 端(H5)

接口语义依据

快手官方 InnerAudioContext 文档与腾讯云音频官方文档明确规定,省略 callback 移除该事件的全部监听;抖音 offError 文档也明确区分有参和无参行为。Taro 现有类型允许 callback 省略。

验证

  • 新增 35 个回归用例,基线实现 24 个失败、11 个通过;修复后全部通过。
  • 覆盖全部十个 offXxx:无参/undefined、重复清空、指定/未知回调、清空后重新订阅;补充事件与实例隔离、首次播放 startTime、DOM 回调 this/参数/去重、分发期间取消,以及 destroy 后保留原 Audio 引用时不再触发旧监听。
  • 经公开 createInnerAudioContext 创建实例,测试使用真实 JSDOM HTMLAudioElement/EventTarget;仅替换 pause,未 mock add/removeEventListener 或 CallbackManager。既有销毁测试的 Audio 桩补充 removeEventListener 方法。
  • 音频测试:2 个套件、36 个用例通过。
  • pnpm --filter @tarojs/taro-h5 run test:ci --watchman=false:30 个套件、278 个用例通过,退出码 0。
  • pnpm --filter @tarojs/taro-h5 run build:通过,退出码 0;存在未修改的 previewImage/Swiper 类型及 Sass 弃用告警。
  • 全工作区 pnpm build:通过,退出码 0;存在未修改文件的类型、组件及循环依赖等构建告警。
  • 修改源码和测试 ESLint(--no-ignore)、git diff --check 和提交钩子通过。

Node.js 22.18.0 / pnpm 10.0.0。分支独立基于上游 main 0493b5251da,不包含 Vue v-model 等其他修复。未修改依赖或锁文件,构建生成的无关 definition 变更已清理。

验证使用受控原生事件,没有真实音频播放、浏览器或真机验证。不改变共享 CallbackManager 的分发语义,也不宣称完整解决历史 #14447 的全部场景。

Summary by CodeRabbit

  • Bug 修复
    • 音频事件现在支持移除指定回调,或一次性移除该事件的全部回调;销毁音频上下文时会清理已注册的监听,减少残留回调触发。
    • 首次播放时的 startTime 定位不再依赖用户注册的播放监听,添加或移除用户监听不会影响该定位行为。

@coderabbitai

coderabbitai Bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f235a5cd-22de-4337-af05-75d74ec4136c
Commits

Reviewing files that changed from the base of the PR and between 3c398a6 and b9e0a46.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 62259a85-1391-4aca-bc2d-fc1784d9a3a2
Commits

Reviewing files that changed from the base of the PR and between 0493b52 and 3c398a6.

Files selected for processing: 3
  • packages/taro-h5/__tests__/media/audio-listeners.test.ts
  • packages/taro-h5/__tests__/media/audio.test.ts
  • packages/taro-h5/src/api/media/audio/InnerAudioContext.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

Walkthrough

H5 InnerAudioContext 现在按事件类型管理用户监听器,支持移除指定回调或清除该事件的全部用户回调。首次播放的 startTime 处理使用独立内部监听器。销毁时会清理用户监听器和内部监听器。

Changes

音频事件监听器管理

层 / 文件 摘要
分离首次播放处理
packages/taro-h5/src/api/media/audio/InnerAudioContext.ts, packages/taro-h5/__tests__/media/audio-listeners.test.ts
首次播放时设置 startTime 的处理改用独立的内部 play 监听器。测试验证移除 play 用户监听器后,首次播放和 seek 仍按预期设置时间,并覆盖上下文隔离。
管理事件注册与移除
packages/taro-h5/src/api/media/audio/InnerAudioContext.ts, packages/taro-h5/__tests__/media/audio-listeners.test.ts, packages/taro-h5/__tests__/media/audio.test.ts
事件接口改用监听器管理方法。省略回调或传入 undefined 时移除该事件的全部用户监听器;传入回调时只移除匹配项。测试覆盖十种事件、重复移除、重新订阅及模拟对象的 removeEventListener。
销毁时清理监听器
packages/taro-h5/src/api/media/audio/InnerAudioContext.ts, packages/taro-h5/__tests__/media/audio-listeners.test.ts
销毁时移除已管理的用户监听器和内部首次播放监听器。测试覆盖 DOM 回调参数与 this、重复注册、派发期间移除、销毁后不再触发回调及重复销毁。

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

  • NervJS/taro#19586:该 PR 修改 CallbackManager.trigger 的派发期间移除行为;本 PR 的 onStop 和 onError 接口也使用该管理器。

Suggested reviewers: single-dancer

Merge Risk: Minimal · previous review 3c398

No actionable merge-blocking issue is established for the H5 audio listener changes. The change is ready for normal merge checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 标题“fix(h5): 支持音频事件无参取消全部监听”与变更内容一致。变更修改了 H5 端 InnerAudioContext 的监听器管理:省略 callback 时移除该事件的全部用户监听器,指定 callback 时只移除对应回调,并将首次播放的 startTime 内部监听器与用户 onPlay 监听器分离,销毁时同时解绑。标题准确概括了主要变更,且使用 fix(h5) 前缀说明作用范围。…
Linked Issues check Passed PR 满足直接关联 issue #19590 的编码要求。InnerAudioContext.ts 为十个 offXxx 实现了两种行为:省略或传入 undefined 时移除该事件的全部用户监听,传入回调时仅移除该回调。offError 和 offStop 将可选参数传给现有 CallbackManager。独立的 onFirstPlay 监听器保留了首次播放的 `s…
Out of Scope Changes check Passed 变更范围与 issue #19590 一致。新增的监听器登记、DOM 原生事件行为测试、分发期间移除测试和销毁清理测试,均直接支持音频事件取消监听或其生命周期要求。audio.test.ts 仅补充销毁测试所需的 removeEventListener 桩。未发现与该修复无关的功能、依赖或公共接口变更。
Full details: Title check

Explanation

标题“fix(h5): 支持音频事件无参取消全部监听”与变更内容一致。变更修改了 H5 端 InnerAudioContext 的监听器管理:省略 callback 时移除该事件的全部用户监听器,指定 callback 时只移除对应回调,并将首次播放的 startTime 内部监听器与用户 onPlay 监听器分离,销毁时同时解绑。标题准确概括了主要变更,且使用 fix(h5) 前缀说明作用范围。标题为中文,与仓库的提交规范一致。

Full details: Linked Issues check

Explanation

PR 满足直接关联 issue #19590 的编码要求。InnerAudioContext.ts 为十个 offXxx 实现了两种行为:省略或传入 undefined 时移除该事件的全部用户监听,传入回调时仅移除该回调。offError 和 offStop 将可选参数传给现有 CallbackManager。独立的 onFirstPlay 监听器保留了首次播放的 startTime 处理,destroy 同时移除用户监听器和内部监听器。新增测试覆盖十个事件、两种参数形式、重复移除、指定回调、隔离性、startTime 和销毁清理。

Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 1 functions across 3 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

小兔听见音符跳,
play 监听各归其位。
移除回调清清楚,
销毁之后静悄悄。
耳边只剩月光摇。

@coderabbitai
coderabbitai Bot requested a review from Single-Dancer November 1, 2026 11:57
@codecov

codecov Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 57.83%. Comparing base (e1bf0c1) to head (b9e0a46).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #19591      +/-   ##
==========================================
+ Coverage   57.75%   57.83%   +0.08%     
==========================================
  Files         470      470              
  Lines       25966    25987      +21     
  Branches     6511     6528      +17     
==========================================
+ Hits        14996    15029      +33     
- Misses       9038     9164     +126     
+ Partials     1932     1794     -138     
Flag Coverage Δ
taro-cli 59.69% <ø> (ø)
taro-runtime 66.00% <ø> (ø)
taro-web 67.37% <100.00%> (+0.47%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...s/taro-h5/src/api/media/audio/InnerAudioContext.ts 74.52% <100.00%> (+13.35%) ⬆️

... and 37 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Single-Dancer Single-Dancer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

审查提交:3c398a65e14b6cb0df36077f83f32fac62a26b8c。

未发现阻止合入的问题,建议合并。

核对了公开 createInnerAudioContext 入口、十类 on/off 监听、CallbackManager 无参清空路径,以及 destroy 和路由停止链路。原生 DOM 事件仍由 EventTarget 分发;用户监听登记与首次播放 startTime 监听分离,保留指定取消、去重、事件参数/this 和分发期间移除行为。

独立验证(固定 PR 快照,复用本地已有依赖,临时调整 Jest 模块映射及 Babel 依赖解析):

  • 音频测试 2 个套件、36 个用例通过。
  • 新增 35 个回归用例运行于基线 0493b52 的音频实现:24 个失败、11 个通过;运行于修复后实现全部通过。
  • H5 全部测试:30 个套件、278 个用例通过。

验证边界:Jest/JSDOM 受控原生事件,未验证真实音频播放、真实浏览器或真机;未重新执行 package/全仓构建或独立类型检查(仓库 Jest 配置关闭 TS diagnostics)。

@Single-Dancer Single-Dancer added this to the 4.3.2 milestone Nov 2, 2026
@Single-Dancer
Single-Dancer force-pushed the fix/h5-audio-off-listeners branch from 3c398a6 to b9e0a46 Compare November 2, 2026 06:21
@Single-Dancer
Single-Dancer merged commit cead17f into NervJS:main Nov 2, 2026
25 checks passed
@Jarvis636431
Jarvis636431 deleted the fix/h5-audio-off-listeners branch November 3, 2026 03:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

H5 InnerAudioContext 的 offXxx() 无参调用未取消事件监听

2 participants