Skip to content

fix(runtime): 修复派发期间监听器变更导致重复调用或抛错 - #19593

Merged
Single-Dancer merged 3 commits into
NervJS:mainfrom
Jarvis636431:fix/runtime-dispatch-listener-mutation
Nov 2, 2026
Merged

Single-Dancer merged 3 commits into
NervJS:mainfrom
Jarvis636431:fix/runtime-dispatch-listener-mutation

Conversation

@Jarvis636431

@Jarvis636431 Jarvis636431 commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

这个 PR 做了什么?

runtime DOM 的 dispatchEvent 直接倒序遍历可变监听器数组。回调取消其他监听器会改变下标,使当前回调重复执行;once 回调先解绑再移除其他监听器时,还可能读取 undefined._stop 并抛错。例如注册 first/second/third,third 取消 first,原实现得到 third/third/second。

派发开始时保存注册快照,调用前按包装器身份检查注册是否仍存在。保留原有倒序调用,跳过尚未调用就被移除的注册;新增注册留到下轮,嵌套派发读取最新列表。移除后重新注册同一用户回调不会复活本轮旧注册。

Closes #19592

这个 PR 是什么类型?

  • 错误修复 (Bugfix)

这个 PR 涉及以下平台:

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

验证

  • 新增 12 个回归用例:原实现 8 失败、4 通过;修复后全部通过。直接使用真实 runtime 源码,没有 mock 派发实现。
  • 覆盖取消自身及其他注册、跳过未执行的已移除注册、移除后重新注册、重复回调的独立注册、新增订阅、同步重入、once、停止传播、this/事件参数、返回值及异常传播。
  • 事件专项测试:3 文件、60 用例通过。
  • pnpm --filter @tarojs/runtime run test:ci --maxWorkers=2:22 文件通过,307 用例通过、1 跳过。Node.js 26.8.1 下通过,并在 Node.js 22.14.0 下重跑通过;pnpm 10.0.0。
  • runtime 包构建通过,退出码 0。
  • 修改文件 ESLint(--no-ignore)、git diff --check 和提交钩子通过。

范围与限制

分支基于官方 main 10002ad884e,仅修改派发实现并新增测试,不改变监听器注册顺序或公共 API。与已合并 #19583 的 once 身份/解绑修复、#19586 的 H5 CallbackManager 快照分发属于不同实现。

每次派发增加一次数组复制,并对快照中的注册查询当前数组。未运行全工作区构建、开发者工具、真机或完整 React/Vue 小程序应用验证。

Summary by CodeRabbit

  • Bug 修复
    • 修正事件分发期间监听器变更的处理:分发开始后新增的监听器不会在本轮触发;轮到已移除监听器时会跳过。嵌套分发则会按更新后的监听器列表执行。

@coderabbitai

coderabbitai Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

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: 8e7e55e9-831c-4ade-b858-925207ac1fbd
Commits

Reviewing files that changed from the base of the PR and between 10002ad and 3e46857.

Files selected for processing: 2
  • packages/taro-runtime/src/dom/element.ts
  • packages/taro-runtime/tests/event-dispatch-mutation.spec.ts

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

Walkthrough

TaroElement.dispatchEvent 现在使用分发开始时的监听器快照。调用前已移除的监听器会被跳过,分发期间新增的监听器不会加入当前轮次。新增测试覆盖监听器变更及相关事件行为。

Changes

事件分发

Layer / File(s) Summary
监听器快照分发与回归测试
packages/taro-runtime/src/dom/element.ts, packages/taro-runtime/tests/event-dispatch-mutation.spec.ts
dispatchEvent 遍历监听器快照,并在调用前检查监听器是否仍然注册。测试覆盖监听器移除、新增、重复注册、嵌套分发、once、传播停止、回调上下文、返回值及异常传播。

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

Change: Bug fix · Severity of issue fixed: Medium

Suggested labels: T-weapp

Suggested reviewers: zakarycode, single-dancer

Merge Risk: Minimal · previous review 3e468

Listener mutations are handled using a snapshot. No merge-blocking issue 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 标题准确概括了主要变更:修复事件派发期间监听器变更导致的重复调用或异常。标题简洁、明确,并与代码和测试变更一致。
Linked Issues check Passed 直接关联的开放 issue 为 #19592。TaroElement.dispatchEvent 在派发开始时复制监听器数组,并按监听器包装器身份检查当前注册列表。该实现保留倒序调用,跳过已移除注册,延后新增注册,并使嵌套派发读取最新列表。新增测试覆盖重复注册、once、停止传播、异常传播、返回值、事件参数和嵌套派发。代码和测试满足 issue #19592 的编码要求。
Out of Scope Changes check Passed 变更仅涉及 packages/taro-runtime/src/dom/element.ts 的派发实现和 packages/taro-runtime/tests/event-dispatch-mutation.spec.ts 的回归测试。两项变更都直接支持 issue #19592。未发现与该 issue 无关的公共 API、注册顺序或其他功能变更。
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: 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

我是兔子,轻敲事件铃,
快照列好监听者的名字。
已移除的回调不再登场,
新来的等下一轮再出发。
嵌套派发读最新名单,
胡萝卜旁,测试全通过。

@codecov

codecov Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 57.93%. Comparing base (0984ff4) to head (0735edb).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #19593   +/-   ##
=======================================
  Coverage   57.93%   57.93%           
=======================================
  Files         470      470           
  Lines       26006    26008    +2     
  Branches     6524     6524           
=======================================
+ Hits        15066    15068    +2     
- Misses       9006     9105   +99     
+ Partials     1934     1835   -99     
Flag Coverage Δ
taro-cli 59.69% <ø> (ø)
taro-runtime 66.07% <100.00%> (+0.02%) ⬆️
taro-web 67.94% <ø> (ø)

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/element.ts 70.00% <100.00%> (+0.30%) ⬆️

... and 30 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-dispatch-listener-mutation branch from 3e46857 to 7793a88 Compare November 2, 2026 09:25
@Single-Dancer Single-Dancer added this to the 4.3.2 milestone Nov 2, 2026
…ime-dispatch-listener-mutation' of https://github.com/Jarvis636431/taro into fix/runtime-dispatch-listener-mutation
@Single-Dancer
Single-Dancer merged commit 8b69314 into NervJS:main Nov 2, 2026
25 checks passed
@Jarvis636431
Jarvis636431 deleted the fix/runtime-dispatch-listener-mutation 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.

[Bug]: runtime 派发期间移除监听器导致回调重复执行或抛错

2 participants