Skip to content

fix(runtime): 修复 once 监听器的取消与解绑时机 - #19583

Merged
Single-Dancer merged 1 commit into
NervJS:mainfrom
Jarvis636431:fix/runtime-once-listener-lifecycle
Nov 2, 2026
Merged

Single-Dancer merged 1 commit into
NervJS:mainfrom
Jarvis636431:fix/runtime-once-listener-lifecycle

Conversation

@Jarvis636431

@Jarvis636431 Jarvis636431 commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

这个 PR 做了什么?

runtime DOM 的 once 监听器额外包装原回调,导致 removeEventListener(type, callback) 无法取消注册;回调返回后才解绑,也使同步重入和回调抛错后再次派发时重复执行。

复用现有监听器包装逻辑,让 oldHandler 直接指向原回调;once 在调用用户代码前通过现有 removeEventListener 路径移除当前包装器。保留每次注册的独立包装、普通监听器和 regionchange 的 begin/end 独立生命周期。

Closes #19582

这个 PR 是什么类型?

  • 错误修复 (Bugfix)

这个 PR 涉及以下平台:

  • 所有小程序(使用 Taro runtime DOM 事件实现的路径)

验证

  • 新增 12 个回归用例,原实现 6 个失败、6 个通过;修复后全部通过。
  • 覆盖原回调取消、同步重入、抛错后解绑、this/事件参数、普通与 once 共用回调、其他监听器及事件类型保留、重复注册、回调内重新注册、事件名大小写、regionchange 取消及 begin/end 独立生命周期,以及 once:false 对照。
  • pnpm --filter @tarojs/runtime run test:ci --maxWorkers=2:20 个文件通过,285 个用例通过、1 个跳过。Node.js 26.8.1 / pnpm 10.0.0。
  • pnpm --filter @tarojs/runtime run build:Node.js 22.14.0 / pnpm 10.0.0,退出码 0。沙箱内首次构建生成产物后未退出,结束空闲进程并在放宽沙箱限制后重跑通过。
  • 修改源码及新增测试 ESLint(--no-ignore)、git diff --check 通过。

分支独立基于官方 main 的 603489c8e73,仅包含 runtime 源码和新增测试两个文件。测试直接调用真实 runtime;未运行全工作区构建、小程序开发者工具或真机验证。

与 #19506 的 shared Events.once、#19529 的未注册监听器移除修复属于不同问题。

Summary by CodeRabbit

  • Bug 修复
    • 修复一次性事件监听器的生命周期处理:回调执行前即移除监听器,避免同步重入或回调报错时重复触发。
    • 改进使用原始回调移除监听器时的处理,并保持事件参数、接收者及事件别名行为一致。

@coderabbitai

coderabbitai Bot commented Oct 30, 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: 1f935e21-8e92-4856-bf3a-2f04d86f1ec3
Commits

Reviewing files that changed from the base of the PR and between 10cb657 and 3e65bd8.

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: 368a331a-62da-4187-b7ee-bdd0355931b7
Commits

Reviewing files that changed from the base of the PR and between 2d264d0 and 0569c72.

Files selected for processing: 2
  • packages/taro-runtime/src/dom/event-target.ts
  • packages/taro-runtime/tests/event-once.spec.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

once 监听器现在会在调用用户回调前移除当前注册。新增测试覆盖原回调取消、同步重入、回调抛错,以及事件类型、别名和 once: false 等行为。

Changes

DOM once 监听器生命周期

Layer / File(s) Summary
移除时机与生命周期回归验证
packages/taro-runtime/src/dom/event-target.ts, packages/taro-runtime/tests/event-once.spec.ts
移除 once 专用包装逻辑,并在调用用户回调前移除当前注册。新增测试覆盖原回调取消、同步重入、异常、事件名规范化、regionchange 别名、重复注册及 once: false。

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: chen-jj, single-dancer

Merge Risk: Minimal · previous review 0569c

Once listeners can now be cancelled and are removed before invocation. No introduced merge-blocking risk was identified.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 标题准确概括了 PR 的主要变更,即修复 runtime 中 once 监听器的取消和解绑时机。标题简洁且与改动内容直接相关。
Linked Issues check Passed [#19582] 需求:once 监听器必须可用原始 callback 取消;必须在执行用户回调前移除当前注册,使同步重入和回调抛错都不会重复调用。

代码证据(merge base 603489c 至 head 0569c72):
1. 删除了 isOnce 时的额外包装和递归 addEventListener。原来的 once 包装器不再套在原回调外层,因此 `rem…
Out of Scope Changes check Passed 本次 PR 的变更范围限定在 packages/taro-runtime/src/dom/event-target.ts(源码修复)和 packages/taro-runtime/tests/event-once.spec.ts(新增回归测试)。

源码变更只涉及 once 监听器的包装与解绑逻辑,直接对应 [#19582] 的需求。删除的额外包装和新增的移除调用均属于同一修复。新增测试覆…
Docstring Coverage Passed No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Full details: Linked Issues check

Explanation

[#19582] 需求:once 监听器必须可用原始 callback 取消;必须在执行用户回调前移除当前注册,使同步重入和回调抛错都不会重复调用。

代码证据(merge base 603489c 至 head 0569c72):

  1. 删除了 isOnce 时的额外包装和递归 addEventListener。原来的 once 包装器不再套在原回调外层,因此 removeEventListener(type, callback) 可以通过现有 item.oldHandler === handler 匹配,取消原回调注册。
  2. 统一包装器内新增 if (isOnce) this.removeEventListener(type, handler)。此处 handler 指向已被替换的包装器,因此移除的是当前注册。该移除发生在 oldHandler.apply 之前,同步重入时该注册已不存在,抛错时也已完成解绑。
  3. regionchange 仍分别调用 begin 和 end 的 addEventListener,各自生成独立包装器,因此两条生命周期互不影响。once: false 路径不执行移除,行为保持不变。

测试证据:PR 描述列出 12 个回归用例,覆盖原回调取消、同步重入、抛错后解绑、普通与 once 共用回调、重复注册和 regionchange。本次未逐行核对测试文件,结论依赖 diff 与 PR 描述中的测试说明。

未覆盖项:issue 不要求小程序开发者工具或真机验证,因此不属于本次判定范围。

次要行为变化:hooks.call('onAddEvent', ...) 对 once 监听器原本调用两次(原始 handler 一次,包装器一次),现在只调用一次且传入原始 handler 与 { once: true }。这是去除额外包装的直接结果。issue 未要求保留该调用次数,因此不影响本次判定。

Full details: Out of Scope Changes check

Explanation

本次 PR 的变更范围限定在 packages/taro-runtime/src/dom/event-target.ts(源码修复)和 packages/taro-runtime/tests/event-once.spec.ts(新增回归测试)。

源码变更只涉及 once 监听器的包装与解绑逻辑,直接对应 [#19582] 的需求。删除的额外包装和新增的移除调用均属于同一修复。新增测试覆盖 issue 所列行为及 PR 描述中的兼容性场景(普通监听器、regionchange、once: false 对照),属于支持该修复的测试,不属于无关变更。

未发现与 [#19506](packages/shared 的 Events.once)或 [#19529](移除未注册处理器)相关的改动,两者在 diff 中均无对应代码。

Full details: Docstring Coverage

Explanation

No eligible functions found in the changed files. Skipping docstring coverage check. 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 0 functions across 2 files.

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

小兔看见监听器,先解开再回声。
重入敲门时,旧回调不再应声。
错误吹过草地,注册已悄然离去。
多次注册各自守候,按次完成职责。
once: false 仍听两遍,移除后归于安静。

@codecov

codecov Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 57.57%. Comparing base (0493b52) to head (3e65bd8).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #19583      +/-   ##
==========================================
+ Coverage   57.56%   57.57%   +0.01%     
==========================================
  Files         470      470              
  Lines       25971    25966       -5     
  Branches     6530     6519      -11     
==========================================
+ Hits        14950    14951       +1     
- Misses       9124     9178      +54     
+ Partials     1897     1837      -60     
Flag Coverage Δ
taro-cli 59.69% <ø> (ø)
taro-runtime 66.00% <100.00%> (+0.13%) ⬆️
taro-web 65.76% <ø> (ø)

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

Files with missing lines Coverage Δ
packages/taro-runtime/src/dom/event-target.ts 98.00% <100.00%> (+10.72%) ⬆️

... and 45 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 force-pushed the fix/runtime-once-listener-lifecycle branch from 10cb657 to 3e65bd8 Compare November 2, 2026 02:50
@Single-Dancer Single-Dancer added this to the 4.3.2 milestone Nov 2, 2026
@Single-Dancer
Single-Dancer merged commit 32ef70b into NervJS:main Nov 2, 2026
25 checks passed
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.

[Bug]: runtime once 监听器无法用原回调取消,且重入或抛错后会重复执行

2 participants