Skip to content

fix(runtime): 修复移除未注册事件处理器时误删已有监听器 - #19529

Merged
Single-Dancer merged 1 commit into
NervJS:mainfrom
sep2:fix/runtime-event-listener-removal
Oct 16, 2026
Merged

Single-Dancer merged 1 commit into
NervJS:mainfrom
sep2:fix/runtime-event-listener-removal

Conversation

@sep2

@sep2 sep2 commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

这个 PR 做了什么?

修复 TaroEventTarget.removeEventListener() 在找不到指定事件处理器时误删已有监听器的问题。

问题

当事件类型已经注册了监听器,但传入的处理器未注册时,findIndex() 返回 -1。当前实现虽然会在开发环境发出警告,随后仍执行 handlers.splice(-1, 1),导致最后一个有效监听器被移除。重复移除同一个处理器也会触发此问题,生产环境同样受影响。

例如,先注册处理器 A、B:

  • 移除未注册的 C,会误删 B。
  • 连续两次移除 A,第二次会误删 B。

修复

在现有警告之后增加 index === -1 的提前返回,保证未匹配的移除操作保留现有监听器。

匹配成功时继续执行原有移除流程,保留处理器包装匹配、事件名小写化以及 once 自动移除行为。新增判断为 O(1),整体仍为 O(H),H 为对应事件类型的处理器数量。

回归测试

在 packages/taro-runtime/tests/event.spec.ts 中增加 3 个用例:

  1. 已有 1 个监听器时,移除未注册处理器后仍能正常派发事件。
  2. 已有 2 个监听器时,移除未注册处理器后全部监听器仍能正常派发事件。
  3. 重复移除同一个处理器时保留其它监听器,并验证事件名大小写归一化及后续正常移除。

前两个用例同时验证监听器数组引用及内容保持一致,且节点模板不会切换。

验证

  • 原版代码上的 3 个新增回归用例全部失败;修复后全部通过。
  • pnpm --filter @tarojs/runtime test:ci:11 个测试文件通过,137 个用例通过、1 个跳过。
  • NODE_ENV=production pnpm --filter @tarojs/runtime test tests/event.spec.ts -t '移除未注册的 handler|重复移除同一个 handler':3 个回归用例通过。
  • pnpm --filter @tarojs/shared build:通过。
  • pnpm --filter @tarojs/runtime build:通过。
  • pnpm exec tsc --project packages/taro-runtime/tsconfig.json --noEmit:通过。
  • pnpm exec eslint packages/taro-runtime/src/dom/event-target.ts packages/taro-runtime/tests/event.spec.ts:通过。
  • git diff --check:通过。

这个 PR 是什么类型?

  • 错误修复(Bugfix)

这个 PR 涉及以下平台

  • 所有小程序

Summary by CodeRabbit

  • Bug Fixes
    • 修复移除未注册或重复移除的事件监听器时,误删其他监听器的问题;剩余监听器现在可正常触发。

@coderabbitai

coderabbitai Bot commented Oct 15, 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
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8c89df78-8bcf-48bc-a121-ff24842922db
📥 Commits

Reviewing files that changed from the base of the PR and between 31b2275 and 3299e7e.

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
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 03307f45-16ca-4650-8e1b-d933976f3ee2
📥 Commits

Reviewing files that changed from the base of the PR and between c48bec6 and 31b2275.

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

removeEventListener 在未找到匹配处理器时现在直接返回,不再删除事件列表中的最后一个处理器。新增测试覆盖未注册处理器和重复移除场景。

Changes

事件监听器移除

Layer / File(s) Summary
未匹配处理器的移除保护与测试
packages/taro-runtime/src/dom/event-target.ts, packages/taro-runtime/tests/event.spec.ts
未找到匹配处理器时,removeEventListener 直接返回。测试验证已有监听器不会被误删、监听器数组引用和更新队列保持不变,并覆盖重复移除及后续事件触发。

Priority: ⬇️ Low

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

Change: Bug fix · Severity of issue fixed: Low

Merge Risk: ⚪ Minimal · up to 31b22

The listener-removal fix is ready to merge after normal checks.

Pre-merge checks: Passed 5
Passed Passed checks: 5 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 标题准确描述了主要变更:修复移除未注册事件处理器时误删已有监听器的问题。标题清晰、具体,并与代码和测试改动一致。
Docstring Coverage Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
✨ 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

兔子轻敲事件门,
未匹配时不动旁人。
重复移除也安稳,
留下监听各司职,
测试跑过月光明。

Comment @coderabbitai help to get the list of available commands.

@Single-Dancer Single-Dancer added this to the 4.3.1 milestone Oct 16, 2026
@Single-Dancer
Single-Dancer force-pushed the fix/runtime-event-listener-removal branch from 31b2275 to 3299e7e Compare October 16, 2026 08:01
@codecov

codecov Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 55.36%. Comparing base (1cf1bbf) to head (3299e7e).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff            @@
##             main   #19529    +/-   ##
========================================
  Coverage   55.35%   55.36%            
========================================
  Files         467      467            
  Lines       25650    25652     +2     
  Branches     6426     6439    +13     
========================================
+ Hits        14199    14201     +2     
+ Misses       9573     9454   -119     
- Partials     1878     1997   +119     
Flag Coverage Δ
taro-cli 60.27% <ø> (ø)
taro-runtime 58.60% <100.00%> (+0.02%) ⬆️
taro-web 57.39% <ø> (ø)

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 80.00% <100.00%> (+0.75%) ⬆️

... and 42 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 merged commit 42f568a into NervJS:main Oct 16, 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.

2 participants